Connecting remote server mysql database to localhost - php

How can I connect my localhost PHP files to my remote server MYSQL database?
For example: if we download WordPress, and it gives us an option to login, so that our login credentials are saved with WordPress but still our localhost files connects to that database.
How do I do that?
Thanks.

If you want your PHP code to connect to a remote MySQL database, you just have to specify that remote host when calling the function that connects to MySQL.
For example, when calling mysql_connect(), don't specify localhost as first parameter -- but your real remote host.
Same with mysqli::__construct() or PDO, of course.
A couple of notes :
Your remote host must accept connections to MySQL (think about the firewall)
Your remote MySQL database server must accept connections from non-localhost
Sending queries, and fetching results, to/from a far away server will be a lot slower than doing so on localhost !

mysql_connect(SQL_IP, SQL_NAME, SQL_PASS);

Well when you install Wordpress set the host to be the IP of your remote server. If this isn't what you're looking for, please reword your question. If you're not using word press, check ou the PHP documentation for mysql_connect. The first parameter is the host. Enter the IP or hostname of your remote server here. And then follow that with your username and password.
mysql_connect('remote-host', 'myuser', 'mypassword');
Note: Some hosts do not allow remote connections to MySQL. Check your remote server doesn't have 3306 firewalled or only instructed MySQL to bind it to 127.0.0.1.

Related

retrieve data from mysql database using php from a different computer

I am a newbie on PHP . Can someone tell me how to connect and retrieve data from mysql database server using PHP from a different computer.
First of all, the MySQL server must accept remote connections.
Also MySQL's user that you use, must be allowed to log from PHP computer's IP.
Then you can simply connect from your PHP server changing configuration of connection by setting host parameter to MySQL server's IP instead of localhost.
Google for mysql remote connection in order to find information about how to configure it to accept remote connections.
Localhost in name suggest locally , you cannot access mysql database from another computer .
Copy the file and install , only then it will work.

Can not connect to my mysql database

I have been recently using "localhost" as my mysql database, and now i want to update my website to web.
I took free hosting at "serversfree.com" and uploaded all my web files there.Now i have problem.I made mysql database on that website, and uploaded my mysql database from localhost.
I can go to that server and list all files i have in that mysql database, but when i want to connect my .php documents i don't know how.What to put under "hostname" (i was recently using only localhost).Do i need to put a link where my web mysql database is?
Or i need to put a link where my website is?
I tried both but didn't worked for me
Most web hosts I've used have MySQL running on the same box so I usually can keep localhost as the server.
Your host should have some documentation on where to point the server to.
Check if your database credentials, are from a valid user in that database..
What hostname did you use to upload your mysql database from localhost? It would be the same hostname. Probably mysql.serversfree.com. If their mysql server is on the same subdomain then you can use localhost as well. To connect your php scripts to a mysql server you need to use the hostname of the mysql server, not the hostname of your http server.
If your mysql is running in the same server then you can use localhost or 127.0.0.1 but if mysql is in another server probably you need remote connections

How to connect to WordPress database remotely?

I have a WordPress website, which is not done by me, all I need to do is connect to its database remotely and query some results from its database. The point is that I don't know anything about WordPress, and I saw that the database information are written inside the wp-config.php file, when I opened it, there was the username and password, but the host name was this:
define('DB_HOST', 'localhost');
Which seems a bit curious to me. The website uses cPanel X from GoDaddy, and when I logged in there and opened phpMyAdmin, it had these information under database server part:
Server: Localhost via UNIX socket
So, I really don't know how to remotely connect to the database and query things, because host name says localhost. I'm used to ASP.NET MVC, and where I specify the host name to be the actual server ip or hostname of the database server. Any idea how should I proceed?
This is probably a better question for superuser as it's technically a hosting related question. That being said, you would need to contact GoDaddy and find out if they even allow remote MySQL connections on their servers. Most hosting companies disable external connections to the MySQL server on port 3306, but if you portscan your domain name/IP and see 3306 open then you can probably remote connect to it. Just use the domain name/IP of your site as the host, and pull the user/pass/db name from the config file.
You can do this by using command line.
GRANT ALL ON wpdb.* TO wpuser#`192.168.2.25` IDENTIFIED BY `Pa55w0rd!`;
Check here for a detailed tutorial.

Cannot connect to mysql database on remote host

I am not sure if the terms I am using are correct so ask for clarification if you need :).
Anyways, I am hosting through HostEasySolutions (Server A). It comes with a MySql database, using PHPMyAdmin as the frontend. On Server A, I added some PHP files to access the database, and it works fine.
If I copy the php files to my other server, through DreamHost (Server B), I cannot connect to the database. I get the error: Can't connect to MySQL server on 'combinedsystems.ca' (110)
In the cPanel on Server A, I added the IP for Server B into the Remote Database Access Hosts, I also just added the wildcard '%', just to see if I could get it working...but still it can't find the server.
I am not sure what is going on, as far as I can see there is no firewall.
The only thing that stands out to me, is that if I go to PHPmyAdmin for Server A, it says Server: Localhost via UNIX socket, where as for Server B's PHPMyAdmin, it says: Server: mysql.pdem.info via TCP/IP
Most (all?) hosting companies have port 3306 closed on the firewall.
What you need to do:
adding remote database access in the database
edit my.cnf to listen on all interfaces. Default mysql listens only to localhost
most of the time you are out of luck! Even if there is no firewall (would be very unsecure) you have no access to my.cnf

Host my wordpress site locally using the server database

I have set up my wordpress site on my local machine and I would like it to talk to the live mysql database on the server. I accessed the wp-config.php file on my machine and changed the hostname to use the ip address instead of localhost, but it will not work.
What do I need to do?
It could be that your remote database is configured to accept connections only from localhost for security reasons. Most web providers set it up that way. In that case, you have no chance of making this work.
Anyway, even if you would get it to work, you will encounter two problems: It will be awfully slow, and the HTML served by the remote database will contain references to server URLs (as opposed to local ones).
If you need more detailed information, you will need to post any error messages you get from mySQL.
Alternatively, you can try running the mysql command-line utility to connect.
mysql -u username -h server -p wordpress_database
Make sure that works first before attempting to get Wordpress to connect.
My guess is you won't be able to connect due to firewall issues. MySql uses port 3306 by default, so if the server's firewall doesn't allow connections through that port, you'll either have to change the port mysql is using (probably a bad idea if other apps expect to see MySql on that port) or get that port opened on the server (make sure you only open it for local IPs, so someone else can't get to your MySql instance!)

Categories