Connecting remote server mysql database to localhost
How can I connect my localhost
PHP
files to my remote server MYSQL
database?
For example: if we download WordPress
,开发者_StackOverflow社区 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.
精彩评论