Deploying multiple MySQL services on the same server can effectively improve machine utilization. In addition, it is safer to classify different content into different services.
MySQL provides mysqld_multi, but it is not used in actual applications. Disadvantages:
1. Suspending and restarting all services will stop.
2. The configuration file is lengthy, causing trouble in splitting and migration.
A common method in the industry is to put database data files and configuration files, and enable and disable scripts in a directory. When a service is started, it acts as a separate service. When it is disabled, it has no impact on other services. During the migration, you only need to migrate the directory to the target machine.
A typical example:
10.10.82.83 [server2] $ cat start. Sh
#! /Bin/sh
Rundir = "/opt/server2 ";
Echo $ rundir;
/Usr/local/MySQL/bin/mysqld_safe -- defaults-file = "$ rundir"/My. CNF -- PID-file = "$ rundir"/MySQL. PID -- datadir = "$ rundir"/var -- socket = "$ rundir"/MySQL. sock -- log-error = "$ rundir"/err. log -- log-Slow-queries = "$ rundir"/slow_query.log &
In the preceding example, MySQL is installed in/usr/local/MySQL according to official suggestions.
Sometimes you need to use multiple different versions on the same server, for example, test the new version. According to the above logic, install the new mysqld under/usr/local/MySQL-new and change start. Sh to/usr/local/MySQL-New/bin/mysqld_safe. The database can be started normally, but it is not a new version. Why?
The official explanation is: You need to download the source code version and specify the running directory during compilation. This is not necessary.
Study the mysqld_safe file and find that the logic is:
A. Obtain the configuration parameters from the command line.
B. Obtain the configuration parameters from the configuration file.
C. For the mysqld directory (basedir) and data file directory (datadir), if none of the above are specified in the configuration, first check whether the current directory is available, otherwise, set/usr/local/MySQL to the default value.
Therefore, although mysqld_safe under mysql-New is used, it is still running/usr/local/MySQL/bin/mysqld, that is, the old version. You need to modify the startup script or the configuration file accordingly:
#! /Bin/sh
Rundir = "/opt/server0 ";
Echo $ rundir;
/Usr/local/MySQL-New/bin/mysqld_safe -- defaults-file = "$ rundir"/My. CNF -- ledir =/usr/local/MySQL-New/bin -- basedir =/usr/local/MySQL-new -- PID-file = "$ rundir"/MySQL. PID -- datadir = "$ rundir"/var -- socket = "$ rundir"/MySQL. sock -- log-error = "$ rundir"/err. log -- log-Slow-queries = "$ rundir"/slow_query.log &
In this way, you can run multiple MySQL versions on the same machine at the same time.
# -------------------------- The following section shows my experiment...
The above part is a little different from my own practice, but the principles are the same, on the server, I directly set the parameters in/usr/local/mysql5/bin/mysqld_safe -- defaults-file =/etc/my5.cnf & TO THE my5.cnf file. now, so it looks a little shorter. In fact, the most important thing is -- defaults-file. This parameter is not known at first, and many turns have been taken. faint. 2 pages in total.
When installing MySQL, the following parameters are included:
./Configure -- prefix =/usr/local/mysql5 -- localstatedir =/var/data/mysql5 -- With-Unix-socket-Path =/tmp/mysql5.sock
In this way, separate the installation paths of different databases.
If you want to use commands such as/etc/mysqld5 stop, you need to change the stop segment in/etc/mysqld5, like the following code (note pid_file = '/var/data/mysql5/lookyellowpage.com. PID '# Jack added this line is added. You can specify the path in it to avoid/etc/My when running stop. the content in the CNF variable, because the retrieved content may be from other database services and may cause errors ).
'Stop ')
# Stop daemon. We use a signal here to avoid having to know
# Root password.
# The RedHat/Suse lock directory to remove
Lock_dir =/var/lock/subsys/mysqlmanager
# If the manager pid_file doesn't exist, try the server's
If test! -S "$ pid_file"
Then
Pid_file = $ server_pid_file
Lock_dir =/var/lock/subsys/MySQL
Fi
Pid_file = '/var/data/mysql5/lookyellowpage.com. Pi' # Jack added
Echo $ pid_file
If test-s "$ pid_file"
Then
Mysqlmanager_pid = 'cat $ pid_file'
Echo $ mysqlmanager_pid
Echo $ echo_n "shutting down MySQL"
Kill $ mysqlmanager_pid
# Mysqlmanager shocould remove the pid_file when it exits, so wait for it.
Wait_for_pid removed
# Delete lock for Redhat/Suse
If test-F $ lock_dir
Then
Rm-F $ lock_dir
Fi
Else
Log_failure_msg "MySQL manager or server PID file cocould not be found! "
Fi