Linux中執行多個MySQL例項
這裡將介紹使用mysqld_safe來啟動多個MySQL例項。對於使用一個RPM釋出版本安裝的MySQL來說,在多數Linux平臺中MySQL服務的啟動和關閉是由systemd來管理的。在這些平臺上沒有安裝msyqld_safe因為不需要。
一種在Linux上執行多個MySQL例項的方法是用不同的預設TCP/IP埠和Unix套接字檔案編譯不同的伺服器,以便每個伺服器都監聽不同的網路介面。為了在每個不同的基本目錄中進行編譯,還會自動生成一個單獨的、編譯後的資料目錄、日誌檔案和每個伺服器的PID檔案位置。
假設一個現有的5.6伺服器被配置成TCP/IP埠為3306並且Unix socket檔案為/tmp/mysql.sock。為了配置一個新的5.7.21伺服器使用不同的操作引數,使用CMake命令進行編譯:
shell> cmake . -DMYSQL_TCP_PORT=port_number \
-DMYSQL_UNIX_ADDR=file_name \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.21
這裡,port_number和file_name必須與預設的TCP/CP埠號和Unix socket檔案路徑名不同,並且CMAKE_INSTALL_PREFIX值指定的安裝目錄不能是現有MySQL安裝目錄相同的目錄。
如果有一個MySQL伺服器正在監聽一個指定的埠號,你可以使用下面的命令來找出多個重要配置變數所使用的操作引數,包括base目錄和Unix socket檔名:
shell> mysqladmin --host=host_name --port=port_number variables
透過命令所顯示的資訊,你也可以知道當配置另一個伺服器時什麼選項值將不能使用。
如果指定localhost作為主機名,mysqladmin預設會使用一個Unix socket檔案來進行連線而不是使用TCP/IP。為了顯性指定連線協議,使用--protocol={TCP|SOCKET|PIPE|MEMORY}選項。
如果只是使用不同的Unix socket檔案和TCP/IP埠來啟動一個MySQL例項那麼不需要編譯一個新的MySQL伺服器。可以使用相同的伺服器二進位制檔案並且在執行時為每個MySQL例項使用不同的引數。一種方式是使用命令列選項:
shell>mysqld_safe --socket=file_name --port=port_number
為了啟動第二個MySQL例項,給mysqld_safe提供不同的--socket和--port選項值並傳一個--datadir=dir_name選項因此這個例項將使用不同的資料目錄。
另一種方法是將每個MySQL例項的選項放入不同的選項檔案,然後啟動每個例項時使用--defaults-file選項來指定合適選項檔案的路徑。
shell> mysqld_safe --defaults-file=/usr/local/mysql/my.cnf shell> mysqld_safe --defaults-file=/usr/local/mysql/my.cnf2
另一種方法來完成相同的功能是使用環境變數來設定Unix socket檔名和TCP/IP埠號:
shell> MYSQL_UNIX_PORT=/tmp/mysqld-new.sock shell> MYSQL_TCP_PORT=3307 shell> export MYSQL_UNIX_PORT MYSQL_TCP_PORT shell> mysql_install_db --user=mysql shell> mysqld_safe --datadir=/path/to/datadir &
這是一種快速啟動第二個例項進行測試的方法。它的好處是設定的環境變數可以應用到從相同shell執行呼叫的任何客戶端程式。因此對於這些客戶端連線會自動指向第二例項。
另一種方法是在Linux中使用mysqld_multi指令碼來管理多個MySQL例項。
下面來建立三個例項(例項的埠號為3307,3308,3309)
建立儲存這三個例項的資料庫檔案目錄
-bash-4.2$ mkdir mysql3307 -bash-4.2$ mkdir mysql3308 -bash-4.2$ mkdir mysql3309 -bash-4.2$ ls -lrt 總用量 4 drwxrwxrwx. 5 mysql mysql 4096 8月 22 21:46 mysql drwxr-xr-x. 2 mysql mysql 6 9月 4 10:26 mysql3307 drwxr-xr-x. 2 mysql mysql 6 9月 4 10:26 mysql3308 drwxr-xr-x. 2 mysql mysql 6 9月 4 10:26 mysql3309
為每個例項建立一個選項檔案(my3307.cnf,my3308.cnf,my3309.cnf)
my3307.cnf檔案內容如下:
-bash-4.2$ cat my3307.cnf [mysqld] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql3307 bind-address=0.0.0.0 user=mysql port=3307 log-error=/mysqldata/mysql3307/mysql.err pid-file=/mysqldata/mysql3307/mysqld.pid socket = /mysqldata/mysql3307/mysql.sock character-set-server=utf8mb4 default-storage-engine=INNODB explicit_defaults_for_timestamp = true
my3308.cnf檔案內容如下:
-bash-4.2$ cat my3308.cnf [mysqld] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql3308 bind-address=0.0.0.0 user=mysql port=3308 log-error=/mysqldata/mysql3308/mysql.err pid-file=/mysqldata/mysql3308/mysqld.pid socket = /mysqldata/mysql3308/mysql.sock character-set-server=utf8mb4 default-storage-engine=INNODB explicit_defaults_for_timestamp = true
my3309.cnf檔案內容如下:
-bash-4.2$ cat my3309.cnf [mysqld] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql3309 bind-address=0.0.0.0 user=mysql port=3309 log-error=/mysqldata/mysql3309/mysql.err pid-file=/mysqldata/mysql3309/mysqld.pid socket = /mysqldata/mysql3309/mysql.sock character-set-server=utf8mb4 default-storage-engine=INNODB explicit_defaults_for_timestamp = true
初始化資料庫
-bash-4.2$ mysqld --defaults-file=/mysqlsoft/mysql/my3307.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql3307 --user=mysql -bash-4.2$ mysqld --defaults-file=/mysqlsoft/mysql/my3308.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql3308 --user=mysql -bash-4.2$ mysqld --defaults-file=/mysqlsoft/mysql/my3309.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql3309 --user=mysql
啟動資料庫
-bash-4.2$ mysqld_safe --defaults-file=/mysqlsoft/mysql/my3307.cnf & [1] 10359 -bash-4.2$ 2019-09-05T09:39:35.467416Z mysqld_safe Logging to '/mysqldata/mysql3307/mysql.err'. 2019-09-05T09:39:35.545107Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3307 -bash-4.2$ mysqld_safe --defaults-file=/mysqlsoft/mysql/my3308.cnf & [1] 10624 -bash-4.2$ 2019-09-05T09:42:28.457387Z mysqld_safe Logging to '/mysqldata/mysql3308/mysql.err'. 2019-09-05T09:42:28.532350Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3308 -bash-4.2$ mysqld_safe --defaults-file=/mysqlsoft/mysql/my3309.cnf & [1] 10889 -bash-4.2$ 2019-09-05T09:45:03.772185Z mysqld_safe Logging to '/mysqldata/mysql3309/mysql.err'. 2019-09-05T09:45:03.847584Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3309
修改每個MySQL例項的使用者密碼並關閉例項
-bash-4.2$ mysql --port=3307 --host=127.0.0.1 --user=root --password=nCohVRg-=7LP 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 3 Server version: 5.7.26 Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye -bash-4.2$ mysqladmin --port=3307 --host=127.0.0.1 --user=root --password=123456 shutdown mysqladmin: [Warning] Using a password on the command line interface can be insecure. -bash-4.2$ mysql --port=3308 --host=127.0.0.1 --user=root --password=g*tV/I%#s6j# 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 2 Server version: 5.7.26 Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye -bash-4.2$ mysqladmin --port=3308 --host=127.0.0.1 --user=root --password=123456 shutdown mysqladmin: [Warning] Using a password on the command line interface can be insecure. -bash-4.2$ mysql --port=3309 --host=127.0.0.1 --user=root --password=eIsXkThGK5*4 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 2 Server version: 5.7.26 Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye -bash-4.2$ mysqladmin --port=3309 --host=127.0.0.1 --user=root --password=123456 shutdown mysqladmin: [Warning] Using a password on the command line interface can be insecure.
使用mysqld_multi來管理多個MySQL例項
修改配置檔案my.cnf增加以下內容(增加三個例項的選項引數)
[mysqld_multi] mysqld=/mysqlsoft/mysql/bin/mysqld_safe mysqladmin =/mysqlsoft/mysql/bin/mysqladmin log =/mysqlsoft/mysql/mysqld_multi.log [mysqld1] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql3307 bind-address=0.0.0.0 user=mysql port=3307 log-error=/mysqldata/mysql3307/mysql.err pid-file=/mysqldata/mysql3307/mysqld.pid socket = /mysqldata/mysql3307/mysql.sock character-set-server=utf8mb4 default-storage-engine=INNODB explicit_defaults_for_timestamp = true [mysqld2] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql3308 bind-address=0.0.0.0 user=mysql port=3308 log-error=/mysqldata/mysql3308/mysql.err pid-file=/mysqldata/mysql3308/mysqld.pid socket = /mysqldata/mysql3308/mysql.sock character-set-server=utf8mb4 default-storage-engine=INNODB explicit_defaults_for_timestamp = true [mysqld3] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql3309 bind-address=0.0.0.0 user=mysql port=3309 log-error=/mysqldata/mysql3309/mysql.err pid-file=/mysqldata/mysql3309/mysqld.pid socket = /mysqldata/mysql3309/mysql.sock character-set-server=utf8mb4 default-storage-engine=INNODB explicit_defaults_for_timestamp = true
使用mysqld_multi來啟動例項
[mysql@localhost ~]$ mysqld_multi start 1
報錯了,從日誌可以看到啟動例項時呼叫了兩次,我這裡的測試環境在建立多個例項之前建立了一個mysqld例項(也許存在影響)
[mysql@localhost mysql]$ tail -f mysqld_multi.log Starting MySQL servers 2019-09-06T05:40:07.558168Z mysqld_safe Logging to '/mysqldata/mysql3307/mysql.err'. 2019-09-06T05:40:07.563783Z mysqld_safe Logging to '/mysqldata/mysql3307/mysql.err'. 2019-09-06T05:40:07.618543Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3307 2019-09-06T05:40:07.623821Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3307 root@localhost mysql3307]# tail -f mysql.err 2019-09-06T06:24:30.964335Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files. 2019-09-06T06:24:31.964485Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11 2019-09-06T06:24:31.964573Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files. 2019-09-06T06:24:32.964723Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11 2019-09-06T06:24:32.964812Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files. 2019-09-06T06:24:33.964935Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11 2019-09-06T06:24:33.964987Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files. 2019-09-06T06:24:34.965105Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11 2019-09-06T06:24:34.965178Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files. 2019-09-06T06:24:35.965292Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11 2019-09-06T06:24:35.965340Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files. 2019-09-06T06:24:36.965460Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11 2019-09-06T06:24:36.965509Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files. 2019-09-06T06:24:37.965632Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
所以這裡在使用mysqld_multi來啟動例項時使用--defaults-file
[mysql@localhost bin]$ mysqld_multi --defaults-file=/mysqlsoft/mysql/my.cnf start 1 [mysql@localhost bin]$ mysqld_multi --defaults-file=/mysqlsoft/mysql/my.cnf start 2,3 [mysql@localhost mysql]$ tail -f mysqld_multi.log Starting MySQL servers 2019-09-06T06:49:50.003877Z mysqld_safe Logging to '/mysqldata/mysql3307/mysql.err'. 2019-09-06T06:49:50.096954Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3307 mysqld_multi log file version 2.16; run: 五 9月 6 14:59:33 2019 Starting MySQL servers 2019-09-06T06:59:33.644263Z mysqld_safe Logging to '/mysqldata/mysql3308/mysql.err'. 2019-09-06T06:59:33.650226Z mysqld_safe Logging to '/mysqldata/mysql3309/mysql.err'. 2019-09-06T06:59:33.704593Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3308 2019-09-06T06:59:33.710937Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3309
到此使用mysqld_multi來管理多個例項的操作就完成了。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26015009/viewspace-2661917/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在Windows中執行多個MySQL例項WindowsMySql
- java多執行緒例項Java執行緒
- Qt中的多執行緒與執行緒池淺析+例項QT執行緒
- Java多執行緒2:Thread中的例項方法Java執行緒thread
- mysqld_multi安裝多個mysql例項MySql
- mysqld_multi啟動多個mysql例項MySql
- dddsample一個可執行的例項
- linux執行緒池簡單例項Linux執行緒單例
- c++11多執行緒入門例項C++執行緒
- C#多執行緒程式設計例項C#執行緒程式設計
- 一個例項中,多個synchronized方法的呼叫synchronized
- mysql搭建多個例項(mysqld_multi的使用)MySql
- Win10 Redstone 4允許執行多個UWP應用程式例項Win10
- MySQL在Windows上安裝多個例項的方法MySqlWindows
- 一臺MySQL資料庫啟動多個例項MySql資料庫
- docker redis 多個例項DockerRedis
- Linux中的15個‘echo’ 命令例項Linux
- 【RAC】rac中如何指定job的執行例項
- 【多執行緒與高併發3】常用鎖例項執行緒
- Linux—一臺主機部署多版本mysql服務和mysql例項LinuxMySql
- 掌握C#中的GUI多執行緒技巧:WinForms和WPF例項詳解C#GUI執行緒ORM
- pytest(13)-多執行緒、多程式執行用例執行緒
- 多個mapreduce連線例項
- 在同一臺計算機中執行多個MySQL服務計算機MySql
- 使用Portainer集中管理多地域內網執行的Docker例項AI內網Docker
- Python3多執行緒爬蟲例項講解Python執行緒爬蟲
- Python多執行緒非同步任務佇列(例項)Python執行緒非同步佇列
- 多執行緒併發鎖分類以及簡單例項執行緒單例
- pytest多程式/多執行緒執行測試用例執行緒
- Activiti的流程例項【ProcessInstance】與執行例項【Execution】
- oracle一個例項配置多個listener或多個埠Oracle
- mysql一次執行多個SQL檔案MySql
- java多執行緒結合單例模式例項,簡單實用易理解Java執行緒單例模式
- oracle 多個例項監聽不到Oracle
- oracle多個例項啟動方法Oracle
- 應用程式每次只能執行一個例項(C#)C#
- 僅允許程式一個例項執行VC6
- linux自動automatic啟動停止多個oracle例項LinuxOracle