Local Theme Development on WordPress MultiSite

So this blog runs as part of my WP Multi Site network. Multi-site is great – one click to upgrade the WP core for all sites, one setup for caching etc.

One problem is local dev though. You don’t want to pull the whole network down to your local machine to work on a theme (it’d take ages) but you still want to develop against the posts and pages that are on the live site.

Interesting solution to this:

WP Multi-site stores DB tables with different prefixes for each site. So for instance you’d have wp_2_postswp_2_options etc. for site No. 2.

Let’s try altering a few bits in wp-config.php to make use of this:

define('WP_HOME', 'http://localsite.loc');
define('WP_SITEURL', 'http://localsite.loc');
define('DB_NAME', 'your_multisite_db');
define('DB_USER', 'your_multisite_user');
define('DB_PASSWORD', 'yourpw');
define('DB_HOST', '127.0.0.1:3307');
$table_prefix = 'wp_2_';

Finally create an SSH tunnel so you can access the DB running on your server on local port 3307 (Your local MySQL will be running on 3306). …..presuming you need to do this of course (article all about this here):

ssh -L 127.0.0.1:3307:127.0.0.1:3306 user@yourserver -N

That done, you should be good to go! One important caveat with this method: wp_users and wp_usermeta tables are global to all sites in the Multisite network (along with a few others). Therefore you won’t be able to login on your local copy and anything involving users on the front end will be screwed (like displaying post authors for instance). This really wasn’t much of a problem for my purposes on this site though. …so I thought I’d share.

Hope this helps someone!


Creating MySQL Users

So you’re setting up a new LAMP site and you need a database. It’s not very secure to just use the root user, so you’ll be needing this:

[bash]

mysql -u root -p
create database my_new_db;
CREATE USER ‘newuser’@’localhost’ IDENTIFIED BY ‘password’;
GRANT ALL PRIVILEGES ON my_new_db.* TO ‘newuser’@’localhost’;
FLUSH PRIVILEGES;
exit

[/bash]

Sorted!


Logging MySQL Queries in Ubuntu

It can be a really handy debugging tool to see exactly what the database is doing sometimes. To do that on Ubuntu 12.04 Server and probably a lot of other linux distros, do this:

Open /etc/mysql/my.cnf

Find these lines:

[bash]# general_log_file = /var/log/mysql/mysql.log
# general_log = 1[/bash]

and uncomment them. Save it and restart mysql:

[bash]sudo /etc/init.d/mysql restart[/bash]

Now tail the log file (you may need root permissions for this):

[bash]sudo tail -f /var/log/mysql/mysql.log[/bash]

Remember not to do this on a production server, as it will majorly slow things down and create some huge log files! Not the dream.