Use different ways to build MySQL multi-instance

The so-called multi-instance is to build and run multiple MySQL instances on one server. Each instance uses different service ports and monitors through different sockets. Physically, each instance has an independent parameter configuration file and database.

In general, it is not recommended to run multiple MySQL instances on a server, especially in a production environment, because this will cause the problem of resource grabbing from each other. However, in some high-availability environments, there are such requirements. For example, in a failover cluster environment built with Heartbeat, if the active and standby servers only run one MySQL instance, the standby servers are usually in an idle state, resulting in a waste of resources; Running a MySQL instance requires the other party to take over its tasks when one party fails, that is, running two MySQL instances at the same time. Based on this requirement, the MySQL server should be reasonably configured to have the ability to run multiple instances at the same time.

The key to how to build and run multiple MySQL instances on one server lies in how to assign independent parameter files to each instance. The following are several commonly used multi-instance solutions, learn and familiarize yourself with them.

system environment

OS: CentOS 5.8 (x86_64) Kernel: 2.6.18-308.el5 DB: MySQL 5.5.17

one. Using the source package to install MySQL to achieve multiple instances

The source package is the most flexible installation package. It can be customized and compiled with some path parameters, and can be installed to any path. Therefore, multiple MySQL instances can be built on one server. In this example, to install two MySQL instances, the steps are as follows:

1. Preparations

Starting from MySQL 5.5, the cmake tool is used to compile the source package, so to install it first, you can refer to other documents, which will not be explained here. In addition, two dependent packages, ncurses-devel and bison, need to be installed before compiling, otherwise the compilation will fail.

- install dependencies

# rpm -ivh ncurses-devel-5.5-24.20060715.x86_64.rpm

# rpm -ivh bison-2.3-2.1.x86_64.rpm

——Create mysql system group and user

# groupadd -g 497 mysql

# useradd –u 499 -g mysql mysql

——Unzip the installation package

# tar -zxvf mysql-5.5.17.tar.gz

# cd mysql-5.5.17

2. Compile and install

l MySQL instance 1:

(port: 3306, installation directory: /usr/local/mysqla, data file directory: /data/lib/mysqla)

——Custom compilation

# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysqla-DMYSQL_TCP_PORT=3306 -DMYSQL_DATADIR=/data/lib/mysqla-DMYSQL_UNIX_ADDR=/data/lib/mysqla/mysql.sock -DSYSCONFDIR=/usr/local/mysqla-DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1-DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1-DWITH_PARTITION_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1-DWITH_SSL=yes -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci-DEXTRA_CHARSETS=all

# make

# make install

——Create system database and system table

# cd /usr/local/mysqla/

# scripts/mysql_install_db --user=mysql--basedir=/usr/local/mysqla --datadir=/data/lib/mysqla

l MySQL instance 2:

(port: 3307, installation directory: /usr/local/mysqlb, data file directory: /data/lib/mysqlb)

- Delete old cache and object files

Before compiling again, clear the original cache information and object files, as follows:

# cd mysql-5.5.17

# make clean

# rm -rf CMakeCache.txt

——Custom compilation

# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysqlb-DMYSQL_TCP_PORT=3307 -DMYSQL_DATADIR=/data/lib/mysqlb-DMYSQL_UNIX_ADDR=/data/lib/mysqlb/mysql.sock -DSYSCONFDIR=/usr/local/mysqlb-DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1-DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1-DWITH_PARTITION_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1-DWITH_SSL=yes -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci-DEXTRA_CHARSETS=all

# make

# make install

——Create system database and system table

# cd /usr/local/mysqlb/

# scripts/mysql_install_db --user=mysql--datadir=/data/lib/mysqlb

3. Configuration parameter file

After the above two compilations and creation of the system database, two MySQL instances are installed and built, and then the parameter files are configured for them respectively.

——View the default location of the parameter file

# /usr/local/mysqla/bin/mysql --help|grep '/my.cnf'

/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysqla/my.cnf ~/.my.cnf

# /usr/local/mysqlb/bin/mysql --help|grep '/my.cnf'

/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysqlb/my.cnf ~/.my.cnf

It can be seen from this that 3 of the 4 default parameter files of the two MySQL instances are the same, so they cannot be used, otherwise they cannot be distinguished; of course, the same parameter part among multiple instances can be configured in this way, but Inconvenient to manage.

