mysql 使用mysqld_multi在一臺機器上安裝多個mysql實例(多實例安裝)

https://dev.mysql.com/doc/refman/5.7/en/mysqld-multi.html

多實例安裝即是一臺服務器安裝多個mysql實例;這樣可以充分利用硬件資源;通過mysqld_multi程序即可。

例如我們現在有一臺配置較高的機器:32C,512G,8SSD做RIAND5,而4C 8G纔是主流服務器配置。這麼一臺服務器僅僅跑一個mysql是非常奢侈的。現在我們就可以在這臺機器上同時部署三個mysql實例:mysqld3306、mysqld3307、mysqld3308。

mysqld_multi配置

1、配置mysqld_multi節點;mysqld_safe命令啓動mysql、mysqladmin停止mysql、mysqld_multi.log日誌(有問題都可以到這裏排查)。關於mysqld_safe、mysqladmin見 https://www.jianshu.com/p/a5f24d0f736e。當然這裏使用mysqld=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqd也是可以的,只不過mysqld沒有守護的功能。
2、配置三個實例:mysqld3306、mysqld3307、mysqld3308;分別指定port、datadir、socket。
3、此時的[mysqld]節點下的配置是mysqld3306、mysqld3307、mysqld3308三個實例所共享的。
4、[client] 節點配置了默認客戶端連接參數。我這裏配置了root用戶、密碼還有指定socket爲/tmp/mysql.sock3306。則隨後可以直接使用mysql命令連接到mysql3306實例而不需要額外指定參數。

[mysqld]
user = mysql
basedir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64
log_error=error.log
plugin-load=validate_password.so

[mysqld_multi]
mysqld=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld_safe
mysqladmin=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqladmin
log=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/mysqld_multi.log

[mysqld3306]
port=3306
datadir=/mdata/mysql57-3306
socket= /tmp/mysql.sock3306

[mysqld3307]
port=3307
datadir=/mdata/mysql57-3307
socket= /tmp/mysql.sock3307

[mysqld3308]
port=3308
datadir=/mdata/mysql57-3308
socket= /tmp/mysql.sock3308

[client]
user=root
password=1111aaA_
socket= /tmp/mysql.sock3306
[mysql]
prompt=(\\u@\\h) [\\d]>\\

啓動過程

1、以mysqld3306實例爲例,初始化數據如下。注意指定datadir。

[root@localhost bin]# ./mysqld --initialize --datadir=/mdata/mysql57-3306

注意:[mysqld]下配置了plugin-load=validate_password.so 密碼插件的話就不能使用--initialize-insecure,請使用--initialize。否則初始化後密碼是空的,然後連不上。

mysqld3306實例生成密碼如下:

[root@localhost mysql57-3306]# cat error.log 
2021-05-01T03:22:15.245117Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-01T03:22:16.562503Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-01T03:22:16.692390Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-01T03:22:16.750221Z 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: 6e1b5ce4-aa2c-11eb-befe-000c292882e9.
2021-05-01T03:22:16.751331Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-01T03:22:17.412496Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-01T03:22:17.657496Z 1 [Note] A temporary password is generated for root@localhost: Ye-7i!4-gtw)

2、使用mysqld_multi 命令啓動實例,start後面跟的3306和[mysqld3306]中的3306對應起來,這裏可以使用任何名字。如[mysqld1]就這樣啓動:./mysqld_multi start 1。

[root@localhost bin]# ./mysqld_multi start 3306

3、查看狀態

[root@localhost bin]# ./mysqld_multi report 3306
Reporting MySQL servers
MySQL server from group: mysqld3306 is running

4、查看進程。如下啓動mysqld3306 實例後就出現了兩個進程:mysqld_safe 和 mysqld 。

[root@localhost bin]# ps -ef |grep mysql
root      11675      1  0 10:55 pts/0    00:00:00 /bin/sh /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld_safe --port=3306 --datadir=/mdata/mysql57-3306 --socket=/tmp/mysql.sock3306
mysql     11827  11675  2 10:55 pts/0    00:00:00 /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld --basedir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64 --datadir=/mdata/mysql57-3306 --plugin-dir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock3306 --port=3306
root      11874   7035  0 10:55 pts/0    00:00:00 grep --color=auto mysql

5、繼續初始化和啓動其它進程。
./mysqld_multi start 3307
./mysqld_multi start 3308

6、最終的mysql相關進程有6個。如下,分別3個mysqld、3個mysqld_safe 。

[root@localhost bin]# ps -ef |grep mysql
root      11675      1  0 10:55 pts/0    00:00:00 /bin/sh /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld_safe --port=3306 --datadir=/mdata/mysql57-3306 --socket=/tmp/mysql.sock3306
mysql     11827  11675  0 10:55 pts/0    00:00:00 /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld --basedir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64 --datadir=/mdata/mysql57-3306 --plugin-dir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock3306 --port=3306
root      11948      1  0 10:56 pts/0    00:00:00 /bin/sh /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld_safe --port=3307 --datadir=/mdata/mysql57-3307 --socket=/tmp/mysql.sock3307
mysql     12104  11948  2 10:56 pts/0    00:00:00 /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld --basedir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64 --datadir=/mdata/mysql57-3307 --plugin-dir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock3307 --port=3307
root      12199      1  0 10:57 pts/0    00:00:00 /bin/sh /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld_safe --port=3308 --datadir=/mdata/mysql57-3308 --socket=/tmp/mysql.sock3308
mysql     12352  12199  7 10:57 pts/0    00:00:00 /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld --basedir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64 --datadir=/mdata/mysql57-3308 --plugin-dir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock3308 --port=3308
root      12398   7035  0 10:57 pts/0    00:00:00 grep --color=auto mysql

