设置 Linux 服务器中 MySQL 允许远程访问

开启 MySQL 远程访问权限: 在linux系统上登陆mysql服务。

-- root 是用户名
[root@localhost ~]# mysql -u root -p
Enter password: --  输入密码

创建远程连接 MySQL 的用户:

-- 创建用户、密码及权限范围 第一个 roo t为用户名 @后为适用的主机,‘%’表示所有电脑都可以访问连接,第二个 root 为密码
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.0.2' IDENTIFIED BY 'root' WITH GRANT OPTION;                 
Query OK, 0 rows affected (1.57 sec)

-- 立即生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

查看数据库用户:

-- 使用 mysql 库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

-- 查看用户
mysql> SELECT DISTINCT CONCAT('User: [', user, '''@''', host, '];') AS USER_HOST FROM user;   
+---------------------------------------+
| USER_HOST                             |
+---------------------------------------+
| User: [root'@'127.0.0.1];             |
| User: [root'@'192.168.0.2];           |
| User: [root'@'::1];                   |
| User: [root'@'localhost];             |
| User: [root'@'localhost.localdomain]; |
+---------------------------------------+
5 rows in set (0.00 sec)

创建成功。

查看端口:

mysql> show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.01 sec)

 

开启3306端口:

[root@localhost ~]# vim /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
                                                                                                                       
"/etc/sysconfig/iptables" 14L, 543C  

 注意:一定要加在后两行的前面。

 

重启防火墙:

[root@localhost ~]# service iptables restart
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:清除防火墙规则:                                 [确定]
iptables:正在卸载模块:                                   [确定]
iptables:应用防火墙规则:                                 [确定]

 

 查看服务器ip地址:

[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:16:F4:E6  
          inet addr:192.168.0.123  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: hjjj:jjji::iii:oooo:oooo:ioio/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2931 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1631 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:322681 (315.1 KiB)  TX bytes:266043 (259.8 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

 

连接测试:

 

posted @ 2017-07-01 18:09  Chinda  阅读(31367)  评论(1编辑  收藏  举报