In order to make each MySQL instance have an independent parameter file, only the third default parameter file can be used, that is, the parameter file is stored in the installation directory (basedir) of each instance. In addition, it is also necessary to ensure that the fourth parameter file does not exist, because it has a higher priority, and if it exists, it will overwrite the previous parameter file settings.

The detailed configuration of the parameter files is not described here. It is assumed that the respective parameter files have been configured for the two MySQL instances and stored in their respective installation directories, that is, the /usr/local/mysqla|mysqlb directory.

4. Launch the instance

To start the MySQL instance, you need to set the correct permissions for the datadir directory, otherwise the instance will fail to start; this step should be set by default, check it, if not set, use the following command to set it.

# chown -R mysql.mysql mysql/

- Start two MySQL instances

# /usr/local/mysqla/bin/mysqld_safe --user=mysql &

# /usr/local/mysqlb/bin/mysqld_safe --user=mysql &

- view progress

# ps -ef | grep mysql

root 1694 26876 0 13:04 pts/2 00:00:00 more/usr/local/mysqla/support-files/mysql.server

root 2270 13474 0 13:13 pts/1 00:00:00 /bin/sh/usr/local/mysqla/bin/mysqld_safe --user=mysql

mysql 2805 2270 113:13 pts/1 00:00:00 /usr/local/mysqla/bin/mysqld--basedir=/usr/local/mysqla --datadir=/data/lib/mysqla--plugin-dir=/usr/local/mysqla/lib/plugin --user=mysql--log-error=/data/lib/mysqla/mysql.err --pid-file=/data/lib/mysqla/mysql.pid--socket=/data/lib/mysqla/mysql.sock --port=3306

root 2828 13474 0 13:13 pts/1 00:00:00 /bin/sh/usr/local/mysqlb/bin/mysqld_safe --user=mysql

mysql 3361 2828 25 13:13 pts/1 00:00:00 /usr/local/mysqlb/bin/mysqld--basedir=/usr/local/mysqlb --datadir=/data/lib/mysqlb --plugin-dir=/usr/local/mysqlb/lib/plugin--user=mysql --log-error=/data/lib/mysqlb/mysql.err--pid-file=/data/lib/mysqlb/mysql.pid --socket=/data/lib/mysqlb/mysql.sock--port=3307

root 3381 13474 0 13:13 pts/1 00:00:00 grep mysql

As can be seen, two MySQL instances (processes) were successfully started

――To close the service, execute the following command

# /usr/local/mysqla/bin/mysqladmin shutdown

# /usr/local/mysqlb/bin/mysqladmin shutdown

5. Modified to service management mode

In order to facilitate management, it is modified to service management mode, as follows:

——Copy service files

# cp /usr/local/mysqla/support-files/mysql.server/etc/rc.d/init.d/mysqla

# cp /usr/local/mysqlb/support-files/mysql.server/etc/rc.d/init.d/mysqlb

——Add service

# chkconfig --add mysqla

# chkconfig --list mysqla

mysqla 0:off 1:off 2:on 3:on 4:on 5:on 6:off

# chkconfig --add mysqlb

# chkconfig --list mysqlb

mysqlb 0:off 1:off 2:on 3:on 4:on 5:on 6:off

- start the service

# service mysqla start

Starting MySQL.. [ OK ]

[root@db ~]# service mysqlb start

Starting MySQL.. [ OK ]

So far, we have successfully built two MySQL instances through the source package, which have independent ports, parameter files and databases.

two. Use binary package to install MySQL to achieve multi-instance

The binary package is actually the compiled source package, and the parameter files of each instance can no longer be isolated by customizing the default path of the parameter file; however, it should be noted that the default path of the third parameter file refers to It is basedir (installation directory), which can be used to isolate the parameter files of each instance, so one server can build multiple MySQL instances.

Also install two MySQL instances, the steps are as follows:

1. Preparations

——Create mysql system group and user

# groupadd -g 497 mysql

# useradd -u 499 -g mysql mysql

——Unzip the installation package

Extract the binary installation package to the /usr/local/ directory and rename it to mysql, which is the default directory for the basedir parameter when the binary package is compiled.

# tar zxvf mysql-5.5.17-linux2.6-x86_64.tar.gz -C /usr/local/

# cd /usr/local/

# mv mysql-5.5.17-linux2.6-x86_64 mysql

——Copy the decompressed mysql and generate the following two folders to be used as installation directories for the two instances

# cd /usr/local/

