linux下mysql允许远程连接

时间:2022-05-24 14:46:20

1.查看linux防火墙是否开放3306端口

执行iptables -nL --line-number
linux下mysql允许远程连接
这里显示DROP代表防火墙阻止了3306端口。

2.添加防火墙例外

执行vim /etc/sysconfig/iptables
linux下mysql允许远程连接

3.重启防火墙

执行service iptables restart
查看是否变为ACCEPT

4.创建远程连接用户并授权

mysql> select Host,User,Password from mysql.user;
+----------------+------------------+-------------------------------------------+

| Host | User | Password |
+----------------+------------------+-------------------------------------------+

| localhost | root | *836E233974EBE6EA32F95F890A91363F8427F78B |
| iz94926clkiz | root | *
836E233974EBE6EA32F95F890A91363F8427F78B |
| 127.0.0.1 | root | *836E233974EBE6EA32F95F890A91363F8427F78B |
| ::1 | root | *
836E233974EBE6EA32F95F890A91363F8427F78B |
| localhost | debian-sys-maint | *1460ED3535ABDBB887F9E5F57F40A2354610CDF3 |
+----------------+------------------+-------------------------------------------+

rows in set (0.00 sec)

创建用户

create user test identified by '123456';
+----------------+------------------+-------------------------------------------+
| Host | User | Password |
+----------------+------------------+-------------------------------------------+

| localhost | root | *836E283974EBE6EA32F95F890A91363F8427F78B |
| iz949s6clkiz | root | *
836E283974EBE6EA32F95F890A91363F8427F78B |
| 127.0.0.1 | root | *836E283974EBE6EA32F95F890A91363F8427F78B |
| ::1 | root | *
836E283974EBE6EA32F95F890A91363F8427F78B |
| localhost | debian-sys-maint | *1460ED35E5ABDBB887F9E5F57F40A2354610CDF3 |
| % | test | *
6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+----------------+------------------+-------------------------------------------+
rows in set (0.00 sec)

授权

grant all privileges on *.* to 'test'@'%'identified by '123456' with grant option;
flush privileges;

修改用户密码

update mysql.user set password=password('新密码') where User="test" and Host="localhost";

删除用户

delete from user where User='test' and Host='localhost';