Installing Multiple MySQL
The need to have multiple instances of MySQL (the well-known mysqld process) running in the same server concurrently in a transparent way, instead of having them executed in separate containers/virtual machines.
MySQL provides a tool to facilitate the execution of multiple instances called mysqld_multi
“mysqld_multi is designed to manage several mysqld processes that listen for connections on different Unix socket files and TCP/IP ports. It can start or stop servers, or report their current status”
Understanding the concept of groups in my.cnf
You may have noticed already that MySQL’s main configuration file (or “option file”), my.cnf, is arranged under what is called group structures: Sections defining configuration options specific to a given program or purpose. Usually, the program itself gives name to the group, which appears enclosed by brackets. Here’s a basic my.cnf showing two such groups:
1 2 3 4 5 6 7 8 9 10 11 12 |
[client] port = 3306 socket = /var/run/mysqld/mysqld.sock user = root password = root [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 datadir = /var/lib/mysql |
The options defined in the group [client] above are used by the mysql command-line tool. As such, if you don’t specify any other option when executing mysql it will attempt to connect to the local MySQL server through the socket in /var/run/mysqld/mysqld.sock and using the credentials stated in that group. Similarly, mysqld will look for the options defined under its section at startup.
Getting started with multiple instances:
To have multiple instances of MySQL running we must replace the [mysqld] group in the my.cnf configuration file by as many [mysqlN] groups as we want instances running, with “N” being a positive integer, also called option group number. This number is used by mysqld_multi to identify each instance, so it must be unique across the server. Apart from the distinct group name, the same options that are valid for [mysqld] applies on [mysqldN] groups, the difference being that while stating them is optional for [mysqld] (it’s possible to start MySQL with an empty my.cnf as default values are used if not explicitly provided) some of them (like socket, port, pid-file, and datadir) are mandatory when defining multiple instances – so they don’t step on each other’s feet. Here’s a simple modified my.cnf showing the original [mysqld] group plus one other instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[mysqld1] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 datadir = /var/lib/mysql log_error = /var/log/mysq/error.log innodb_buffer_pool_size = 8M server-id = 101 [mysqld2] user = mysql pid-file = /var/run/mysqld/mysqld1.pid socket = /var/run/mysqld/mysqld1.sock port = 3307 datadir = /var/lib/mysql1 log_error = /var/log/mysql/error1.log innodb_buffer_pool_size = 8M server-id = 102 |
Besides using different pid files, ports and sockets for the new instances I’ve also defined a different datadir for each – it’s very important that the instances do not share the same datadir.
Run mysql_install_db :
1 |
mysql_install_db --user=mysql --datadir=/var/lib/mysql1 |
Note that if /var/lib/mysql1 doesn’t exist and you start this instance anyway then myqld_multi will call mysqld_install_db itself to have the datadir created and the system tables installed inside it.
Two commands you might find useful when configuring multiple instances are:
1 |
mysqld_multi --example |
Which provides an example of a my.cnf file configured with multiple instances and showing the use of different options
1 |
my_print_defaults --defaults-file=/etc/mysql/my.cnf mysqld1 |
Which shows how a given group (“mysqld1” in the example above) was defined within my.cnf
Commaonds to start/stop/check mysql multiple instances
Check status for all mysql processes
1 |
mysqld_multi report |
Start Instance
1 2 |
mysqld_multi start 1 mysqld_multi start 2 |
Stop Instances
1 2 |
mysqld_multi stop 1 mysqld_multi stop 2 |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
Ashish Arjariya
Very well explained step by step..
sai
Nice info