Detailed tutorial on setting up multiple instances of MySQL 8 on CentOS 7 (you can have as many as you want)

Detailed tutorial on setting up multiple instances of MySQL 8 on CentOS 7 (you can have as many as you want)

cause

I recently started to refactor the project, and I might need to use master-slave plus read-write separation. So I wanted to build one locally to try out the effect. As a result, a Baidu search turned up a lot of results, but few of them were tried by myself. Most of them copied other people's content. The key is that there will be errors in actual applications. The browser opened nearly 20 tabs, and I tried several of them but all had problems. I couldn't use them at all. Not only wasting time, but it also made me feel frustrated, so I did it myself.

1. Preparation

1. First check and clean up the mysql related files in the system

# Check if the Mysql file exists in the system find / -name mysql
 ​
 # Delete the files or folders that exist in mysql rm -rf /usr/lib64/mysql/
 ​
 # Check if there is any mysql-related dependency rpm -qa|grep mysql
 ​
 # Uninstall mysql dependency rpm -e mysql-.....

2. Download the corresponding version of MySQL installation file

Because I am using CentOS 7 64-bit, select the operating system as shown below in the MySQL official website, and then select the tar package for download.

Let me explain here why I use the tarball to install instead of the common online method of one normal installation and the other installation using rpm. Because if the first one is installed normally using yum and the second one is installed locally, then the installation path cannot be specified and the two mysqls cannot be put together for systematic management. So I use the tarball here to install multiple instances and put them in the same directory for easy management and configuration.

# Create a MySQL user before downloading. Why create a MySQL user?
Because the unzipped mysql file does not have any group, and in order to avoid MySQL being unable to read certain files during configuration and startup, it is recommended to use the mysql user to run mysql
 adduser mysql
 # Set password passwd mysql # Next enter the password twice​
 # Switch user su mysql
 ​
 # Download the MySQL tarball wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-el7-x86_64.tar.gz
 ​
 # Unzip the file and rename the folder to mysql_01
 tar xf mysql-8.0.20-el7-x86_64.tar.gz ./mysql_01
 ​
 # Copy mysql_01 and rename it to mysql_02
 cp -r ./mysql_01 ./mysql_02/
 ​
 # Switch to the root user, change the two mysql arrays to the mysql user, and then switch back to the mysql user exit
 chown -R mysql:mysql /home/mysql/mysql_01/ /home/mysql/mysql_02/
 su mysql

2. Modify the configuration

The next step is to configure MySQL. The configurations of the two MySQLs are actually similar, except that the paths are different. Here I will only show the configuration of mysql_01. For mysql_02, just replace the corresponding path with your own path.

1. Customize my.cnf

# Switch to the mysql_01 path cd ./mysql_01
 ​
 # Create a configuration file my.cnf, edit it directly using vim, and save it. Among them, ### indicates a required item, and # indicates an optional item.vim my.cnf
 ----------The following is the file content-------------
 [mysqld]  
 ### Port number port=10085  
   
 ### Installation directory basedir=/home/mysql/mysql_01/ 
   
 ### Data storage path datadir=/home/mysql/mysql_01/data/ 
   
 ###Session file directory socket=/home/mysql/mysql_01/mysql.sock
   
 # The maximum number of connection failures allowed max_connect_errors=10  
   
 # The character set encoding of the server character-set-server=utf8 
   
 #Default storage engine default-storage-engine=INNODB 
   
 #Default user user=mysql  
   
 # Enable slow query slow_query_log=on  
   
 # Slow query log file directory slow_query_log_file=/home/mysql/mysql_01/slow-query.log [client]    
 ### The port number used to connect to the server is port=10085 ​ ### Session file socket=/home/mysql/mysql_01/mysql.sock     
 #Default character set encoding default-character-set=utf8 ---------------End of file content-------------------- ​
 # In the above configuration, you can see that there is a data folder in the datadir item, but there is no data folder in mysql_01.
Therefore, you need to create a data folder mkdir data

2. Modify the default configuration

Next, you need to modify the default configuration information of MySQL

# Modify the default configuration information in the file vim ./support-files/mysql.server
 # Change all the configurations related to basedir and datadir in the file to the current path

