简体   繁体   中英

Connecting to a database from local server

I operate a large PHP website with some pages connecting to to a MySQL database, which works fine. I have now created a local test server and have downloaded my PHP pages and my database to wamp server where the pages the don't require a database connection work fine. However, on the pages that use the database, mysql_connect() requests throw an error:

Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

I can get a connection working by changing the MySQL request on the PHP page to:

$server="localhost";
$user="root";
$password="";

leaving $database="xxxx"; (original name)

This solution would mean I have to change every page with a MySQL request, and then change them back once development is complete.

Is there a way to get the original MySQL connect requests to work while keeping the original security data on the PHP pages? For example, by changing the wamp server config.inc.php file?

I've tried a number of combinations in the config.inc.php file but none seem to work.

As I sayed in my comment, if you have the mysql connection credentials hardcoded in every file, you only have two alternatives:

  1. Replace the credentials in every file (with a massive search and replace)
  2. Put the same credentials in your local server. If the code try to connect to a remote host, you can put that host in your host file pointing to your localhost. This way you can simulate the remote environment.

A good solution would be to save these informations in a file that you include in all your other pages that need a mysql connection.

database.php

$server="localhost";
$user="root";
$password="";
$database = "nameofyourdb"

// Might event do the mysql_connect here

index.php ou any other page

include_once('database.php');
// your code
// containing mysql_query() and so on

Then you just need to change informations in one file when yo go from local to production.

Might even use a trick like this to avoid the change:

if ($_SERVER['SERVER_ADDR'] == '127.0.0.1') {
    // local
    $server="localhost";
    $user="root";
    $password="";
    $database = "nameofyourdb";
}
else {
    // production
    $server="localhost";
    $user="...";
    $password="...";
    $database = "...";
}

Be careful using the mysql API which is deprecated. You should move to PDO for example.

As previous answers have stated, you should simply move your connection parameters into a non-human readable file (ie outside your webserver root directory, if possible). This way you can store them securely and not risk outside access.

Then, include your file into each page where it is needed. Examples:

config.inc.php:

<?php
$db['host'] = '127.0.0.1';
$db['user'] = 'user';
$db['pw'] = 'passwd';
$db['name'] = 'dbname';

You could even get creative in this file:

<?php
// Production settings
$db['host'] = '127.0.0.1';
$db['user'] = 'user';
$db['pw'] = 'passwd';
$db['name'] = 'dbname';

// Test/Sandbox settings
/* 
$db['host'] = '127.0.0.1';
$db['user'] = 'user';
$db['pw'] = 'passwd';
$db['name'] = 'dbname2';
*/

Then simply switch the comments everytime you switch environments.

Then, just include this file into pages where you need to connect to your database:

index.php:

<html><head><tltle>My page</title>
...HTML Code here...
<?php include_once("/path/to/config.inc.php"); // Again, store this file outside the web directory, if possible ?>
<?php mysqli_connect($db['host'],$db['user'],$db['pw'],$db['name']); ?>

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