php 连接到不同服务器上的 mysql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14438544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to mysql on a different server
提问by user1783675
Im trying to connect to mysql database server from another server. The following are configurations on both servers.
我试图从另一台服务器连接到 mysql 数据库服务器。以下是两台服务器上的配置。
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.
服务器 1:我用 php 和 apache 服务安装了 xampp,它有以下 ip 172.x1.x1.x1
服务器2:我安装了mysql,它有以下ip 172.x1.x1.x2。
the following is the connection script I am using to connect to the database
以下是我用来连接数据库的连接脚本
<?php
//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
上面的脚本位于名为 app_config.php 的文件中,位于服务器 1 下面的脚本位于名为 connect.php 的文件中,也位于服务器 1 中
<?php
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
mysql_select_db(DATABASE_NAME) 或 die($table_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
警告: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 4号线
致命错误:在第 5 行调用 C:\xampp\htdocs\scripts\functions\database\connect.php 中未定义的函数 handle_error()
It will be awsome if you can help me guys.
如果你能帮助我,那就太好了。
采纳答案by Suku
Since your database server is different from your php/apache server you need to specify the hostname as 172.x1.x1.x2in mysql-php connection string.
由于您的数据库服务器与您的 php/apache 服务器不同,您需要172.x1.x1.x2在 mysql-php 连接字符串中指定主机名。
Also make sure that mysql user roothave remote connection permission. Other wise mysql-server will not allow your rootuser to login remotely. i.e. from your server1.
还要确保 mysql 用户root具有远程连接权限。其他明智的 mysql-server 将不允许您的root用户远程登录。即从您的 server1。
You can make sure that from mysql.usertable.
你可以从mysql.user表中确定。
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 mysqlquery:
要创建具有远程连接权限的用户,请使用以下mysql查询:
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'your_password';
回答by mohammad mohsenipur
1)Make sure firewall Allow MySQL.
1) 确保防火墙允许 MySQL。
2)Make sure MySQL remote connection permission Allowed .
2) 确保 MySQL 远程连接权限 Allowed 。

