首先更新软件源并安装 MySQL。

1
2
sudo apt update
sudo apt install mysql-server

检查一下服务是否正常启动。

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo service mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-12-12 09:15:19 UTC; 20min ago
Main PID: 23378 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 28547)
Memory: 356.2M
CGroup: /system.slice/mysql.service
└─23378 /usr/sbin/mysqld

Dec 12 09:15:19 instance-test systemd[1]: Starting MySQL Community Server...
Dec 12 09:15:19 instance-test systemd[1]: Started MySQL Community Server.

接下来连接一下测试安装是否成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo mysql -u root
use mysql;
SELECT Host, User, authentication_string FROM user;
+-----------+------------------+------------------------------------------------------------------------+
| Host | User | authentication_string |
+-----------+------------------+------------------------------------------------------------------------+
| localhost | debian-sys-maint | ********************************************************************** |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | |
+-----------+------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)

可以看到,这里 root 账号只有 localhost,且密码为空,需要我们手动设置密码并配置远程连接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
CREATE USER 'root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
SELECT Host, User, authentication_string FROM user;
+-----------+------------------+------------------------------------------------------------------------+
| Host | User | authentication_string |
+-----------+------------------+------------------------------------------------------------------------+
| % | root | ********************************************************************** |
| localhost | debian-sys-maint | ********************************************************************** |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | ********************************************************************** |
+-----------+------------------+------------------------------------------------------------------------+
6 rows in set (0.00 sec)
flush privileges;
exit

仅在这里配置还不够,我们还需要修改 MySQL 的配置文件。

1
2
sudo service mysql stop
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

将 bind-address 前加上注释符号 #,或将后面的值改为 0.0.0.0。

1
2
#bind-address           = 127.0.0.1
bind-address = 0.0.0.0

重新启动 MySQL 服务。

1
sudo service mysql start

在其他计算机上尝试连接此 MySQL 服务器。

1
2
mysql -h xxx.xxx.xxx.xxx -u root -p
Enter password:

如果遇到了报错 ERROR 2003 (HY000): Can’t connect to MySQL server on ‘xxx.xxx.xxx.xxx:3306’ (10060),则可能是防火墙的问题,需要开放 3306 端口。但是,别忘了 ssh 的 22 端口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo ufw allow 22
sudo ufw allow 3306/tcp
sudo ufw reload
sudo ufw enable
sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To Action From
-- ------ ----
22 ALLOW IN Anywhere
3306/tcp ALLOW IN Anywhere
22 (v6) ALLOW IN Anywhere (v6)
3306/tcp (v6) ALLOW IN Anywhere (v6)

然后移除 iptables 防火墙,不然会冲突,ufw 规则不会生效。

1
2
sudo apt purge netfilter-persistent
sudo reboot

再次尝试连接,就成功啦!