Linux:mysql允许远程连接

更新到2018-6-27

现在使用   阿里云  服务器,关于远程连接MySQL的问题 首先要看看你的  阿里云 服务器 是否设置,开放了3306 端口。



然后再进行如下操作。

(其实很多  服务器  有上一步以后 ,就可以使用了。另:MySQL5.6   和  MySQL5.7  具体库表 里面的字段也有所不同)



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

执行iptables -nL --line-number 
这里写图片描述 
这里显示DROP代表防火墙阻止了3306端口。

2.添加防火墙例外

执行vim /etc/sysconfig/iptables 
这里写图片描述

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

创建用户

create user test identified by '123456';
  • 1
+----------------+------------------+-------------------------------------------+
| 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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

授权

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

修改用户密码

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

删除用户

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

更新到2018-6-27

现在使用   阿里云  服务器,关于远程连接MySQL的问题 首先要看看你的  阿里云 服务器 是否设置,开放了3306 端口。



然后再进行如下操作。

(其实很多  服务器  有上一步以后 ,就可以使用了。另:MySQL5.6   和  MySQL5.7  具体库表 里面的字段也有所不同)



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

执行iptables -nL --line-number 
这里写图片描述 
这里显示DROP代表防火墙阻止了3306端口。

2.添加防火墙例外

执行vim /etc/sysconfig/iptables 
这里写图片描述

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

创建用户

create user test identified by '123456';
  • 1
+----------------+------------------+-------------------------------------------+
| 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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

授权

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

修改用户密码

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

删除用户

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

猜你喜欢

转载自blog.csdn.net/qq_34306360/article/details/80834255
今日推荐