mysql ubuntu 开启3306端口,设置远程访问

远程登陆数据库的时候出现了下面出错信息 :
ERROR 2003 ( HY000 ) : Can 't connect to MySQL server on ' xxx.xxx.xxx.xxx ',
经过今天下午的反复纠结,关于MySql数据库无法远程连接的问题可以归结为以下几点:
1). 没有授予相应的权限:

例如,你想root使用123456从任何主机连接到mysql服务器

mysql>GRANT ALL PRIVILEGES ON *.* TO ' root '@' % ' IDENTIFIED BY ' 123456 ' WITH GRANT OPTION;
mysql>FLUSH RIVILEGES

如果你想允许用户jack从ip为10.10.50.127的主机连接到mysql服务器,并使用654321作为密码

mysql>GRANT ALL PRIVILEGES ON *.* TO ' jack '@’10.10.50.127’ IDENTIFIED BY ' 654321 ' WITH GRANT OPTION;
mysql>FLUSH RIVILEGES

2). 修改mysql数据库中的user表使相应的用户能从某一主机登陆

mysql -u root –p
mysql>use mysql;
mysql>update user set host = ' % ' where user = ' root ';
mysql>select host, user from user;

3). 防火墙禁止了3306端口,以ufw为例    

root@xxx:~/python_pro/newsite# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
root@xxx:~/python_pro/newsite# ufw allow 3306
Rule added
Rule added (v6)
root@vultr:~/python_pro/newsite# ufw reload
Firewall reloaded

 


4). 修改MySQL的配置文件
(我这边是在这个目录,每个人目录不同) /etc/mysql/mysql.conf.d/mysqld.cnf,
因为默认3306端口只允许本地访问的,注释掉这行

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.

#bind-address = 127.0.0.1

然后重启Mysql,service mysql restart

 
posted @ 2019-01-04 11:34  月河  阅读(4727)  评论(0编辑  收藏  举报