Howto run two instances of MySQL in the same server

Posted by HostsVault | Posted in How-To's | Posted on 02-06-2009-05-2008

2

One of our users demanded 2 mysql instances on his dedicated server as he had his  mysql hacked before on his previous host so he wanted to separate his own databases in a completely different directory and even using another MySQL process to assure physical separation I will show you here how to accomplish this

First create another mysql directory :

mkdir /var/lib/mysql2
chown mysql.mysql /var/lib/mysql2/

Then make a copy of your current my.cnf :

cp /etc/my.cnf /etc/my2.cnf

Next edit/add this inside your my2.cnf :

[mysqld]
pid-file = /var/lib//mysql2/mysql2.pid
socket = /var/lib/mysql2/mysql2.sock
port = 3300 # (or any other random port of your choice)
datadir = /var/lib/mysql2
log = /var/log/mysql2.log

Then you will have to initialize MySQL on your new datadir :

mysql_install_db --user=mysql --datadir=/var/lib/mysql2/

Now we are ready to start this instance

mysqld_safe --defaults-file=/etc/my2.cnf&

The reason of adding & in the end is to make this command run in the background and bring  you back to your normal bash shell.

But hey now how can I connect to this new instance ,its easy just use this command :

mysql -h localhost --port=3300

If you need to use mysqldump use this syntax :

mysqldump -s=/var/lib/mysql2/mysql2.sock dbase_name > db.sql

for an added layer of security you can disable remote MySQL access by adding this under [mysqld] section in your my2.cnf :

skip-networking
VN:F [1.9.3_1094]
Rating: 5.5/10 (396 votes cast)
VN:F [1.9.3_1094]
Rating: +5 (from 97 votes)
Howto run two instances of MySQL in the same server, 5.5 out of 10 based on 396 ratings
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Furl
  • Slashdot
  • StumbleUpon
  • Technorati

Comments posted (2)

When i connect to the new instance using
mysql -h localhost –port=3300

command it is connected wtih the old port 3306 and shows all the database in first instance.

How do i connect to the new instance ?

Thanks in advance.

Regards
Jeetendra Ranjan

VA:F [1.9.3_1094]
Rating: 3.0/5 (1 vote cast)
VA:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Really a good post.

Will revert back once i test it.

VA:F [1.9.3_1094]
Rating: 3.7/5 (3 votes cast)
VA:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Write a comment