MySQL Series II Multi-Instance Configuration

MySQL Series II Multi-Instance Configuration

Tutorial Series

MySQL series: Basic concepts of MySQL relational database
MariaDB-server installation of MySQL series
MySQL Series 3 Basics
MySQL Series 4 SQL Syntax
MySQL series five views, stored functions, stored procedures, triggers
MySQL series 6 users and authorization
MySQL Series 7 MySQL Storage Engine
MySQL Series 8 MySQL Server Variables
MySQL series 9 MySQL query cache and index
MySQL Series 10 MySQL Transaction Isolation to Implement Concurrency Control
MySQL Series 11 Logging
MySQL Series 12 Backup and Recovery
MySQL Series 13 MySQL Replication
MySQL Series 14 MySQL High Availability Implementation
MySQL series 15 MySQL common configuration and performance stress test

What is MySQL multi-instance?

Simply put, MySQL multi-instance is to open multiple different service ports (such as: 3306, 3307) on a server at the same time, and run multiple MySQL service processes at the same time. These service processes provide services through different sockets listening to different service ports.

These MySQL multiple instances share a set of MySQL installation programs, using different my.cnf (can also be the same) configuration files, startup programs (can also be the same) and data files. When providing services, multiple MySQL instances appear to be logically independent of each other, and they obtain the corresponding amount of hardware resources from the server based on the corresponding settings in the configuration file.

To give an example, multiple MySQL instances are equivalent to multiple bedrooms in a house. Each instance can be regarded as a bedroom, and the entire server is a house. The server's hardware resources (cpu, men, disk) and software resources (CentOS operating system) can be regarded as the bathroom, kitchen, and living room of the house, which are the shared resources of the house. If you are a migrant worker in Beijing and rent a house with friends, I believe you will understand it better. You all live together, rest in your own bedroom, and when you go out, you will definitely share the above-mentioned public resources. This will give you a good understanding of MySQL multiple instances.

In fact, many network services can be configured with multiple instances, such as nginx, Apache, haproxy, redis, etc. This is widely used in portal websites.

If multiple test environments are needed in one physical machine, then multiple instances of the database need to be built. Multiple instances mean running multiple programs, and there is no impact between instances. Note that the listening port needs to be different.

Environment: CentOS7.5, compile and install MariaDB-10.2.15 version, software installation directory: /app/mysql/

1) Create a running directory environment

[root@centos7 ~]# mkdir -p /mysqldb/{3306,3307,3308}/{etc,socket,pid,log,data,bin}
[root@centos7 ~]# chown -R mysql:mysql /mysqldb/

2) Initialize the database

[root@centos7 ~]# cd /app/mysql/
[root@centos7 mysql]# scripts/mysql_install_db --datadir=/mysqldb/3306/data/ --user=mysql --basedir=/app/mysql/ 
[root@centos7 mysql]# scripts/mysql_install_db --datadir=/mysqldb/3307/data/ --user=mysql --basedir=/app/mysql/
[root@centos7 mysql]# scripts/mysql_install_db --datadir=/mysqldb/3308/data/ --user=mysql --basedir=/app/mysql/

The above is compiled and installed. The installation directory is /app/mysql/. You need to enter the software installation directory and execute the initialization script. If it is a package installed by yum, just run the mysql_install_db command directly.

3) Provide the configuration file and modify it as needed

[root@centos7 mysql]# cp support-files/my-huge.cnf /mysqldb/3306/etc/my.cnf
[root@centos7 mysql]# cp support-files/my-huge.cnf /mysqldb/3307/etc/my.cnf
[root@centos7 mysql]# cp support-files/my-huge.cnf /mysqldb/3308/etc/my.cnf
[root@centos7 mysqldb]# cd /mysqldb/
[root@centos7 mysqldb]# vim 3306/etc/my.cnf
[mysqld]
port = 3306
datadir = /mysqldb/3306/data
socket = /mysqldb/3306/socket/mysql.sock
[root@centos7 mysqldb]# vim 3307/etc/my.cnf #Change according to the above configuration example [root@centos7 mysqldb]# vim 3308/etc/my.cnf

4) Provide service startup script

[root@centos7 ~]# cat mysqld #Script example#!/bin/bash

port=3306 #needs to be changed to the port number of the current instancemysql_user="root"
mysql_pwd=""
cmd_path="/app/mysql/bin" #bin in the installation directory
mysql_basedir="/mysqldb" #The directory where the instance database file is locatedmysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
    else
      printf "MySQL is running...\n"
      exit
    fi
}


function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
   fi
}


function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac
[root@centos7 ~]# cp mysqld /mysqldb/3306/bin/
[root@centos7 ~]# cp mysqld /mysqldb/3307/bin/
[root@centos7 ~]# cp mysqld /mysqldb/3308/bin/
[root@centos7 ~]# vim /mysqldb/3306/bin/mysqld
port=3306
[root@centos7 ~]# vim /mysqldb/3307/bin/mysqld
port=3307
[root@centos7 ~]# vim /mysqldb/3308/bin/mysqld
port=3308

