Running multiple instances of MySQL on the same machine
There are various methods to run multiple instances of mysql (on different ports) on the same machine. We can either compile the mysql binary with different defaults and paths, use mysqld_multi or the MySQL Sandbox project. Still the simplest solution I’veĀ used in the past for such situations is to use the same binary and use a separate configuration file (with separate port, pid, socket and data directory). This post will explain this method.
1. Create separate data and log directories
We need to create new directories for our datadir and log folder (if used). Also we need to assign proper permissions on those folders:
mkdir /var/lib/mysql2
chown -R mysql.mysql /var/lib/mysql2/
mkdir /var/log/mysql2
chown -R mysql.mysql /var/log/mysql2
2. Create a new mysql configuration file
Next we need a separate configuration file. We can start by copying the existing one and changing the needed values. This example was taken on a debian machine that holds the mysql configurations under /etc/mysql/my.cnf. We just copy this folder and modify it from there:
cp -R /etc/mysql/ /etc/mysql2
if you use a redhat variant package then your configuration file is under /etc/my.cnf by default and you can just copy it directly:
cp /etc/my.cnf /etc/my2.cnf
(or change the path appropriately for your configuration file is in a different place).
Next, we need to edit our new configuration file and at least update the mysql port (default to 3306), the pid and socket to be different than the default ones, and also point the data and log folders to the ones created before. On my debian machine this can be done with something like this:
cd /etc/mysql2/
sed -i 's/3306/3307/g' my.cnf
sed -i 's/mysqld.sock/mysqld2.sock/g' my.cnf
sed -i 's/mysqld.pid/mysqld2.pid/g' my.cnf
sed -i 's/var\/lib\/mysql/var\/lib\/mysql2/g' my.cnf
sed -i 's/var\/log\/mysql/var\/log\/mysql2/g' my.cnf
3. Initializing and starting
Finally we need to initialize the default dbs:
mysql_install_db --user=mysql --datadir=/var/lib/mysql2/
Alternatively we can copy the existing /var/lib/mysql if this is needed (shut down mysql prior to do this).
Finally we can start our new mysql instance with:
mysqld_safe --defaults-file=/etc/mysql2/my.cnf &
We can connect to our new instance using:
mysql -S /var/run/mysqld/mysqld2.sockor
mysql -h 127.0.0.1 -P 3307
and if we no longer need it, stop it with:
mysqladmin -S /var/run/mysqld/mysqld2.sock shutdown
Here is a script we have used to create many instances that we needed for testing. Feel free to use or change it to suit your needs (it is for debian machines), and you run it with new_mysql_instance.sh <ID>
#!/bin/bash
i=$1
port=$[3306+$i]
mkdir /var/lib/mysql$i
chown -R mysql.mysql /var/lib/mysql$i/
mkdir /var/log/mysql$i
chown -R mysql.mysql /var/log/mysql$i
cp -R /etc/mysql/ /etc/mysql$i
cd /etc/mysql$i/
sed -i "s/3306/$port/g" my.cnf
sed -i "s/mysqld.sock/mysqld$i.sock/g" my.cnf
sed -i "s/mysqld.pid/mysqld$i.pid/g" my.cnf
sed -i "s/var\/lib\/mysql/var\/lib\/mysql$i/g" my.cnf
sed -i "s/var\/log\/mysql/var\/log\/mysql$i/g" my.cnf
mysql_install_db --user=mysql --datadir=/var/lib/mysql$i/
mysqld_safe --defaults-file=/etc/mysql$i/my.cnf &>
Tags: mysql, tips, Tips & Tricks







19th January 2009, 15:59
Why not use mysqlmanager?
19th January 2009, 16:21
@rusty_angel: I was not aware of this solution. Thank you for your addition.
19th January 2009, 16:26
I once spent two days writing my own scripts for that (with almost no knowlege of bash), before I found mysqlmanager
2nd February 2009, 19:42
Once you have multiple mysql instances running, how do you do a mysqldump on a particular instance?
Is it mysqldump -s=/var/mysql/mysql2.sock to dump from the 2nd instance and mysqldump -s=/var/mysql/mysql1.sock to dump from the 1st instance?
3rd February 2009, 15:07
@Jake: yes, if you run it locally you can use the
--socket=pathor-S pathparameter. While remotely just use the host/port where the respective server is listening.2nd April 2009, 18:54
mysqlmanager has been deprecated, so if you’d like to upgrade your mysql someday, you don’t want to use it.
26th October 2009, 18:29
If anyone is trying this and receive this result when installing the 2nd database:
ERROR: 1005 Can’t create table ‘db’ (errno: 13)
although all the permissions are correct … take a look into your /var/log/syslog … it is possible, that apparmor is installed and blocks any r/w access from mysqld outside its default directories …
You have to update the apparmor configuration /etc/apparmor.d/usr.sbin.mysqld – and restart apparmor!
regards
28th October 2009, 00:18
[...] Creating a 2nd MySQL Instance Very simple and straightforward. Follow these instructions on ducea.com. [...]
29th October 2009, 23:08
[...] Running multiple instances of MySQL on the same machine | MDLog:/sysadmin This entry was written by delicious and posted on October 29, 2009 at 3:08 pm and filed under links. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL. [...]
11th December 2009, 23:30
I took this a little further by integrating it with the debian init.d script, which includes some maintenance tasks done under the mysql user debian-sys-maint. I basically pulled out the parts of the dpkg .postinst script that set this up. You can get the debian-sys-maint from /etc/mysql/debian.cnf. Here is a summary of what I did (I called my second instance mysql.replica instead of mysql2):
– sudo mkdir /var/lib/mysql.replica
sudo chown mysql.mysql /var/lib/mysql.replica
– cp -r /etc/mysql /etc/mysql.replica
edit paths, port (3307)
– cp /etc/init.d/mysql /etc/init.d/mysql.replica
edit paths
add –defaults-file=/etc/mysql.replica/my.cnf to mysqld_safe
add –defaults-file=/etc/mysql.replica/my.cnf to mysqld
– mysql_install_db –user=mysql –datadir=/var/lib/mysql.replica
– /etc/init.d/mysql.replica start
– mysql –defaults-file=/etc/mysql.replica/my.cnf -u root
ALTER TABLE user CHANGE password Password varchar(41) collate utf8_bin NOT NULL default ”;
UPDATE user SET password=PASSWORD(“”) WHERE user=’root’;
FLUSH PRIVILEGES;
REPLACE INTO user SET
host=’localhost’, user=’debian-sys-maint’, password=password(”),
Select_priv=’Y', Insert_priv=’Y', Update_priv=’Y', Delete_priv=’Y',
Create_priv=’Y', Drop_priv=’Y', Reload_priv=’Y', Shutdown_priv=’Y',
Process_priv=’Y', File_priv=’Y', Grant_priv=’Y', References_priv=’Y',
Index_priv=’Y', Alter_priv=’Y', Show_db_priv=’Y', Super_priv=’Y',
Create_tmp_table_priv=’Y', Lock_tables_priv=’Y', Execute_priv=’Y',
Repl_slave_priv=’Y', Repl_client_priv=’Y';
– update-rc.d mysql.replica defaults 19 21
The second instance can now be started and stopped (and will start automatically at boot) just like the primary instance.
16th December 2009, 00:02
@Andrew: thank you for your addition to the post.
21st February 2010, 12:26
How to setup a replication server on same hos running to instances of mysql?
any idea?
22nd February 2010, 16:28
@d.bilbao: it should work the same as on different machines. Just be sure to use the proper MASTER_PORT if needed.