Tutorial Series MySQL series: Basic concepts of MySQL relational database 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:
|
<<: 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
Preface This article mainly shares with you an ex...
How does "adaptive web design" work? It’...
Using mock.js in Vue project Development tool sel...
I have seen a lot of MySQL-related syntax recentl...
<br />The frame structure allows several web...
Table of contents 1. Implementation of counter 2....
Table of contents 1. Introduction to MHA 1. What ...
Table of contents 1. Overview 2. Use docker to de...
This article mainly introduces the typing effect ...
When developing applications that use a database,...
What is a style guide? Simply put, it’s a document...
Table of contents Problem description: Solution 1...
Preface: I'm currently learning Linux and .Ne...
1. Docker cross-host communication Docker cross-h...
Trigger Introduction A trigger is a special store...