centos7 implements multiple instances of MYSQL

Since this is to implement multiple instances of mysql, I won’t explain too much about the installation here. If necessary, you can read the installation tutorials of other bloggers (the yum source of mysql can be installed by downloading the rpm package from the mysql official website)

MySQL :: Download MySQL Yum Repository

Leading part

First, you need to install mysql. I installed it using yum source.

So just yum -y install mysql-server mysql

After the installation is complete, check the status of the service

The service name of MySQL is mysqld.

So use systemctl status mysqld to check the status

Note: Both firewall and selinux need to be turned off!

systemctl status firewalld

This is the firewall running

Use systemctl stop firewalld to temporarily turn off the firewall (it will not work after restarting)

Turn off firewall permanently

systemctl disable firewalld

Check selinux status

getenforce

Temporarily shut down selinux

getenforce 0

Permanently shut down selinux (it is recommended to shut down permanently, otherwise you will have to reset it next time you start it)

First enter the selinux configuration file

vim /etc/selinux/config

Go to the configuration file

to modify

Perform instantiation operations

Create mysql instance directory

mkdir -p /data/mysql/330{6,7}/{data,logs,conf,socket,pid}

The structure is as follows:

You don’t necessarily have the tree command, as long as the folder structure is correct, it doesn’t matter.

Set directory owner group

chown -R mysql:mysql /data/mysql

Check it out

It can be seen here that the owner and group are all mysql

Back up the original configuration file of mysql

mv /etc/my.cnf /etc/my.cnf_bak

Added configuration file my3306.cnf

vim /data/mysql/3306/conf/my.cnf

[mysqld]
user=mysql
port=3306
server_id = 3306
datadir = /data/mysql/3306/data
socket = /data/mysql/3306/socket/mysql.sock
symbolic-links=0
log-error = /data/mysql/3306/logs/mysqld.log
pid-file = /data/mysql/3306/pid/mysqld.pid

Copy a copy to port 3307

cp /data/mysql/3306/conf/my.cnf /data/mysql/3307/conf/my.cnf

Modify all the contents of 3306 in the file to 3307

sed -i ‘s/3306/3307/g’ /data/mysql/3307/conf/my.cnf

Check out the configuration file of 3307

cat /data/mysql/3307/conf/my.cnf

[mysqld]
user=mysql
port=3307
server_id = 3307
datadir = /data/mysql/3307/data
socket = /data/mysql/3307/socket/mysql.sock
symbolic-links=0
log-error = /data/mysql/3307/logs/mysqld.log
pid-file = /data/mysql/3307/pid/mysqld.pid

Back up the startup service file of the service

mv /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/mysqld.service_bak

Added mysqld3306.service startup file

vim /usr/lib/systemd/system/mysqld3306.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/data/mysql/3306/pid/mysqld.pid
TimeoutSec=0
PermissionsStartOnly=true
#ExecStartPre=/usr/bin/mysqld_pre_systemd_3306 3306
ExecStart=/usr/sbin/mysqld --defaults-file=/data/mysql/3306/conf/my.cnf --daemonize --pid-file=/data/mysql/3306/pid/mysqld.pid $MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false

Added mysqld3307.service startup file

cp /usr/lib/systemd/system/mysqld3306.service /usr/lib/systemd/system/mysqld3307.service

Modify the file directory of the mysql service file

sed -i ‘s/3306/3307/g’ /usr/lib/systemd/system/mysqld3307.service

View content

cat /usr/lib/systemd/system/mysqld3307.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/data/mysql/3307/pid/mysqld.pid
TimeoutSec=0
PermissionsStartOnly=true
#ExecStartPre=/usr/bin/mysqld_pre_systemd_3307 3307
ExecStart=/usr/sbin/mysqld --defaults-file=/data/mysql/3307/conf/my.cnf --daemonize --pid-file=/data/mysql/3307/pid/mysqld.pid $MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false

Initialize multiple instances 3306, 3307

mysqld –defaults-file=/data/mysql/3306/conf/my.cnf –initialize –user=mysql –datadir=/data/mysql/3306/data

mysqld –defaults-file=/data/mysql/3307/conf/my.cnf –initialize –user=mysql –datadir=/data/mysql/3307/data

Start multiple instances 3306, 3307

Check out mysql services

netstat -nutpl | grep mysql

ps -ef | grep mysql

It’s almost done here.

How to log in to mysql instance

When logging in with mysql, first find the temporary password

cat /data/mysql/3306/logs/mysqld.log | grep password

Because I made a mistake before and forgot to delete the l log file, the previous temporary password still exists, so there are two temporary passwords, but the first one has expired. Don’t worry, everyone.

Then log in

mysql -P 3306 -S /data/mysql/3306/socket/mysql.sock -uroot -p’hX> & amp;f%Rqq3P + ‘

Login successful!

Another instance also logs in using this method.

After entering, the database cannot be operated and the password must be changed:

ALTER USER ‘root’@’localhost’ identified by “Mysql@2023”;

If OK is displayed, it means the password has been changed successfully.