Ubuntu 20.04 + MySQL 8 安装记录
安装
清除已有
$ dpkg --list|grep mysql
$ sudo apt-get autoremove --purge mysql-server-8.0
$ dpkg -l|grep ^rc|awk ‘{print$2}’|sudo xargs dpkg -P
$ sudo apt-get autoremove --purge mysql-common
$ rm -fr /var/lib/mysql
$ dpkg --list|grep mysql
安装 MySQL
$ sudo apt update
$ sudo apt install mysql-server # 默认 mysql 8.0
配置
使用安全脚本配置 MySQL
$ sudo mysql_secure_installation
按照提示配置即可。可选择配置 root 是否启用远程登录,是否保留匿名账户和测试数据库等。
远程登录
修改配置文件,使得 MySQL 接受任意 IP 地址访问。
$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf # 将 bind-address = 127.0.0.1 改为 bind-address = 0.0.0.0
如无法远程登录,可从以下几个方面排查
- 云服务器提供商安全组
是否放通 3306 端口 云主机防火墙
如 iptables。$ sudo iptables -L # 查看现有规则 $ sudo iptables -F # 直接清除所有规则
- MySQL 配置文件中 bind-address
MySQL 中用户 host,启用远程登录 host 应为 % 或指定 IP 地址
登录进 MySQL 查看。以下 test 对应 host 为 % 可远程登录,root 对应 localhost 不可远程登录mysql> select user, host from mysql.user; +------------------+-----------+ | user | host | +------------------+-----------+ | test | % | | debian-sys-maint | localhost | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+
密码
MySQL 8 更改了密码规则,可能导致第三方无法连接。
将密码规则由 caching_sha2_password 更换为 mysql_native_password 即可。
mysql> select plugin from mysql.user;
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
| caching_sha2_password |
| caching_sha2_password |
| caching_sha2_password |
| auth_socket |
+-----------------------+
mysql> alter user 'test'@'%' identified with mysql_native_password by 'your_password';
此处评论已关闭