很多时候受限于资源限制,需要在单机上安装多个MySQL实例。
本文基于MySQL 5.7.27,介绍如何在单机环境下安装多个MySQL实例。
操作系统是Cent OS 6.8。
下文中的方案,建立了3个实例,端口分别为3306, 3307, 3308。
首先创建mysql用户组和用户。
并设置不允许登录。
# groupadd mysql
# useradd -s /sbin/nologin -g mysql -M mysql
下载地址
https://downloads.mysql.com/archives/community/
版本 5.7.27
二进制安装文件 mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
解压二进制文件,移动到指定目录,并创建软链接。
[root@localhost tmp]# tar zvxf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
[root@localhost tmp]# mkdir -p /application
[root@localhost tmp]# mv mysql-5.7.27-linux-glibc2.12-x86_64 /application/mysql-5.7.27
[root@localhost tmp]# ln -s /application/mysql-5.7.27 /application/mysql
这里使用/application/作为安装目录。
创建3个数据文件目录:
mkdir -p /data/{3306,3307,3308}/data
创建3个实例的3个配置文件,分别为,
/data/3306/my.cnf
/data/3307/my.cnf
/data/3308/my.cnf
其中,3306的配置文件内容如下:
[mysql]
default-character-set = utf8
port = 3306
socket = /data/3306/mysql.sock
[mysqld]
user = mysql
log-bin = mysql-bin
port = 3306
socket = /data/3306/mysql.sock
basedir = /application/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
# 记录 relay.info 到数据表中
relay_log_info_repository = TABLE
# 记录 master.info 到数据表中
master_info_repository = TABLE
[mysqld_safe]
log-error = /data/3306/mysql_3306.err
pid-file = /data/3306/mysqld.pid
其他两个实例 3307,3308的配置文件,需要将’3306’修改为对应的端口号,并将server-id 设置为2、3。
另外,需要将/etc/my.cnf删除。
3个实例的启动文件分别为:
/data/3306/mysql
/data/3307/mysql
/data/3308/mysql
启动文件的创建方式如下。
首先,拷贝安装包中的启动文件mysql.server,
cp /application/mysql/support-files/mysql.server /data/3306/mysql
接着,在文件开头添加MYSQL_HOME环境变量
export MYSQL_HOME=/data/3306
3307,3308的启动文件使用相同的方式进行创建。
将/data目录用户属主修改为mysql:
chown -R mysql.mysql /data
分别为实例3306,3307, 3308初始化数据库文件:
# cd /application/mysql/bin
[root@localhost bin]# ./mysqld --initialize --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
2020-01-28T08:00:35.203296Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-01-28T08:00:35.507051Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-01-28T08:00:35.559643Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-01-28T08:00:35.630885Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 43cf1658-41a4-11ea-9f90-080027974966.
2020-01-28T08:00:35.633731Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-01-28T08:00:35.634290Z 1 [Note] A temporary password is generated for root@localhost: H_Bia>K/Y2tt
[root@localhost bin]# ./mysqld --initialize --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
2020-01-28T08:06:53.599276Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-01-28T08:06:53.855428Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-01-28T08:06:53.895878Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-01-28T08:06:53.957149Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 254f18a1-41a5-11ea-be32-080027974966.
2020-01-28T08:06:53.960087Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-01-28T08:06:53.960716Z 1 [Note] A temporary password is generated for root@localhost: &Agc;sJX1MGy
[root@localhost bin]# ./mysqld --initialize --basedir=/application/mysql --datadir=/data/3308/data --user=mysql
2020-01-28T08:07:04.398172Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-01-28T08:07:04.647708Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-01-28T08:07:04.696848Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-01-28T08:07:04.757893Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 2bbf28c5-41a5-11ea-bf30-080027974966.
2020-01-28T08:07:04.760586Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-01-28T08:07:04.761052Z 1 [Note] A temporary password is generated for root@localhost: h0u:SPJSe8R*
初始化过程中,会创建临时密码,用于登录。
# /data/3306/mysql start
Starting MySQL. [ OK ]
查看服务是否启动成功
[root@localhost data]# ps -ef | grep mysql
root 17839 1 0 11:03 pts/2 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --datadir=/data/3306/data --pid-file=/data/3306/mysql.pid
mysql 18090 17839 2 11:03 pts/2 00:00:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/data/3306/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/data/3306/mysql_3306.err --pid-file=/data/3306/mysql.pid --socket=/data/3306/mysql.sock --port=3306
root 18133 12155 0 11:03 pts/2 00:00:00 grep mysql
[root@localhost data]#
[root@localhost data]# netstat -anp | grep 3306
tcp 0 0 :::3306 :::* LISTEN 18090/mysqld
unix 2 [ ACC ] STREAM LISTENING 69304 18090/mysqld /data/3306/mysql.sock
mysql服务启动后,重新设置密码
# /application/mysql/bin/mysqladmin -S /data/3306/mysql.sock -u root password 'mysql123456' -p
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
登录MySQL测试
# mysql -S /data/3306/mysql.sock -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
# /data/3306/mysql start
Starting MySQL.2020-01-28T02:33:58.935315Z mysqld_safe error: log-error set to '/data/3306/mysql_3306.err', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/data/3306/mysql[FAILED]
解决办法:
# touch /data/3306/mysql_3306.err
# chown -R mysql:mysql /data/3306/mysql_3306.err