配置 Mysql 允许远程连接

配置 Mysql 允许远程连接

MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1)来连接访问. 这对于 Web-Server 与 MySql-Server 都在同一台服务器上的网站架构来说是没有问题的. 但随着网站流量的增加, 后期服务器架构可能会将 Web-Server 与 MySql-Server 分别放在独立的服务器上, 以便得到更大性能的提升, 此时 MySql-Server 就要修改成允许 Web-Server 进行远程连接.

假设我们有:
Web-Server : 192.168.1.100 //ubuntu
Mysql-Server : 192.168.1.101 //xp

我们可以按照下面的步骤修改:
1, 登录 Mysql-Server 连接本地 mysql (默认只允许本地连接)

01 Microsoft Windows XP [版本 5.1.2600]
02 (C) 版权所有 1985-2001 Microsoft Corp.
03  
04 C:\Documents and Settings\kuco>mysql -h localhost -u root -p
05 Enter password:
06 Welcome to the MySQL monitor.  Commands end with ; or \g.
07 Your MySQL connection id is 13
08 Server version: 5.1.45-community-log MySQL Community Server (GPL)
09  
10 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
11  
12 mysql>

2, 修改 Mysql-Server 用户配置

01 mysql> USE mysql; -- 切换到 mysql DB
02 Database changed
03 mysql> SELECT UserPassword, Host FROM user-- 查看现有用户,密码及允许连接的主机
04 +------+----------+-----------+
05 User Password | Host      |
06 +------+----------+-----------+
07 | root |          | localhost |
08 +------+----------+-----------+
09 1 row in set (0.00 sec)
10  
11 mysql> -- 只有一个默认的 root 用户, 密码为空, 只允许 localhost 连接
12 mysql> -- 下面我们另外添加一个新的 root 用户, 密码为空, 只允许 192.168.1.100 连接
13 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.100' IDENTIFIED BY '' WITH GRANT OPTION;
14  
15 mysql> -- 当然我们也可以直接用 UPDATE 更新 root 用户 Host, 但不推荐, SQL如下:
16 mysql> -- UPDATE user SET Host='192.168.1.100' WHERE User='root' AND Host='localhost' LIMIT 1;

3, 修改 Mysql 配置文件 my.ini

1 bind-address = 127.0.0.1
2 将 bind-address = 127.0.0.1 这一行注释掉, 即修改为:
3 #bind-address = 127.0.0.1
4 到此 Mysql-Server 端配置就完成了.

4, 连接 Web-Server , 检查一下是否能连上

1 kuco@kuco-desktop:/$ /opt/lampp/bin/mysql -h 192.168.1.101 -u root -p
2 Enter password:
3 Welcome to the MySQL monitor.  Commands end with ; or \g.
4 Your MySQL connection id is 23
5 Server version: 5.1.45-community-log MySQL Community Server (GPL)
6  
7 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
8  
9 mysql> -- 一切OK

当然也可以用 php 程序去验证:

1 <?php
2 mysql_connect("192.168.1.101""root"""or die("Could not connect: " . mysql_error());
3 ?>
This entry was posted in MysqlPHP  and tagged . Bookmark the  permalink. Trackbacks are closed, but you can  post a comment.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值