As shown below

And modify the path of the MySQL configuration file loaded in the file to the my.cnf file just created, as shown in the figure below. After the modification is completed, save and exit.

You also need to modify another file under support-files

# Modify the mysqld_multi.server file in the support-files folder vim ./support-files/mysqld_multi.server
 ​
 #Still modify the two properties of basedir and datadir, as shown in the following figure

3. Initial installation and start service

1. Initial installation

# Switch to the root directory of mysql_01 cd /home/mysql/mysql_01/
 ​
 # Initialize installation --defaults-file specifies the initialization configuration file --console prints the initialization information on the console./bin/mysqld --defaults-file=/home/mysql/mysql_01/my.cnf --initialize
 --console --user=mysql
 ​
 # After successful initialization, the initialization password will be printed in the console, the general format is as follows root@localhost: fa356fgss,

2. Start the service

# Start the mysql service of the current mysql_01 in the background. You still need to specify the configuration file, because we set a socket parameter in the configuration file.
Only when it is started according to this configuration file will it be generated according to the path configured by the socket, otherwise it will be automatically generated in /tmp/mysql.sock.
If mysql_02 is also generated in this way, it will cause an overlay, resulting in only one service being connected during connection, or even an error.
 ./bin/mysqld_safe --defaults-file=./my.cnf &
 ​
 # Use the command to check whether the mysql service is started, as shown below: netstat -nultp|grep 10085

4. Connect to database and change password

# From the above, you can see that the MySQL service has been started, so you can connect to MySQL./bin/mysql --socket=./mysql.sock -uroot -p # Enter the initial password​
 # Change the MySQL login password. Here I change it to root. Note the semicolon at the end. alter user root@localhost identified by "root";

After the same configuration, start mysql_02, the effect is as follows

The above is the configuration of MySQL_01. The same is true for MySQL_02. You only need to change the corresponding directory to your own file path. After installing multiple instances, you can perform related configurations such as master-slave replication and read-write separation.

Summarize

This is the end of this article about setting up multiple instances of MySQL 8 on CentOS 7 (you can set up as many as you want). For more information about setting up multiple instances of MySQL 8 on CentOS 7, 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:
  • Installing MySQL multiple instances under Linux as a data backup server to achieve multi-master to one-slave multi-instance backup
  • MySQL multi-instance configuration solution
  • MySQL tutorial on how to deploy multiple instances on a single machine using mysqld_multi
  • Deploy MySQL 5.7.17 binary installation and multi-instance configuration on CentOS 6.5
  • Quickly implement MySQL deployment and one-machine multi-instance deployment
  • In-depth analysis based on MySQL multi-instance installation
  • MySQL multi-instance deployment and installation guide under Linux

<<:  About deploying a web project to Alibaba Cloud Server (5 steps to do it)

>>:  Simple implementation method of vue3 source code analysis

Recommend

Solution to incomplete text display in el-tree

Table of contents Method 1: The simplest way to s...

WeChat applet uses canvas to draw clocks

This article shares the specific code of using ca...

CocosCreator classic entry project flappybird

Table of contents Development Environment Game en...

SQL function to merge a field together

Recently, I need to query all the fields in a rel...

Install Kafka in Linux

Table of contents 1.1 Java environment as a prere...

js implements shopping cart addition and subtraction and price calculation

This article example shares the specific code of ...

A brief discussion on this.$store.state.xx.xx in Vue

Table of contents Vue this.$store.state.xx.xx Get...

Guide to using env in vue cli

Table of contents Preface Introduction-Official E...

Linux uses iptables to limit multiple IPs from accessing your server

Preface In the Linux kernel, netfilter is a subsy...

Nginx merges request connections and speeds up website access examples

Preface As one of the best web servers in the wor...

How to install Composer in Linux

1. Download the installation script - composer-se...

WeChat applet implements a simple dice game

This article shares the specific code of the WeCh...

MySql quick insert tens of millions of large data examples

In the field of data analysis, database is our go...

mysql delete multi-table connection deletion function

Deleting a single table: DELETE FROM tableName WH...