一、删除mysql残留文件,防止之前安装的忘记了
1.查询mysql文件
dpkg --list|grep mysql
2.执行删除操作
sudo apt-get remove mysql-common
sudo apt-get autoremove --purge mysql-server-5.7
dpkg -l|grep ^rc|awk '{print$2}'|sudo xargs dpkg -P
二、开始安装
1. sudo apt-get install mysql-server
第一次运行报错 ,中途需要输入root用户密码
执行 sudo apt-get update 解决
2. sudo apt-get install mysql-client
3.sudo apt-get install libmysqlclient-dev
三、查看mysql状态,版本号
service mysql status
root@VM-0-5-ubuntu:/# mysql -v
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.35-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Reading history-file /root/.mysql_history
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select version();
--------------
select version()
--------------
+-------------------------+
| version() |
+-------------------------+
| 5.7.35-0ubuntu0.18.04.1 |
+-------------------------+
1 row in set (0.00 sec)
mysql> exit;
Writing history-file /root/.mysql_history
Bye
四、登录mysql
mysql -u root -p
4.1 如果安装的时候未输入密码或者是登录提示密码错误进行如下操作
查看默认配置文件:sudo cat /etc/mysql/debian.cnf
下方有账号和密码
4.2修改root密码
UPDATE mysql.user SET authentication_string=PASSWORD('123456'), PLUGIN='mysql_native_password' WHERE USER='root';
五、设置远程访问
mysql -u root -p
use mysql;
select host, user from user;
update user set host = '%' where user = 'root';
关闭服务:service mysql stop
修改配置文件:sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
启动服务:service mysql start
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200416192652665.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzgyNjMzNg==,size_16,color_FFFFFF,t_70