连接到不同服务器上的 mysql [英] Connect to mysql on a different server

查看:47
本文介绍了连接到不同服务器上的 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一台服务器连接到 mysql 数据库服务器.以下是两台服务器上的配置.

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

服务器 1:我用 php 和 apache 服务安装了 xampp,它有以下 ip 172.x1.x1.x1

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

服务器 2:我安装了mysql,它有以下ip 172.x1.x1.x2.

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

<?php
    //Database connection constants
    define("DATABASE_HOST", "172.x1.x1.x2");
    define("DATABASE_USERNAME", "root");
    define("DATABASE_PASSWORD", "");
    define("DATABASE_NAME", "management");
?>

上述脚本位于名为 app_config.php 的文件中,位于服务器 1以下脚本位于名为 connect.php 的文件中,它也位于服务器 1

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

<?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)或死($table_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第 4 行的 .php

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

致命错误:在第 5 行调用 C:\xampp\htdocs\scripts\functions\database\connect.php 中未定义的函数 handle_error()

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.

推荐答案

由于您的数据库服务器与您的 php/apache 服务器不同,您需要将主机名指定为 172.x1.x1.x2在 mysql-php 连接字符串中.

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.

还要确保 mysql 用户 root 具有远程连接权限.其他明智的 mysql-server 将不允许您的 root 用户远程登录.即来自您的 server1.

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.

您可以从 mysql.user 表中确定.

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)

% 表示任何主机.

要创建具有远程连接权限的用户,请使用以下mysql查询:

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

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

这篇关于连接到不同服务器上的 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