mysqld_multi安裝多個mysql例項

花花蘑菇發表於2016-12-27

一個機器上安裝多個mysql例項,除了將每個例項在不同的目錄下編譯安裝,為每個例項指定不同的埠,socket,配置檔案,安裝目錄等。還可以使用mysqld_multi方式。

mysqld_multimysql設計的專門用來管理不同埠,不同socketmysql例項的工具。它可以同時啟動,停止多個mysql例項,也可以檢視這些例項的狀態。

mysqld_multi會去配置檔案my.cnf(或者是—defaults-file引數指定的配置檔案)中檢查每一個以[mysqldN]為開頭的一組配置(N這裡是數字)。這個Nmysqld_multi用來區分每一個mysql例項用的。用法如下:

mysqld_multi start|stop|reload|report N1, N2, N3,…   //reload等價於stopstart

mysqld_multi需要的資訊記錄在配置檔案my.cnf中的[mysqld_multi]組下。

注意,為了能用mysqld_multi統計管理所有的mysql例項,該管理賬號必須存在於所有的mysql例項上,而且密碼也一致。

 

以下是我做的測試以及安裝步驟:

1、 編寫配置檔案my.cnf,如下:

[root@CentOSMysql1 etc]# cat my.cnf

[mysqld_multi]

mysqld=mysqld_safe

#表示用mysqld_safe來啟動mysql

mysqladmin=/usr/local/mysql/bin/mysqladmin

#指定mysqladmin工具的路徑

log=/usr/local/mysql/mysqld_multi.log

#指定mysqld_multi的日誌檔案

user=root

pass=123456

#指定使用mysqld_multi工具的使用者和密碼

 

[mysqld3307]

#指定例項編號為3307的一些配置引數

datadir=/opt/data3307

port=3307

socket=/tmp/mysql.sock3307

 

[mysqld3308]

#指定例項編號為3308的一些配置引數

datadir=/opt/data3308

port=3308

socket=/tmp/mysql.sock3308

 

[mysqld56]

#指定例項編號為56的一些配置引數

basedir=/usr/local/mysql56/

datadir=/opt/data56

socket=/tmp/mysql.sock56

port=3310

 

2、 準備安裝之前工作

(1)檢視是否有mysql使用者和mysql

[root@CentOSMysql1 etc]# cat group |grep mysql

[root@CentOSMysql1 etc]# groupadd mysql

[root@CentOSMysql1 mysql]# cat /etc/passwd|grep mysql

[root@CentOSMysql1 etc]# useradd -g mysql mysql

(2)下載mysql的安裝檔案。這裡我下載的都是linux generic型別的

[root@CentOStest1 opt]# wget

[root@CentOStest1 opt]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

 (3)解壓這些安裝包

[root@CentOSMysql1 opt]# tar -zxf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz

[root@CentOSMysql1 opt]# tar -zxf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz

 

3、 安裝編號為3307mysql例項,這裡我計算用mysql5.7的版本。

[root@CentOSMysql1 opt]#mv mysql-5.7.16-linux-glibc2.5-x86_64 /usr/local

[root@CentOSMysql1 opt]#cd /usr/local/

[root@CentOSMysql1 local]#ln –s mysql-5.7.16-linux-glibc2.5-x86_64 mysql

[root@CentOSMysql1 local]# cd mysql

[root@CentOSMysql1 mysql]# chown -R mysql:mysql .

[root@CentOSMysql1 mysql]# bin/mysqld --initialize --user=mysql --datadir=/opt/data3307

//這條命令執行成功之後,會在結尾列印出臨時密碼,如下:

2017-12-21T02:08:32.598340Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-12-21T02:08:33.002195Z 0 [Warning] InnoDB: New log files created, LSN=45790

2017-12-21T02:08:33.090315Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2017-12-21T02:08:33.179548Z 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: d898cae9-e5f3-11e7-9e66-000c299323ae.

2017-12-21T02:08:33.181477Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2017-12-21T02:08:33.184759Z 1 [Note] A temporary password is generated for root@localhost: L;0/g:XaweYi

[root@CentOSMysql1 mysql]# bin/mysql_ssl_rsa_setup  --datadir=/opt/data3307

到這裡,編號為3307的例項初步安裝完畢,還剩下一個步驟就是修改臨時密碼。改密碼之前需要把其所在mysql服務啟動起來,這裡我們用mysqld_multi啟動,先檢視下狀態,如下:

[root@CentOSMysql1 bin]# mysqld_multi report

-bash: mysqld_multi: command not found

解決方法為將mysqld_multi工具所在的路徑新增到環境變數中去:

[root@CentOSMysql1 bin]# vi /etc/profile

//在檔案尾部新增以下語句

PATH=/usr/local/mysql/bin:$PATH

"/etc/profile" 79L, 1828C written

[root@CentOSMysql1 bin]# source /etc/profile

