Use mysqld_multi in Linux to manage multiple MySQL servers

Source: Internet
Author: User
Tags unix domain socket

When multiple MySQL servers need to be run on a machine, mysqld_multi is a great tool. It can be started, shut down, and reported by using simple commands.

The configuration file below is very concise, just for testing purposes, so make a backup of the original "my. cnf" before the experiment, it will make people feel at ease. The configuration file is as follows:
[Client]
# Port = 3306
# Socket =/var/run/mysqld. sock
User = root
Password = dandan
[Mysqld_multi]
# Mysqld =/usr/bin/mysqld_safe
# Mysqladmin =/usr/bin/mysqladmin
# User = root
# Password = dandan
[Mysqld1]
Port = 3306
Socket =/var/run/mysqld/mysql. sock1
Pid-file =/var/run/mysqld/db-app1.pid
# Log =/usr/bin/mysql/data1/db-app.log
Datadir =/var/lib/mysql/
User = mysql
[Mysqld2]
Port = 3307
Socket =/var/run/mysqld/mysql. sock2
Pid-file =/var/run/mysqld/db-app2.pid
Datadir =/var/lib/mysql/mysql2
# Log =/usr/bin/mysql/data2/db-app.log
User = mysql
[MysqlGNR], the GNR of each server is different. It can be a number greater than or equal to 0. Each server listens to different ports and has its own Unix Domain socket. Note that the data file directory is www. adbrite. each server of cc must have its own data directory. MySQL is installed under Ubuntu. The default data directory is under the "/var/lib/mysql" path, and there is a mysql database under this path, it stores the configuration data of the database, and every server needs this database. Therefore, create a directory named mysql2 under this directory and then "cp-R mysql mysql2 ".
First look at the var/run/mysqld/directory
Root @ ecy-geek:/var/run/mysqld # ls/var/run/mysqld/
Enable two servers
Root @ ecy-geek:/var/run/mysqld # mysqld_multi start 1, 2
Let's look at the var/run/mysqld/directory.
Root @ ecy-geek:/var/run/mysqld # ls/var/run/mysqld/
Db-app1.pid db-app2.pid mysql. sock1 mysql. sock2
Report the status of the server. It can be seen that both servers are running.
Root @ ecy-geek:/var/run/mysqld # mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
Disable Server 1
Root @ ecy-geek:/var/run/mysqld # mysqld_multi stop 1;
Root @ ecy-geek:/var/run/mysqld # ls/var/run/mysqld/
Db-app2.pid mysql. sock2
Report the status again. The server 1 is closed.
Root @ ecy-geek:/var/run/mysqld # mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is running
Try Client Connection
Root @ ecy-geek:/var/run/mysqld # mysql-S/var/run/mysqld/mysql. sock2
Welcome to the MySQL monitor. Commands end with; or \ g.
Your MySQL connection id is 4
Server version: 5.1.31-1ubuntu2 (Ubuntu)
Type 'help; 'or' \ H' for help. Type '\ C' to clear the buffer.
Mysql>
Root @ ecy-geek:/var/run/mysqld # mysql-P3307
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld. sock' (2)

It can be seen that it is normal to use Unix Domain to connect to the server, but the socket is not connected. It seems that the mysql client can understand that the same server is on a host, therefore, it stubbornly uses Unix Domain to connect to the server regardless of user instructions. The above is just a simple demonstration. Generally, some mysqld_multi options exist in the [mysqld_multi] combination. We recommend that you use mysqld_safe to start the server. I am confused by the options in it, so I didn't add the [mysqld_multi] group to the configuration file. I will try again later.

Use "service mysql start" to enable the mysql server. The mysqld_safe script is also used by default. In Linux, many programs are started using shell scripts. bash will start a new process running script, and the program to run this script will become a sub-process of this process, as long as the child process does not exit, the parent process will continue to run. If "kill-9" is used to kill the parent process, the child process becomes an orphan process and is adopted by the init process. The Linux qq STARTUP script is very simple, as follows:

Root @ ecy-geek:/home/ecy/C # which qq
/Usr/bin/qq
Root @ ecy-geek:/home/ecy/C # file/usr/bin/qq
/Usr/bin/qq: POSIX shell script text executable
Root @ ecy-geek:/home/ecy/C # cat/usr/bin/qq
#! /Bin/sh
Cd/usr/share/tencent/qq/
./Qq
Root @ ecy-geek:/home/ecy/C # ps-ef | grep qq
Root 2658 1278 0 00:00:00 pts/1 grep qq
Ecy 6187 1 0 11: 41? 00:00:00/bin/sh/usr/bin/qq
Ecy 6188 6187 0? 00:00:04./qq
You can see that the parent process of the process running the/usr/bin/qq script is the init process, and qq started from the graphic interface is like this, directly input/usr/bin/qq from the shell to start qq. The parent process of the script process is the bash process that runs the qq.
Root @ ecy-geek:/home/ecy/C # ps-ef | grep mysql
Root 1974 1278 0 00:00:00 pts/1 grep mysql
Root @ ecy-geek:/home/ecy/C # service mysql start
* Starting MySQL database server mysqld [OK]
* Checking for login upt, not cleanly closed and upgrade needing tables.
Root @ ecy-geek:/home/ecy/C # ps-ef | grep mysql
Root 2027 1 0 00:00:00 pts/1/bin/sh/usr/bin/mysqld_safe
Mysql 2075 2027 1 00:00:00 pts/1/usr/sbin/mysqld -- basedir =/usr -- datadir =/var/lib/mysql -- user = mysql -- log-error =/var /lib/mysql/ecy-geek.err -- pid-file =/var/lib/mysql/ecy-geek.pid
Root 2198 1278 0 00:00:00 pts/1 grep mysql
The mysqld_safe script is very complicated. I am also reviewing shell for a while. When can I analyze the mysqld_safe script? ^_^

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.