主要介绍在Ubuntu系统上安装mysql 8.x 版本的数据库,以及远程连接配置
1. 安装
sudo apt install mysql-server
如果apt命令执行不了,执行命令:
sudo apt update
2. 修改配置
执行命令:
sudo mysql_secure_installation
根据引导提示,进行选择:
Validate Password Component … (使用密码强度校验组件) n
New Password:(设置新密码,并重复一遍)
Remove anonymous users(删除匿名用户)n
Disallow root login remotely(拒绝远程root账号登录)n
Remove test database and access to it(移除test数据库) n
Reload privilege tables now(现在就重新载入权限表) y
如果提示:
… Failed! Error: SET PASSWORD has no significance for user ‘root’@‘localhost’
执行下述操作:
sudo mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by'mynewpassword';
mysql> exit
3. 配置远程连接
# 编辑mysql配置文件
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 注释下行
# bind-address=127.0.0.1
配置远程访问:
sudo mysql -uroot -p
# 在mysql中依次执行下述sql语句
mysql> use mysql;
mysql> update user set Host='%' where User='root';
mysql> select User, Host from user;
# 查询结果
+------------------+-----------+
| User | Host |
+------------------+-----------+
| root | % |
| uav | % |
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
# 刷新
mysql> flush privileges;
4. 修改root密码
MySQL8中修改了用户密码的加密方式,root用户的默认plugin值是auth_socket,将它改为caching_sha2_password:
# 修改加密方式
mysql> select user,plugin,authentication_string from user;
mysql> update user set plugin="caching_sha2_password" where user='root';
# 修改后可以看到authentication_string项的值改为NULL了
mysql> select user,plugin,authentication_string from user;
mysql> update user set authentication_string=sha2("1234",32) where user='root';
mysql> select user,plugin,authentication_string from user;
mysql> flush privileges;
# 修改root密码
mysql> alter user 'root'@'%' identified with mysql_native_password by '1234%^&*';
mysql> flush privileges;
5. 创建数据库
mysql> create database 数据库;
6. 创建新用户,并授予权限
mysql> create user '用户'@'%' identified by '密码';
mysql> grant all privileges on 数据库.* to '用户'@'%';
mysql> flush privileges;
7. 重启mysql服务,查看端口状态
# 查看3306端口是否打开
netstat -an|grep 3306
# 重启mysql服务
sudo service mysql restart
# 再次查看端口
netstat -an|grep 3306
如果变成 :::3306 代表远程配置成功,可以通过另一台主机Navicat连接测试
8. 防火墙开放数据库端口
# 查看防火墙状态及放行的端口
sudo ufw status
# 开启防火墙
sudo ufw enable
# 关闭防火墙
sudo ufw disable
# mysql数据库默认端口3306,防火墙添加放行3306端口
sudo ufw allow 3306
9. 卸载MySQL
- 查看查看MySQL的依赖项
dpkg --list|grep mysql
- 卸载:
sudo apt-get remove mysql-common
sudo apt-get autoremove --purge mysql-server-8.0
- 查看剩余依赖
dpkg --list|grep mysql
- 继续删除剩余依赖项
sudo apt-get autoremove --purge mysql-****