環境變數生效之後重試

[root@CentOSMysql1 mysql]# mysqld_multi report

Reporting MySQL servers

MySQL server from group: mysqld3307 is not running

MySQL server from group: mysqld3308 is not running

MySQL server from group: mysqld56 is not running

[root@CentOSMysql1 mysql]# mysqld_multi start 3307

[root@CentOSMysql1 mysql]# mysqld_multi report

Reporting MySQL servers

MySQL server from group: mysqld3307 is running

MySQL server from group: mysqld3308 is not running

MySQL server from group: mysqld56 is not running

可以看到編號3307mysql例項已經啟動,然後修改root密碼。因為my.cnf裡面配置了root使用者的密碼是123456,所以現在我們將root的密碼改為“123456”:

[root@CentOSMysql1 mysql]# mysql -uroot -p -S /tmp/mysql.sock3307

Enter password:

//這裡輸出剛才安裝時出現的臨時密碼。

mysql> set password=password("123456");

到這裡,編號為3307的例項安裝完畢。

 

4、 安裝編號為3308mysql例項,這裡仍用mysql5.7的版本,但是埠號以及datadir要根據my.cnf中的來。

[root@CentOSMysql1 mysql]# bin/mysqld --initialize --user=mysql --datadir=/opt/data3308

2017-12-21T02:36:31.294011Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-12-21T02:36:31.779696Z 0 [Warning] InnoDB: New log files created, LSN=45790

2017-12-21T02:36:31.881428Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2017-12-21T02:36:31.948774Z 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: c138a63c-e5f7-11e7-8f7a-000c299323ae.

2017-12-21T02:36:31.950062Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2017-12-21T02:36:31.953758Z 1 [Note] A temporary password is generated for root@localhost: whKyXCtxz9;g

//臨時密碼同樣在尾處生成。

[root@CentOSMysql1 mysql]# bin/mysql_ssl_rsa_setup  --datadir=/opt/data3308

拉起編號3308mysql例項服務

[root@CentOSMysql1 mysql]# mysqld_multi start 3308

修改編號3308mysql例項中root使用者的密碼為“123456”

[root@CentOSMysql1 mysql]# mysql -uroot -p -S /tmp/mysql.sock3308

Enter password:

//這裡輸出剛才安裝時出現的臨時密碼。

mysql> set password=password("123456");

到這裡,編號為3308的例項安裝完畢。

 

5、 安裝編號為56mysql例項,這裡使用mysql5.6的版本,由安裝目錄,埠號以及datadir根據my.cnf中的來。

[root@CentOSMysql1 opt]#mv mysql-5.6.34-linux-glibc2.5-x86_64 /usr/local

[root@CentOSMysql1 opt]#cd /usr/local/

[root@CentOSMysql1 local]#ln –s mysql-5.6.34-linux-glibc2.5-x86_64 mysql56

[root@CentOSMysql1 local]# cd mysql56

[root@CentOSMysql1 mysql56]# chown -R mysql:mysql .

[root@CentOSMysql1 mysql56]# scripts/mysql_install_db --user=mysql  --datadir=/opt/data56

這一步和mysql5.7有些不一樣,不會生成臨時密碼。Mysql5.6的臨時密碼預設是空。

[root@CentOSMysql1 local]# mysqld_multi start 56

拉起編號56mysql例項服務

[root@CentOSMysql1 local]# mysql -uroot -p -S /tmp/mysql.sock56

修改編號56mysql例項中root使用者的密碼為“123456”

mysql> set password="123456";

ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number

mysql> set password=password("123456");

到這裡,編號為56的例項安裝完畢。

 

6、 最後,試試用mysqld_multi統一起停所有的mysql例項

[root@CentOSMysql1 ~]# mysqld_multi report

Reporting MySQL servers

MySQL server from group: mysqld3307 is running

MySQL server from group: mysqld3308 is running

MySQL server from group: mysqld56 is running

[root@CentOSMysql1 ~]# mysqld_multi report

Reporting MySQL servers

MySQL server from group: mysqld3307 is running

MySQL server from group: mysqld3308 is running

MySQL server from group: mysqld56 is running

[root@CentOSMysql1 ~]# mysqld_multi stop

[root@CentOSMysql1 ~]# mysqld_multi report

Reporting MySQL servers

MySQL server from group: mysqld3307 is not running

MySQL server from group: mysqld3308 is not running

MySQL server from group: mysqld56 is not running

[root@CentOSMysql1 ~]# mysqld_multi start

[root@CentOSMysql1 ~]# mysqld_multi report

Reporting MySQL servers

MySQL server from group: mysqld3307 is running

MySQL server from group: mysqld3308 is running

MySQL server from group: mysqld56 is running

 

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28497416/viewspace-2131453/,如需轉載,請註明出處,否則將追究法律責任。

相關文章