Ubuntu系统安装与配置MySQL

一、安装MySQL

1. 安装mysql服务

sudo apt-get update
sudo apt install mysql-server-5.7

2. mysql版本

mysql -V

 3. 查看MySQL默认账号和密码

sudo cat /etc/mysql/debian.cnf

二、配置MySQL

sudo mysql_secure_installation
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N 

#2
 Please set the password for root here...
 New password: (输入密码)
Re-enter new password: (重复输入)

#3
 By default, a MySQL installation has an anonymous user,
 allowing anyone to log into MySQL without having to have
 a user account created for them...
 Remove anonymous users? (Press y|Y for Yes, any other key for No) : N 

#4
 Normally, root should only be allowed to connect from
 'localhost'. This ensures that someone cannot guess at
 the root password from the network...
 Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N 

#5
 By default, MySQL comes with a database named 'test' that
 anyone can access...
 Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N 

#6
 Reloading the privilege tables will ensure that all changes
 made so far will take effect immediately.
 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y 

三、查看mysql服务状态

systemctl status mysql.service
 ps -ef|grep mysqld

四、修改root账户秘密认证方式

sudo cat /etc/mysql/debian.cnf

password后面的就是密码

然后在命令行输入:

mysql -u debian-sys-maint -p
  • 在出现的Enter password输入上面的密码 ,进入mysql命令模式
  • 创建数据库,修改密码

use mysql;

update mysql.user set authentication_string=('password') where user='root' and Host ='localhost';
update user set plugin="mysql_native_password"; 
flush privileges;
quit;

五、配置远程访问mysql

1.修改配置文件,注释掉bind-address = 127.0.0.1

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

2. 保存退出,进入mysql

mysql -uroot -p
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;

重启

扫描二维码关注公众号,回复: 16397953 查看本文章
sudo service mysql restart

查看状态

systemctl status mysql.service

六、删除MySQL

删除 mysql:

sudo apt autoremove --purge mysql-server-*
sudo apt remove mysql-server
sudo apt autoremove mysql-server
sudo apt remove mysql-common

清理残留数据

dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

猜你喜欢

转载自blog.csdn.net/Mouer__/article/details/125520447