There are plenty instruction on how to run multiple mysql instances on a single server on various blogs. Most of them are based on the original mysql rpm package produced by Oracle (nowdays). In my opinion this could be unnecessary overhead when you have OS provided mysql server package and all you need to configure another instance is to slightly modify bundled configuration and init.d script.
To separate new instance from default mysql server configuration we will use
- server port 3307
- pid file /var/run/mysqld/mysql2.pid
- server config file /etc/my2.cnf
- Data dir (for all database file storage) /home/mysql2
- Server log file /var/log/mysqld2.log
- Socket file /home/mysql2/mysql.sock
- First we need to make /etc/my2.cnf –
here is original minimal /etc/my.cnf1234567891011121314[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Default to using old password format for compatibility with mysql 3.x# clients (those using the mysqlclient10 compatibility package).[mysql.server]user=mysqlbasedir=/var/lib[mysqld_safe]err-log=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidAnd here modified /etc/my2.cnf
123456789101112131415[mysqld]datadir=/home/mysql2socket=/home/mysql2/mysql.sockport=3307user=mysql# Default to using old password format for compatibility with mysql 3.x# clients (those using the mysqlclient10 compatibility package).[mysql.server]user=mysqlbasedir=/var/lib[mysqld_safe]err-log=/var/log/mysqld2.logpid-file=/var/run/mysqld/mysqld2.pidOr as patch file
1234567891011121314151617181920--- /etc/my2.cnf.old 2013-11-15 09:30:18.149241693 +0200+++ /etc/my2.cnf 2013-11-15 09:30:51.767247315 +0200@@ -1,6 +1,7 @@[mysqld]-datadir=/var/lib/mysql-socket=/var/lib/mysql/mysql.sock+datadir=/home/mysql2+socket=/home/mysql2/mysql.sock+port=3307user=mysql# Default to using old password format for compatibility with mysql 3.x# clients (those using the mysqlclient10 compatibility package).@@ -13,7 +14,7 @@ basedir=/var/lib[mysqld_safe]-err-log=/var/log/mysqld.log-pid-file=/var/run/mysqld/mysqld.pid+err-log=/var/log/mysqld2.log+pid-file=/var/run/mysqld/mysqld2.pidYou can download patch file and apply it
123cd /etccp my.cnf my2.cnfpatch -b --verbose < ~/my2.cnf.patch - Next step – create modified startup script, that would read proper configuration file, extract port number from it (by default port number is not passed) and invoke mysqld process with set parameters. Here is the patch:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354--- /etc/init.d/mysqld2.old 2013-11-15 12:59:31.561236256 +0200+++ /etc/init.d/mysqld2 2013-11-15 13:54:08.047233073 +0200@@ -5,9 +5,9 @@## chkconfig: - 64 36# description: MySQL database server.-# processname: mysqld-# config: /etc/my.cnf-# pidfile: /var/run/mysqld/mysqld.pid+# processname: mysqld2+# config: /etc/my2.cnf+# pidfile: /var/run/mysqld/mysqld2.pid# Source function library.. /etc/rc.d/init.d/functions@@ -15,9 +15,11 @@# Source networking configuration.. /etc/sysconfig/network+mycnf="/etc/my2.cnf"+exec="/usr/bin/mysqld_safe"-prog="mysqld"+prog="mysqld2"# Set timeouts here so they can be overridden from /etc/sysconfig/mysqldSTARTTIMEOUT=120@@ -34,7 +36,7 @@ lockfile=/var/lock/subsys/$prog# We use my_print_defaults which prints all options from multiple files,# with the more specific ones later; hence take the last match.get_mysql_option(){- result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`+ result=`/usr/bin/my_print_defaults -c $mycnf "$1" | sed -n "s/^--$2=//p" | tail -n 1`if [ -z "$result" ]; then# not found, use defaultresult="$3"@@ -45,6 +47,8 @@ get_mysql_option mysqld datadir "/var/lidatadir="$result"get_mysql_option mysqld socket "$datadir/mysql.sock"socketfile="$result"+get_mysql_option mysqld port "3306"+port="$result"get_mysql_option mysqld_safe log-error "/var/log/mysqld.log"errlogfile="$result"get_mysql_option mysqld_safe pid-file "/var/run/mysqld/mysqld.pid"@@ -98,6 +102,7 @@ start(){# alarms, per bug #547485$exec --datadir="$datadir" --socket="$socketfile" \--pid-file="$mypidfile" \+ --port="$port" \--basedir=/usr --user=mysql >/dev/null 2>&1 &safe_pid=$!# Spin for a maximum of N seconds waiting for the server to come up;
Download the patch to ~/mysqld2.patch and apply
123cd /etc/init.d/cp mysqld mysql2patch --verbose < ~/mysqld2.patch - Now after everything configured we can start new instance and let it configure new data dir with service databases and permissions
1234567891011121314151617181920212223242526272829303132333435/etc/init.d/mysqld2 startInitializing MySQL database:Installing MySQL system tables...OKFilling help tables...OKTo start mysqld at boot time you have to copysupport-files/mysql.server to the right place for your systemPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !To do so, start the server, then issue the following commands:/usr/bin/mysqladmin -u root password 'new-password'/usr/bin/mysqladmin -u root -h centos password 'new-password'Alternatively you can run:/usr/bin/mysql_secure_installationwhich will also give you the option of removing the testdatabases and anonymous user created by default. This isstrongly recommended for production servers.See the manual for more instructions.You can start the MySQL daemon with:cd /usr ; /usr/bin/mysqld_safe &You can test the MySQL daemon with mysql-test-run.plcd /usr/mysql-test ; perl mysql-test-run.plPlease report any problems with the /usr/bin/mysqlbug script![ OK ]Starting mysqld: [ OK ]
Now you have fully functional second instance of mysqld server running with the only overhead of modified configuration files. You can create as many instances as you need the same way.
0 Comments.