您好,欢迎访问一九零五行业门户网

解决虚拟机linux端mysql数据库无法远程访问_MySQL

bitscn.com
解决虚拟机linux端mysql数据库无法远程访问
前天无聊在虚拟机中的centos上装了一个mysql玩玩,突然想用jdbc连接用java操作下数据库,可是怎么都连接不上,具体情况为:
1.ping 192.168.16.128 成功
2.telnet 192.168.16.128 3306 拒绝访问 在3306端口,可见centos防火墙屏蔽了3306端口
修改方式为:
切换到root用户
打开iptables的配置文件:vi /etc/sysconfig/iptables
修改centos防火墙时注意:一定要给自己留好后路,留vnc一个管理端口和ssh的管理端口
内容如下:
# firewall configuration written by system-config-securitylevel
# manual customization of this file is not recommended.
*filter
:input accept [0:0]
:forward accept [0:0]
:output accept [0:0]
:rh-firewall-1-input - [0:0]
-a input -j rh-firewall-1-input
-a forward -j rh-firewall-1-input
-a rh-firewall-1-input -i lo -j accept
-a rh-firewall-1-input -p icmp –icmp-type any -j accept
-a rh-firewall-1-input -p 50 -j accept
-a rh-firewall-1-input -p 51 -j accept
-a rh-firewall-1-input -m state –state established,related -j accept
-a rh-firewall-1-input -m state –state new -m tcp -p tcp –dport 22 -j accept
-a rh-firewall-1-input -j reject –reject-with icmp-host-prohibited
commit
可见我的防火墙只开了22端口,因此我们添加3306端口
修改centos防火墙需要注意的是,你必须根据自己服务器的情况来修改这个文件。
举例来说,如果你不希望开放80端口提供web服务,那么应该相应的删除这一行:
-a rh-firewall-1-input -m state –state new -m tcp -p tcp –dport 80 -j accept
我们添加
-a rh-firewall-1-input -m state –state new -m tcp -p tcp –dport 3306 -j accept
全部修改完之后重启iptables:service iptables restart
你可以验证一下是否规则都已经生效:iptables -l
这样,我们就完成了centos防火墙的设置修改。
这时我们通过telnet 192.168.16.128 3306 成功
但此时我们仍不能访问,因为mysql没有授权远程用户
如,你想root2使用root2从任何主机连接到mysql服务器的话。 
切换到mysql中
mysql>grant all privileges on *.* to 'root2'@'%'identified by 'root2' with grant option; 
或者如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码 
mysql>grant all privileges on *.* to 'myuser'@'192.168.1.3'identified by 'mypassword' with grant option; 
然后执行:
mysql>flush rivileges;
mysql>exit;
使修改生效.
这时我们在远程:mysql -h 192.168.16.128 -u root2 -p 输入密码 root2 成功~
用jdbc连接~成功~撒花~~~
bitscn.com
其它类似信息

推荐信息