5) Modify the script file permissions to prevent the password from being seen by others

[root@centos7 ~]# chmod 700 /mysqldb/3306/bin/mysqld 
[root@centos7 ~]# chmod 700 /mysqldb/3307/bin/mysqld  
[root@centos7 ~]# chmod 700 /mysqldb/3308/bin/mysqld 

6) Start the service

[root@centos7 ~]# service mysqld stop #Ensure that the original service is stopped and release port 3306 [root@centos7 ~]# /mysqldb/3306/bin/mysqld start #Start the service [root@centos7 ~]# /mysqldb/3307/bin/mysqld start
[root@centos7 ~]# /mysqldb/3308/bin/mysqld start
[root@centos7 ~]# ss -tnl #If you see that the ports listened by the three instances are all open, it means that the service is started normally LISTEN 0 80 :::3306 :::*
LISTEN 0 80 :::3307 :::*
LISTEN 0 80 :::3308 :::*

7) Connection test

[root@centos7 ~]# mysql -S /mysqldb/3306/socket/mysql.sock #Use -S to specify the socket file Server version: 10.2.15-MariaDB-log Source distribution
MariaDB [(none)]> show variables like '%port'; #Check if the port is 3306
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| extra_port | 0 |
| large_files_support | ON |
| port | 3306 |
| report_port | 3306 |
+---------------------+-------+
4 rows in set (0.00 sec)

[root@centos7 ~]# mysql -S /mysqldb/3307/socket/mysql.sock #Reconnect and test 3307 and 3308
Server version: 10.2.15-MariaDB-log Source distribution
MariaDB [(none)]> show variables like '%port';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| extra_port | 0 |
| large_files_support | ON |
| port | 3307 |
| report_port | 3307 |
+---------------------+-------+
4 rows in set (0.00 sec)

[root@centos7 ~]# mysql -S /mysqldb/3308/socket/mysql.sock
Server version: 10.2.15-MariaDB-log Source distribution
MariaDB [(none)]> show variables like '%port';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| extra_port | 0 |
| large_files_support | ON |
| port | 3308 |
| report_port | 3308 |
+---------------------+-------+
4 rows in set (0.00 sec)

Multiple instances have been successfully built!

8) Use this command to stop the instance

[root@centos7 ~]# /mysqldb/3306/bin/mysqld stop

9) The last step: add a password for the root user~

[root@centos7 ~]# mysql -S /mysqldb/3307/socket/mysql.sock 
Server version: 10.2.15-MariaDB-log Source distribution

MariaDB [(none)]> update mysql.user set password=PASSWORD("your_password") where user='root';
Query OK, 4 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *9E72259BA9214F692A85B240647C4D95B0F2E08B |
| root | centos7 | *9E72259BA9214F692A85B240647C4D95B0F2E08B |
| root | 127.0.0.1 | *9E72259BA9214F692A85B240647C4D95B0F2E08B |
| root | ::1 | *9E72259BA9214F692A85B240647C4D95B0F2E08B |
| | localhost | |
| | centos7 | |
+------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)

[root@centos7 ~]# mysql -S /mysqldb/3307/socket/mysql.sock -uroot -p'your_password' #Specify the password and log in again OK~

Finally, add your password to the bin/mysqld script file to prevent the service from failing to start

This is the end of this article about MySQL series 2 multi-instance configuration. For more information about MySQL multi-instance configuration, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL database introduction: detailed explanation of multi-instance configuration method
  • MySQL multi-instance configuration solution
  • MySQL tutorial on how to deploy multiple instances on a single machine using mysqld_multi
  • Quickly implement MySQL deployment and one-machine multi-instance deployment
  • In-depth analysis based on MySQL multi-instance installation

<<:  Detailed explanation of the 4 codes that turn the website black, white and gray

>>:  How to share Flash pages through verification methods in website standards

Recommend

Complete steps to use mock.js in Vue project

Using mock.js in Vue project Development tool sel...

XHTML Getting Started Tutorial: Using the Frame Tag

<br />The frame structure allows several web...

Implementation of Vue counter

Table of contents 1. Implementation of counter 2....

Use docker to deploy tomcat and connect to skywalking

Table of contents 1. Overview 2. Use docker to de...

Pure HTML+CSS to achieve typing effect

This article mainly introduces the typing effect ...

Solution to the problem of repeated triggering of functions in Vue project watch

Table of contents Problem description: Solution 1...

Implementation of Docker cross-host network (overlay)

1. Docker cross-host communication Docker cross-h...

Introduction and use of triggers and cursors in MySQL

Trigger Introduction A trigger is a special store...