简体   繁体   中英

I want to connect one server database to from another server

I have two server named server1 and server2.both having diffrent Static ip address.I want to access server2 database from server1.Both server i have installed PHPmyadmin.In Server1 Operating system is Ubuntu, in server2 fedora12.

I have done this..mysql error 13 is coming

in server2 my.cnf contains

[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"

First you need to enable Remote Access on MySql Server2 .

Then you can simply do that:

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

If your problem is the connection to a remote mysql database, then you could try the code below:

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

Worked for me once!

try creating a connect_server2.php file on server1 with the following contents:

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

It depends how you want to access the database on server2.

Assuming you just want to connect via mysql client try this:

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

Enter password at prompt.

If you want this via php try something like this, replace server_ip with ip from server2 and username and password with values from the mysql server2:

<?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);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM