2012-06-12 279 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); 
?> 
相關問題