Webb Nollan
Webb Nollan

Reputation: 13

WordPress Different DataBases

Is it possible to use two different databases on WordPress depending the environment where the WordPress is? Like for example on my testing environment it uses a DataBase that is not Localhost eventhough the testing environment is Localhost, and on my deployed environment it uses a different DataBase without me having to change anything? Whenever I finish working on my LocalHost I upload the files through FTP but I need to change the DataBase information everytime.

Upvotes: 1

Views: 354

Answers (3)

rodrigo-silveira
rodrigo-silveira

Reputation: 13068

I do a lot of wordpress development (things like themes and plug-ins). I usually install a local copy of wordpress and work off of that. Then once the plug-in or theme is complete, I just export and deploy the specific files to that. There's no need/reason to move all of the files associated with wordpress (including those carrying database information: wp-config.php).

However, in a direct answer to your question, I supposed if that's what you want to do, you could modify the wp-config.php file so that the value of the variables that identify the database depend on your environment. That would be something like this:

///////////////////////////////////////
// Inside wp-config.php
///////////////////////////////////////

// Get as fancy as you need to in determining your environment
if($_SERVER['HTTP_HOST'] == 'localhost') {
    define('DB_NAME', 'my_local_database');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');
}
else {
    define('DB_NAME', 'my_other_database');
    define('DB_USER', 'username');
    define('DB_PASSWORD', 'password');
    define('DB_HOST', 'path_to_other_host');
}

This might not be a good idea, though, because when you update your wordpress that file could be overwritten. Maybe, who knows. But like I said, you'd be better off just moving the files that are specific to your project (like a custom theme or plugin that you work on on your development environment).

Hope that helps.

Upvotes: 1

Wes Crow
Wes Crow

Reputation: 2967

Sure. Set up the database in the wp-config.php file for each environment. See: http://codex.wordpress.org/Editing_wp-config.php#Configure_Database_Settings

Upvotes: 1

Michael
Michael

Reputation: 53

You have to find the database configuration file. where it stores all your database information like user name, password. mark this file read only.

Upvotes: 0

Related Questions