# cp -R mysql mysqla

# cp -R mysql mysqlb

# ll

Note: The mysql directory is reserved here to configure hard connections so that the mysql client program can be accessed in any directory.

2. Create the system database

Create a system database for two MySQL instances. The basedir is /usr/loca/mysqla|mysqlb respectively, and the datadir is /data/lib/mysqla|mysqlb respectively, as follows:

# cd /usr/local/mysql/

# scripts/mysql_install_db --user=mysql--basedir=/usr/local/mysqla --datadir=/data/lib/mysqla

# scripts/mysql_install_db --user=mysql--basedir=/usr/local/mysqlb --datadir=/data/lib/mysqlb

3. Configuration parameter file

In order to make each MySQL instance have an independent parameter file, store it in its own installation directory (basedir), and ensure that there is no parameter file in the default path to prevent reading errors.

Assuming that the parameter file has been configured, the ports are 3306 and 3307, the basedirs are /usr/local/mysqla, /usr/local/mysqlb, and the datadirs are /data/lib/mysqla, /data/lib/mysqlb.

4. Launch the instance

In this case, you can specify a parameter file for it through --defaults-file when starting the instance, as follows:

# /usr/local/mysql/bin/mysqld_safe--defaults-file=/usr/local/mysqla/my.cnf &

# /usr/local/mysql/bin/mysqld_safe--defaults-file=/usr/local/mysqlb/my.cnf &

When closing an instance, you also need to specify its parameter file, otherwise it will fail.

# /usr/local/mysql/bin/mysqladmin--defaults-file=/usr/local/mysqla/my.cnf shutdown

# /usr/local/mysql/bin/mysqladmin--defaults-file=/usr/local/mysqlb/my.cnf shutdown

5. Modified to service management mode

The binary package does not use the default parameter file, so it is a bit complicated to modify the service management mode. It is necessary to modify the basedir and datadir parameters in the service startup file, as follows:

——Copy service files

# cp /usr/local/mysql/support-files/mysql.server/etc/rc.d/init.d/mysqla

# cp /usr/local/mysql/support-files/mysql.server/etc/rc.d/init.d/mysqlb

——Add service

# chkconfig --add mysqla

# chkconfig --add mysqlb

——Modify the service startup file

Edit the two service startup files, and modify the basedir and datadir parameters to appropriate paths, as follows:

# vi /etc/rc.d/init.d/mysqla

basedir=/usr/local/mysqla

datadir=/data/lib/mysqla

# vi /etc/rc.d/init.d/mysqlb

basedir=/usr/local/mysqlb

datadir=/data/lib/mysqlb

Note: By explicitly setting basedir, each MySQL instance has a separate parameter file.

- start the service

# service mysqla start

Starting MySQL.. [ OK ]

# service mysqlb start

Starting MySQL.. [ OK ]

# service mysqlb status

MySQL running (30326) [ OK ]

# service mysqla status

MySQL running (29755) [ OK ]

After changing to the service management mode, it is convenient to start and close the service, but when you log in to the database locally, you need to specify the socket file, because the default /tmp/mysql.sock does not exist, such as:

# mysql --socket=/data/lib/mysqla/mysql.sock

# mysql --socket=/data/lib/mysqlb/mysql.sock

three. Use RPM package to install MySQL to achieve multi-instance

The file layout of an RPM package is fixed and cannot be modified, so a server can usually only have one instance of MySQL installed. But we know that in any way, the MySQL instance is started by calling the mysqld_safe command, and the mysqld_safe command can explicitly specify a parameter file through the --defaults-file parameter, so it can also be installed and run on a server. Multiple MySQL instances, just make some adjustments to the service startup file, let's try this method.

1. Install MySQL

——Install the server and client packages

# rpm -ivh MySQL-server-5.5.17-1.rhel5.x86_64.rpm

# rpm -ivh MySQL-client-5.5.17-1.rhel5.x86_64.rpm

- start the service

# service mysql start

Starting MySQL.. [ OK ]

——View basedir and datadir parameter values

# mysql

mysql> show variables like 'basedir';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| basedir |/usr |

+---------------+-------+

1 row in set (0.00 sec)

mysql> show variables like 'datadir';

+---------------+-----------------+

| Variable_name | Value |

+---------------+-----------------+

| datadir | / var / lib / mysql / |

+---------------+-----------------+

1 row in set (0.01 sec)

