2012-06-12 171 views
-1

我有两个服务器名为server1和server2.both具有不同的静态IP地址。我想从server1访问server2数据库。两台服务器我已经安装了PHPmyadmin.In Server1操作系统是Ubuntu,在server2 fedora12中。我想连接一个服务器数据库到另一台服务器

我已经做了this..mysql错误13即将

在Server2上的my.cnf包含

[mysqld] 
datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 
user=mysql 
# Default to using old password format for compatibility with mysql 3.x 
# clients (those using the mysqlclient10 compatibility package). 
old_passwords=1 

# Disabling symbolic-links is recommended to prevent assorted security risks; 
# to do so, uncomment this line: 
# symbolic-links=0 
# To allow mysqld to connect to a MySQL Cluster management daemon, uncomment 
# these lines and adjust the connectstring as needed. 
#ndbcluster 
#ndb-connectstring="nodeid=4;host=localhost:1186" 
[mysqld_safe] 
log-error=/var/log/mysqld.log 
pid-file=/var/run/mysqld/mysqld.pid 
[ndbd] 
# If you are running a MySQL Cluster storage daemon (ndbd) on this machine, 
# adjust its connection to the management daemon here. 
# Note: ndbd init script requires this to include nodeid! 
connect-string="nodeid=2;host=localhost:1186" 
[ndb_mgm] 
# connection string for MySQL Cluster management tool 
connect-string="host=localhost:1186" 

回答

0

尝试创建于一个connect_server2.php文件具有以下内容:

<? 
    $server2 = '1.2.3.4'; // the IP of server2 
    echo mysql_connect($server2, 'username', 'password') ? 'you have been connected' : 'cannot connect to server2'; 
?> 
1

如果你的问题是一个远程MySQL数据库的连接,那么你可以尝试代码如下:

$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password'); 

曾经为我工作过一次!

3

首先,你需要启用的MySql服务器2远程访问。

然后,你可以简单地这样做:

mysql_connect("xxx.xxx.xxx.xxx", "username", "password") or die(mysql_error()); 
+0

我觉得口是在MySQL的情况下,必须数据库如果没有错误! –

+1

如果未指定,PHP会连接到默认端口(3306)。但你是对的:总是指定端口。 – HBv6

0

这取决于你如何要访问Server2上的数据库。

假设你只是想通过mysql客户端来连接试试这个:

mysql -h <server2ip or hostname> -u <username> -p 

在提示符下输入密码。

如果你想这个通过PHP尝试这样的事情,随着IP从服务器2和用户名和密码从MySQL服务器2的值替换SERVER_IP:

<?php 
$link = mysql_connect('server_ip', 'user', 'password'); 
if (!$link) { 
die('Error connecting to db: ' . mysql_error()); 
} 
echo 'Successful conntected to database'; 
mysql_close($link); 
?> 
相关问题