Often encountered business scenarios is a number of units share a cloud server, then the use of the database is used separately, after all, given the limited resources, then at this time the need for stand-alone Mysql multiple instances to solve the problem.

1、Creating a Mysql User Account

Multiple examples of databases can be created with different accounts and passwords, so here’s a simple example of a mysql user

groupadd mysql
useradd -g mysql -M -s /sbin/nologin mysql

2、Download Mysql and install it

下载地址https://downloads.mysql.com/archives/community/

Unzip the binary file and move it to the specified directory

tar -xvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql

3、Creating configuration files and data directories

Create three data directories

mkdir -pv /data/{3006,3007,3008}/data
tree /data/
/data/
├── 3306
│   └── data
├── 3307
│   └── data
└── 3308
└── data

Creating Configuration Files /data/3306/my.cnf /data/3307/my.cnf /data/3308/my.cnf

The my.cnf configuration reads as follows

[mysql]
default-character-set = utfe
port = 3306
socket = /data/3306/mysql.sock
[mysqld]
user = mysql
log-bin = mysql-bin
port = 3306
socket = /data/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3306/data
character-set-server = utf8
pid-file = /data/3306/mysql.pid
relay-log = /data/3306/relay-bin
relay-log-info-file = /data/3306/relay-log.info
# Server ID
server-id = 1
# gtid
gtid_mode = on
enforce-gtid-consistency = true
log-slave-updates = on
skip_slave_start = 1
# Record relay.info into the data table
relay_log_info_repository = TABLE
# Record master.info into the data table
master_info_repository = TABLE
[mysqld_safe]
log-error = /data/3306/mysql_3306.err
pid-file = /data/3306/mysqld.pid

⚠️ 注意 Copy to the other two instances, pay attention to modify port 3307, 3308, and pay attention to server-id number> to be staggered

4、Authorization and Initialization

Add Mysql data directory user permissions chown -R mysql:mysql /data

Multiple Instance Initialization

cd /usr/local/mysql/bin
./mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/3306/data --user=mysql
./mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/3307/data --user=mysql
./mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/3308/data --user=mysql
# The last line of the initialization printout ends with the MySql login password, remember to save it!

5、Adding MySQL Environment Variables and Systemd Management

Adding MySQL Environment Variables

echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
source /etc/profile

Add systemd management

cat > /etc/systemd/system/mysql-3306.service <<EOF
[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
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3306/my.cnf
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/mysql-3307.service <<EOF
[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
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/mysql-3308.service <<EOF
[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
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target
EOF

6、Start and set up the boot-up service

systemctl start mysql-3306.service
systemctl start mysql-3307.service
systemctl start mysql-3308.service
systemctl enable mysql-3306.service
systemctl enable mysql-3307.service
systemctl enable mysql-3308.service

7、Logging in to the database

[root@localhost ~]#m mysql -S /data/3306/mysql.sock -u root -P 3306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement

⚠️ 注意 Note the login parameters

  • -S is to use the socket
  • -h is to use an IP, such as 127.0.0.1 or localhost
  • -u is the login username
  • -P is to specify the port number, if it is a single instance of the default do not need to add, multiple instances need to specify the port
  • -p Enter the password parameters (note that they are case sensitive, the above is upper case P, this is lower case p)

Note that you need to change your password the first time you log in to MySQL, otherwise you will not have access to the database.

Modify the database user login password, followed by the above operation

mysql> set password for root@localhost = password( '123456' );
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>quit
Bye
# Logging in again to perform database operations works fine
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)