It can be seen that basedir defaults to /usr, and datadir defaults to /var/lib/mysql.

2. Create the system database

By default, the RPM package has created a system database, which is located in the /var/lib/mysql directory. Copy this directory to the /data/lib directory and name them mysqla and mysqlb respectively, so as to create two instances of system database.

# service mysql stop

Shutting down MySQL. [ OK ]

# cp -r /var/lib/mysql/ /data/lib/mysqla

# cp -r /var/lib/mysql/ /data/lib/mysqlb

Of course, it can also be created by the mysql_install_db tool, which is located in the /usr/bin directory, as follows:

# /usr/bin/mysql_install_db --user=mysql--datadir=/data/lib/mysqla

# /usr/bin/mysql_install_db --user=mysql--datadir=/data/lib/mysqlb

3. Configuration parameter file

Configure two parameter files, named mya.cnf and myb.cnf, and store them in the /etc/ directory; the port numbers are 3306 and 3307, and the datadir paths are /data/lib/mysqla, /data/lib/mysqlb .

4. Launch the instance

Like the binary package multi-instance, the parameter file can only be specified by --defaults-file when starting and closing the instance, as follows:

# /usr/bin/mysqld_safe --defaults-file=/etc/mya.cnf &

# /usr/bin/mysqld_safe --defaults-file=/etc/myb.cnf &

# /usr/bin/mysqladmin --defaults-file=/etc/mya.cnf shutdown

# /usr/bin/mysqladmin --defaults-file=/etc/myb.cnf shutdown

5. Modify the service management method

For convenience, it is still necessary to modify it to the service management mode, and perform the following modification operations.

- Create a service startup file

According to the service startup file mysql, create two corresponding service startup files mysqla and mysqlb

# cd /etc/rc.d/init.d/

# cp mysql mysqla

# cp mysql mysqlb

——Add service

# chkconfig --add mysqla

# chkconfig --add mysqlb

——Delete the original service startup files and services (to avoid accidental startup)

# chkconfig --del mysql

# rm /etc/rc.d/init.d/mysql

——Modify the service startup file

Edit the service startup file and make the following adjustments, mainly in the following places:

# vi /etc/rc.d/init.d/mysqla

l 1: Add a variable and set it as a parameter file

my_cnf=/etc/mya.cnf

l 2: Modify the extra_args parameter value, the purpose is to let the parse_server_arguments function parse the datadir and other parameter values ​​from it

(The red font is the added part)

extra_args=""

if test -r "$basedir/my.cnf"

then

extra_args="-e$basedir/my.cnf"

else

if test -r"$datadir/my.cnf"

then

extra_args="-e$datadir/my.cnf"

else

extra_args="-e $my_cnf"

be

be

l 3: Use --defaults-file in the mysqld_safe command to explicitly specify a parameter file

$bindir/mysqld_safe--defaults-file=$my_cnf--datadir="$datadir" --pid-file="$mysqld_pid_file_path"$other_args >/dev/null 2>&1 &

Note: The --defaults-file parameter must be followed by mysqld_safe, and there can be no other parameters in the middle, otherwise the startup will fail.

l 4: Modify lock_file_path. If it is not modified, it is not clear what the impact will be, but just in case, modify it.

lock_file_path="$lockdir/mysqla"

l 5: Find 'status' and replace the command pidof to get the mysqld process with ps

#mysqld_pid=`pidof $libexecdir/mysqld`

mysqld_pid=`ps --user=mysql -f|grep "$libexecdir/mysqld--defaults-file=$my_cnf"|awk ''`

Description: The pidof command is used to obtain the process ID of $libexecdir/mysqld, and then judge the status of the MySQL instance. Since the path and name are fixed, it is impossible to distinguish the process corresponding to each instance, resulting in confusion in Status; use the ps command to filter The function fulfills this requirement, finding its corresponding process for each instance.

In the same way, adjust the service startup file for the second MySQL instance.

- start the service

[root@db mysqla]# service mysqla start

Starting MySQL.. [ OK ]

[root@db mysqla]# service mysqlb start

Starting MySQL.. [ OK ]

So far, we have installed two MySQL instances on one server via RPM packages.

——————————————————————————————————————————

The OCP weekend training in Chongqing Sizhuang in January 2018 is being taught face-to-face, please contact us for an audition! The new OCP weekend class will start on April 1, and registration is hot! For more details, visit the Sizhuang website to consult online customer service~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325060770&siteId=291194637