7、 netstat -anl 看下端口。3306、3307、3308 都被佔用了。

[root@localhost tmp]# netstat -anl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 192.168.6.128:22        192.168.6.1:1042        ESTABLISHED
tcp        0      0 192.168.6.128:22        192.168.6.1:1410        ESTABLISHED
tcp        0      0 192.168.6.128:22        192.168.6.1:2135        ESTABLISHED
tcp        0      0 192.168.6.128:22        192.168.6.1:1043        ESTABLISHED
tcp6       0      0 :::3306                 :::*                    LISTEN     
tcp6       0      0 :::3307                 :::*                    LISTEN     
tcp6       0      0 :::3308                 :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp6       0      0 ::1:323                 :::*                               
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     75933    /tmp/mysql.sock3306
unix  3      [ ]         DGRAM                    11503    /run/systemd/notify
unix  2      [ ACC ]     STREAM     LISTENING     48932    /tmp/mysql.sock3307
unix  2      [ ]         DGRAM                    11505    /run/systemd/cgroups-agent
unix  2      [ ACC ]     STREAM     LISTENING     19191    /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     76809    /tmp/mysql.sock3308
unix  2      [ ]         DGRAM                    33496    

8、連接後修改密碼。
使用sock連接。如mysql3306則指定/tmp/mysql.sock3306。
並set password = '1111aaA_'; 修改當前用戶密碼。

[root@localhost bin]# ./mysql -S /tmp/mysql.sock3306 -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost bin]# ./mysql -S /tmp/mysql.sock3306 -pYe-7i!4-gtw)
-bash: !4: event not found
[root@localhost bin]# ./mysql -S /tmp/mysql.sock3306 -p'Ye-7i!4-gtw)'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.33

Copyright (c) 2000, 2021, 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.

(root@localhost) [(none)]>set password = '1111aaA_';

9、隨後可以直接輸入mysql登錄了。

[root@localhost /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.33 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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.

(root@localhost) [(none)]>\s
--------------
mysql  Ver 14.14 Distrib 5.7.33, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:      9
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.7.33 MySQL Community Server (GPL)
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /tmp/mysql.sock3306
Uptime:         14 min 19 sec

Threads: 2  Questions: 22  Slow queries: 0  Opens: 106  Flush tables: 1  Open tables: 99  Queries per second avg: 0.025
--------------


拓展:
1、同理,其它的mysql3307、mysql3308也是一樣的操作。另外想要直接使用mysql -S /tmp/mysql.sock3307、mysql -S /tmp/mysql.sock3308 直接登錄。那麼需要將其它兩個實例的密碼都改爲1111aaA_。

2、有人喜歡使用mysqld –defaults-file=/etc/my.cnf 的defaults-file參數來指定特定的my.cnf配置文件來實現多實例安裝。但是這樣的話實例一多就不好管理了。所以推薦使用mysqld_multi。

3、多實例IO競爭問題怎麼解決?
對於資源管理、資源調度問題可以通過操作系統層面的技術解決。
LXC
docker
Cgroup

4、MHA、keepalive 這些集羣軟件請不要使用多實例安裝,這種高可用軟件肯定是要部署到多個機器上。

5、每次初始化會不會對已經存在的實例有影響?
初始化實例時請指定datadir:
./mysqld --initialize --datadir=/mdata/mysql57-3306
當然不指定的話,就算原來目錄已經存在數據。那麼此次執行會失敗的。並不會對已經存在的數據目錄產生影響。

[root@localhost bin]# ./mysqld --initialize --datadir=/mdata/mysql57-3308
2021-05-01T04:18:01.028139Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-01T04:18:01.029870Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2021-05-01T04:18:01.029901Z 0 [ERROR] Aborting

6、使用mysqld_multi stop 3306 停止實例時可能會失敗,因爲實際上是去調用mysqladmin來停止,mysqladmin會去讀取[client]下的用戶名和密碼 ,若不符合自然是關閉不了。可以選擇使用kill -0 pid來停止,或者去修改[client]配置:

[client]
user=root
password=1111aaA_
socket= /tmp/mysql.sock3306

隨後關閉是成功的。

[root@localhost bin]# ./mysqld_multi stop 3307
[root@localhost bin]# ./mysqld_multi report 3307
Reporting MySQL servers
MySQL server from group: mysqld3307 is running

另外有一點一定要注意下,mysql官方文檔是把登錄用戶名密碼配置在[mysqld_multi]節點下而不是[client]節點下,當然這樣也可以。

[mysqld_multi]
mysqld=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld_safe
mysqladmin=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqladmin
log=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/mysqld_multi.log
user=root
password=1111aaA_

這裏有人把用戶名密碼放到[mysqld_multi] 後執行mysqld_multi stop 關不掉了。嘗試把password改爲pass就行了。當然我沒有遇到這個問題,我照樣使用password還能正常關閉。

7、選擇單機多實例,還是建立多個虛擬機?
這就是不同的資源調度的方式。
像淘寶的RDS雲數據庫就是使用LXC Container 來進行資源隔離的,其實就是安裝多個實例,這種方式性能會更好。當然虛擬機也有它的好處。

8、一個實例建立多個數據庫。或者多個數據庫分別放到不同實例下。
這個還是得看業務。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章