mysql 不同服务器_连接到MySQL在不同的服务器

bd96500e110b49cbb3cd949968f18be7.png

Im trying to connect to mysql database server from another server. The following are configurations on both servers.

Server 1:

I installed xampp with php and apache services and it has the following ip 172.x1.x1.x1

Server 2:

I installed mysql and it has the following ip 172.x1.x1.x2.

the following is the connection script I am using to connect to the database

//Database connection constants

define("DATABASE_HOST", "172.x1.x1.x2");

define("DATABASE_USERNAME", "root");

define("DATABASE_PASSWORD", "");

define("DATABASE_NAME", "management");

?>

The above script is in a file called app_config.php and its located in server 1

The following script is in a file called connect.php and its also located in server 1

require 'app_config.php';

$connect_error = 'Sorry we\'experiencing connection problems.';

$table_error = 'Table not found';

mysql_connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD)

or die($connect_error);

mysql_select_db(DATABASE_NAME)

or die($table_error);

?>

now when I try to connect I get the following error

Warning: mysql_connect() [function.mysql-connect]: Host 'hr1.saqa.co.za' is not allowed to connect to this MySQL server in C:\xampp\htdocs\scripts\functions\database\connect.php on line 4

Fatal error: Call to undefined function handle_error() in C:\xampp\htdocs\scripts\functions\database\connect.php on line 5

It will be awsome if you can help me guys.

解决方案

Since your database server is different from your php/apache server you need to specify the hostname as 172.x1.x1.x2 in mysql-php connection string.

Also make sure that mysql user root have remote connection permission. Other wise mysql-server will not allow your root user to login remotely. i.e. from your server1.

You can make sure that from mysql.user table.

mysql> select Host,User from user where User = "root";

+------------+------+

| Host | User |

+------------+------+

| 127.0.0.1 | root |

| ::1 | root |

| localhost | root |

| sgeorge-mn | root |

| % | root |

+------------+------+

4 rows in set (0.01 sec)

% means any host.

To create a user with remote connection permission, use following mysql query:

mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'your_password';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值