Article Directory
MySQL 8 allows remote hosts to connect the local server
Environmental Information
Ubuntu version information:
$ lsb_release --all
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04
Codename: bionic
MySQL version information:
$ mysql --version
mysql Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)
New accounts, assign permissions
By default, MySQL only allows local connections, view mysql
the database user
table, you can see all users connect to the server only allows localhost:
mysql> select host, user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
4 rows in set (0.00 sec)
We can create a new account:
mysql> create user 'root'@'192.168.1.4' // 'user_name'@'host_name'
-> identified by 'password'; // 密码
Query OK, 0 rows affected (0.09 sec)
The above command, said: create an account, user name root
, password password
, IP to allow 192.168.1.4
the host to connect to the machine MySQL server.
The new account is added to the mysql
database user
table:
mysql> select host,user from user;
+-------------+------------------+
| host | user |
+-------------+------------------+
| 192.168.1.4 | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-------------+------------------+
5 rows in set (0.00 sec)
Then assign permissions to the account:
mysql> GRANT ALL // 所有权限
-> ON *.* // 任意数据库的任意表
-> TO 'root'@'192.168.1.4'; // 给那一个账户
Query OK, 0 rows affected (0.11 sec)
Open ports
Check the status of the firewall:
$ sudo ufw status
[sudo] password for mk:
Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
We need to open TCP port 3306 protocol:
$ sudo ufw allow 3306/tcp
Rule added
Rule added (v6)
Reload the firewall rules:
$ sudo ufw reload
Firewall reloaded
Check firewall status again:
$ sudo ufw status
Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
3306/tcp ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
3306/tcp (v6) ALLOW Anywhere (v6)
After completion of the above operations, the designated IP remote clients can connect to the machine MySQL server through the account.
reference
Adding Accounts, Assigning Privileges, and Dropping Accounts