Home  >  Article  >  Database  >  mysql root 远程登陆问题的解决_MySQL

mysql root 远程登陆问题的解决_MySQL

WBOY
WBOYOriginal
2016-05-27 14:29:40917browse

bitsCN.com

    今天使用mysql workbench 连接vmware上的mysql时,一直无法连接,查阅了网上的相关资料之后,收集了一些解决方法:
1、授权法:mysql的root用户默认情况下只能在本地登陆,不能远程,若远程连接,需要授权: 
    mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    mysql>FLUSH PRIVILEGES;

    ('root'@'%',指定root用户可以在任意一台计算机上连接数据库服务器,若要指定特定的计算机,可将%替换为特定的IP)

2、改表法:和授权法类似,授权法的结果是在数据库mysql的user表中添加一条记录,host列值为‘%’,user列值为‘root’,如下表:

+-----------------------+------+
| host                           | user |
+-----------------------+------+
| %                              | root |
| 127.0.0.1                   | root |
| localhost                     |        |
| localhost                     | root |
| localhost.localdomain    |        |
| localhost.localdomain    | root |
+-----------------------+------+

3、开放3306端口:网上更多的是强调root用户的授权方法,但是如果只是给root授权而没有开放3306端口,仍旧是无法连接。
开放端口的方法如下:
打开文件 /etc/sysconfig/iptables(该文件路径因操作系统而异),文件内容如下:

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
添加黑体字内容以打开3306端口,重启iptables: 
#service iptables restrat

4、还有一种方法说要修改mysql的配置文件my.cnf中的bind-address选项,我的配置文件中没有这一选项,加上之后,没有效果,不知道什么原因。 

以上四个地方,经反复试验后,其中授权root和开放3306端口是必须的,缺一不可。

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn