简体   繁体   中英

Working with two databases from different domains within a same server

I have two domains.

In domain A, I have a PHP web site and in domain B, I have Java Website.

Now I want to use both database from both site.
How can I do that? I am using MySql 5.7.17

You could set up a third server dedicated to mySQL and share that database with the 2 web servers, or you can just run a mySQL database on each of those computers and use the IP address or domain name of them to connect to each other's server

Explanation:

You currently have a java website running and a php website running on one machine if i understand correctly.

What you need to do is download mySQL, and go through the tutorial to get the server up and running https://dev.mysql.com/doc/mysql-getting-started/en/

Then you need to download the mysql Java driver, and the mysql PHP driver

Then you need to use the drivers to connect to the same mySQL server from the code, which would be on localhost if you are doing it all on one machine.

MySQL is a server. It is its own thing, not a database like SQLite that you create in the java code or php code. You create and set it up separately as it's own server, and then any other server can connect to it

You can open and close a mysql connection any time.

mysql_connect(server, username, password);

Do some stuff with one database, then you can use:

mysql_close();
mysql_connect(server2, username2, password2);

as many times as you like.

Or you can use resource links:

$db1 = mysql_connect(server1, user1, passwd1);
$db2 = mysql_connect(server2, user2, passwd2);

But in this case, each mysql command needs to be referenced with the resource link:

mysql_query(query, $db1);

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