连云端 MySQL 的时候发现死活连不上,ssh 连接本地也没办法登录,估计是密码不记得了),查了半天才找到解决方案
修改 MySQL 密码
首先要将 MySQL 设置成登录无认证
网上说的 MySQL 配置在/etc/my.cnf
文件中,查看文件内容如下
1
2
3
4
5
6
7
8
9
10
11
|
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
|
可以看出,配置文件引入了/etc/my.cnf.d
目录下的文件,我的目录下有mysql-server.cnf
和mysql-default-authentication-plugin.cnf
两个文件,两个文件都是 MySQL 的配置文件,在哪改都一样,只需要在[mysqld]
的下方插入skip-grant-tables
这一条即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#
# This group are read by MySQL server.
# Use it for options that only the server (but not clients) should see
#
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
# 加入这一行
skip-grant-tables
|
随后就可以直接输入mysql
连接数据库了
进入数据库之后,由于我们跳过了验证环节(不加载验证表),所以需要手动加载才能对密码等进行修改,输入:
如果不输入这一行,会有如下报错:
1
|
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
|
加载完毕之后,在 MySQL 8.x 下,需要输入:
1
|
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'your_password';
|
修改密码,这里使用%
作为通配符,允许所有域名下的用户连接数据库
如果提示密码不符合要求,也可以修改密码要求,输入:
1
|
SHOW VARIABLES LIKE 'validate_password%';
|
会显示这样一张表:
1
2
3
4
5
6
7
8
9
10
11
|
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 6 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 1 |
+--------------------------------------+-------+
|
可以输入
1
2
|
SET GLOBAL validate_password.length = 6;
SET GLOBAL validate_password.special_char_count = 0;
|
等进行修改,就可以使用不同强度的密码了
修改完成,需要把skip-grant-tables
从配置文件中删除,然后输入:
1
|
sudo systemctl restart mysqld
|
重启 MySQL 服务,随后就可以远程连接数据库了
检查防火墙
如果仍然无法连接,则需要考虑是否是防火墙的问题,首先尝试服务器域名是否能正常 ping 通,如果可以,再输入:
1
|
telnet 11.22.33.44 3306
|
查看状态,MacOS 下没有telnet
命令的,可以使用brew install telnet
安装
如果结果是:
1
2
|
telnet: connect to address 11.22.33.44: Connection refused
telnet: Unable to connect to remote host
|
则说明端口没有开放,首先查看云服务器的管理后台防火墙是否放开了3306
端口,如果已开放,再输入:
1
|
firewall-cmd --query-port=3306/tcp
|
查看是否开放,如果输出no
则再输入:
1
|
firewall-cmd --zone=public --add-port=3306/tcp --permanent
|
开放端口,开放后需要输入:
1
|
systemctl restart firewalld
|
重启防火墙。如果想要关闭某个端口,则可以输入:
1
|
firewall-cmd --remove-port=3306/tcp --permanent
|
修改完成后同样需要重启防火墙服务,下面列举一些其他防火墙操作指令:
查看防火墙状态
1
|
systemctl status firewalld
|
关闭防火墙
1
|
systemctl stop firewalld
|
打开防火墙
1
|
systemctl start firewalld
|
开放一段端口
1
|
firewall-cmd --zone=public --add-port=40000-45000/tcp --permanent
|
查看开放的端口列表
1
|
firewall-cmd --zone=public --list-ports
|
查看被监听(Listen)的端口
检查端口被哪个进程占用