For connecting to MySQL database hosted on the same server, we generally use ‘localhost’ as hostname. Apart from this, we need the username, password and name of the database we need to connect to.
Remote MySQL database means we have our database located at some distant location, not on the same place(host) where our website/app is. For connecting to such a database, we need to get its hostname, username and password.
After we have all this, here is how we make a connection to it using PHP,
1. Go to the (remote) server, and open database in phpMyAdmin.
2. Copy the hostname appearing at the top.
3. Get the database username and password. Its set at the time of database creation.
4. Once you have all this, make connection to this server from yours using the following PHP code,
<?PHP $con = mysql_connect( 'remote-db-hostname', 'remote-db-username', 'remote-db-password' ); $selectdb = mysql_select_db( 'remote MySQL database name', $con ); ?>
For WordPress site
If you wish to show posts from a remote MySQL database in your WordPress site, just replace the hostname, username, password and database name with remote server’s in wp-config.php file inside WordPress folder.
Once you do this, your WordPress site will start showing posts from that remotely located MySQL database.