一、前提條件是本地已經安裝了mysql,若是沒有安裝請參考https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html按步驟來進行安裝。安裝完以後的話,檢測一下啓動有無問題。若是沒問題的話,接下來執行第二步驟。html
二、修改/etc/my.cnf文件:內容覆蓋成以下mysql
[mysqld@replica01] datadir=/var/lib/mysql-replica01 socket=/var/lib/mysql-replica01/mysql.sock port=3307 log-error=/var/log/mysqld-replica01.log [mysqld@replica02] datadir=/var/lib/mysql-replica02 socket=/var/lib/mysql-replica02/mysql.sock port=3308 log-error=/var/log/mysqld-replica02.log
注意一點:此時尚未/var/lib/mysql-replica01/mysql.sock文件,也沒有/var/lib/mysql-replica02/mysql.sock文件。不要擔憂,只要你從新啓動mysql,它就會自動生成這個文件。linux
三、 這個時候,多實例已經配置成功了,執行如下命令讓多實例mysql啓動起來。sql
systemctl start mysqld@replica01 systemctl start mysqld@replica02
四、爲了能在系統啓動的時候,mysql能自動運行,須要進行以下配置bash
systemctl enable mysqld@replica01 systemctl enable mysqld@replica02
五、經過以下命令查看當前多實例mysql的運行狀態,可執行以下命令dom
systemctl status 'mysqld@replica*'
注意一下:系統命令systemctl支持通配符的形式。該命令執行的結果以下:socket
[root@localhost wuwenlun]# systemctl status 'mysqld@replica0*' ● mysqld@replica01.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld@.service; enabled; vendor preset: disabled) Active: failed (Result: start-limit) since Fri 2017-08-04 14:21:59 CST; 18s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 3085 ExecStart=/usr/sbin/mysqld --defaults-group-suffix=@%I --daemonize --pid-file=/var/run/mysqld/mysqld-%i.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE) Process: 3067 ExecStartPre=/usr/bin/mysqld_pre_systemd %I (code=exited, status=0/SUCCESS) Aug 04 14:21:59 localhost.localdomain systemd[1]: Failed to start MySQL Server. Aug 04 14:21:59 localhost.localdomain systemd[1]: Unit mysqld@replica01.service entered failed state. Aug 04 14:21:59 localhost.localdomain systemd[1]: mysqld@replica01.service failed. Aug 04 14:21:59 localhost.localdomain systemd[1]: mysqld@replica01.service holdoff time over, scheduling restart. Aug 04 14:21:59 localhost.localdomain systemd[1]: start request repeated too quickly for mysqld@replica01.service Aug 04 14:21:59 localhost.localdomain systemd[1]: Failed to start MySQL Server. Aug 04 14:21:59 localhost.localdomain systemd[1]: Unit mysqld@replica01.service entered failed state. Aug 04 14:21:59 localhost.localdomain systemd[1]: mysqld@replica01.service failed. ● mysqld@replica02.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld@.service; enabled; vendor preset: disabled) Active: failed (Result: start-limit) since Fri 2017-08-04 14:22:00 CST; 17s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 3120 ExecStart=/usr/sbin/mysqld --defaults-group-suffix=@%I --daemonize --pid-file=/var/run/mysqld/mysqld-%i.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE) Process: 3102 ExecStartPre=/usr/bin/mysqld_pre_systemd %I (code=exited, status=0/SUCCESS) Aug 04 14:22:00 localhost.localdomain systemd[1]: Failed to start MySQL Server. Aug 04 14:22:00 localhost.localdomain systemd[1]: Unit mysqld@replica02.service entered failed state. Aug 04 14:22:00 localhost.localdomain systemd[1]: mysqld@replica02.service failed. Aug 04 14:22:00 localhost.localdomain systemd[1]: mysqld@replica02.service holdoff time over, scheduling restart. Aug 04 14:22:00 localhost.localdomain systemd[1]: start request repeated too quickly for mysqld@replica02.service Aug 04 14:22:00 localhost.localdomain systemd[1]: Failed to start MySQL Server. Aug 04 14:22:00 localhost.localdomain systemd[1]: Unit mysqld@replica02.service entered failed state. Aug 04 14:22:00 localhost.localdomain systemd[1]: mysqld@replica02.service failed.
很遺憾,執行結果報錯,多實例myql都沒有起來。重啓系統也是如此,那麼此時先不要慌,去查看日誌先。進入/var/log/文件夾,此時你會看到 mysqld-replica01.log mysqld-replica02.log這兩個文件,隨便選取一個文件打開,可看到以下報錯信息:ui
2017-08-04T06:21:58.947677Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2017-08-04T06:21:58.947706Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2017-08-04T06:21:58.947712Z 0 [ERROR] InnoDB: os_file_get_status() failed on './ibdata1'. Can't determine file permissions 2017-08-04T06:21:58.947719Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error 2017-08-04T06:21:59.550446Z 0 [ERROR] Plugin 'InnoDB' init function returned error. 2017-08-04T06:21:59.550491Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 2017-08-04T06:21:59.550496Z 0 [ERROR] Failed to initialize plugins. 2017-08-04T06:21:59.550498Z 0 [ERROR] Aborting
說實話看到這鬼,我也不知道是啥東西,只知道沒有權限。後來上網查了一下,只提供瞭解決方案,沒有指明具體緣由。rest
六、vi /etc/selinux/config修改文件爲永久關閉日誌
# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
SELINUX=permissive #修改此參數爲permissive 。
七、修改完以後重啓一下,執行systemctl status 'mysqld@replica0*',得以下結果,2個mysql實例已經完美啓動了。
[root@localhost wuwenlun]# systemctl status 'mysqld@replica0*' ● mysqld@replica01.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld@.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2017-08-04 14:44:25 CST; 1min 3s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 1107 ExecStart=/usr/sbin/mysqld --defaults-group-suffix=@%I --daemonize --pid-file=/var/run/mysqld/mysqld-%i.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 981 ExecStartPre=/usr/bin/mysqld_pre_systemd %I (code=exited, status=0/SUCCESS) Main PID: 1161 (mysqld) CGroup: /system.slice/system-mysqld.slice/mysqld@replica01.service └─1161 /usr/sbin/mysqld --defaults-group-suffix=@replica01 --daemonize --pid-file=/var/run/mysqld/mysqld-replica01.pid Aug 04 14:44:22 localhost.localdomain systemd[1]: Starting MySQL Server... Aug 04 14:44:25 localhost.localdomain systemd[1]: Started MySQL Server. ● mysqld@replica02.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld@.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2017-08-04 14:44:25 CST; 1min 3s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 1095 ExecStart=/usr/sbin/mysqld --defaults-group-suffix=@%I --daemonize --pid-file=/var/run/mysqld/mysqld-%i.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 973 ExecStartPre=/usr/bin/mysqld_pre_systemd %I (code=exited, status=0/SUCCESS) Main PID: 1159 (mysqld) CGroup: /system.slice/system-mysqld.slice/mysqld@replica02.service └─1159 /usr/sbin/mysqld --defaults-group-suffix=@replica02 --daemonize --pid-file=/var/run/mysqld/mysqld-replica02.pid Aug 04 14:44:22 localhost.localdomain systemd[1]: Starting MySQL Server... Aug 04 14:44:25 localhost.localdomain systemd[1]: Started MySQL Server.
八、可經過以下命令進入指定的mysql
mysql -uroot -p -S /var/lib/mysql-replica01/mysql.sock mysql -uroot -p -S /var/lib/mysql-replica02/mysql.sock
九、這個時候,你沒有密碼進入不了。那麼執行以下命令可獲得mysql安裝的時候隨機生成的一個初始密碼
sudo grep 'temporary password' /var/log/mysqld-replica01.log sudo grep 'temporary password' /var/log/mysqld-replica02.log