單機安裝MySQL多實例

很多時候受限於資源限制,需要在單機上安裝多個MySQL實例。
本文基於MySQL 5.7.27,介紹如何在單機環境下安裝多個MySQL實例。

下文中的方案,建立了3個實例,端口分別爲3306, 3307, 3308。

1.創建MySQL用戶賬號

首先創建mysql用戶組和用戶。
並設置不允許登錄。

# groupadd mysql
# useradd -s /sbin/nologin -g mysql -M mysql
  • -s/sbin/nologin表示禁止該用戶登錄,只需要角色存在即可,加強安全。
  • -g mysql指定mysql用戶屬於mysql組。
  • -M表示不創建用戶家目錄。

2.安裝

2.1下載安裝文件

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

版本 5.7.27
二進制安裝文件 mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz

2.2 解壓安裝文件

解壓二進制文件,移動到指定目錄,並創建軟鏈接。

[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/作爲安裝目錄。

2.3 創建數據文件目錄

創建3個數據文件目錄:

mkdir -p /data/{3306,3307,3308}/data

2.4 創建配置文件

創建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刪除。

2.5 創建啓動文件

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的啓動文件使用相同的方式進行創建。

2.6 修改文件夾權限

將/data目錄用戶屬主修改爲mysql:

chown -R mysql.mysql /data

2.7 初始化數據庫文件

分別爲實例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*

初始化過程中,會創建臨時密碼,用於登錄。

2.8 啓動數據庫

# /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)

3.問題

# /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

4.參考

mysql 配置優化
MySQL 5.7 my.cnf配置文件說明
MySQL5.7官方安裝文檔

發佈了471 篇原創文章 · 獲贊 170 · 訪問量 208萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章