MySQL的Master/Slave叢集安裝和配置,masterslave

來源:互聯網
上載者:User

MySQL的Master/Slave叢集安裝和配置,masterslave

本文講述MySQL的Master/Slave叢集安裝和配置,安裝的版本是最新的穩定版本GA 5.6.19。

為了支援有限的HA,我們使用Master/Slave簡單的讀寫分離叢集。有限的HA是指當Master不可用時,資料不會丟失,但在Master宕機的情況下是不可寫的,必須手工處理故障。如果要支援更高的可用性,可以使用兩台Master來做熱切換。

Master和Slave的MySQL安裝是相同的,只是my.cnf的配置不同,需要配置二進位記錄檔複製。

沒有特殊說明,命名中帶#的為root使用者操作,帶$的為mysql Linux使用者的操作。

安裝準備

1. 在安裝MySQL前,需要確認下面的系統軟體已經安裝在Linux中。

軟體名稱

軟體描述

gcc-4.4.7

程式設計語言編譯器

gcc-c++-4.4.7

C++語言編譯器

cmake-2.6.4-5

跨平台的開源構建系統

ncurses-devel-5.7-3.20090208

控制列印控制台螢幕

2. 建立mysql Linux使用者

# groupadd mysql

# useradd -g mysql mysql

# passwd mysql

3. 準備安裝目錄

建立MySQL安裝目錄,並賦許可權給mysql使用者:

# mkdir /usr/local/mysql-5.6.19

# chown mysql:mysql /usr/local/mysql-5.6.19

# chmod -R 770 /usr/local/mysql-5.6.19

4. 建立MySQL資料存放區目錄:

# mkdir /data

# mkdir /data/mysql

# chown mysql:mysql /data/mysql

5. 授權MySQL解壓源碼目錄/usr/local/src目錄的可執行許可權給所有使用者:

# chmod -R 757 /usr/local/src

安裝MySQL

1. 解壓縮安裝包:

$ cd /usr/local/src

$ tar -xzvf mysql-5.6.19.tar.gz

2. 配置MySQL編譯參數

$ cd /usr/local/src/mysql-5.6.19

$ cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.19\

       -DMYSQL_DATADIR=$MYSQL_DATA_PATH\

       -DSYSCONFDIR=/usr/local/mysql-5.6.19/conf\

       -DDEFAULT_CHARSET=utf8\

       -DDEFAULT_COLLATION=utf8_general_ci\

       -DWITH_READLINE=1\

       -DWITH_INNOBASE_STORAGE_ENGINE=1\

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITH_PERFSCHEMA_STORAGE_ENGINE=1

備忘:

-DCMAKE_INSTALL_PREFIX: 配置MySQL的安裝目錄。

-DMYSQL_DATADIR: 配置MySQL的資料目錄。

-DSYSCONFDIR:  配置MySQL的設定檔目錄。

-DDEFAULT_CHARSET: 預設字元集。

-DDEFAULT_COLLATION:設定預設語言的定序。

-DWITH_READLINE:支援大量匯入mysql資料。

-DWITH_INNOBASE_STORAGE_ENGINE:使用INNOBASE儲存引擎。

-DWITH_ARCHIVE_STORAGE_ENGINE:常應用於日誌記錄和彙總分析,不支援索引。

-DWITH_BLACKHOLE_STORAGE_ENGINE:黑洞儲存引擎。

-DWITH_PERFSCHEMA_STORAGE_ENGINE:效能模式引擎。

3. 執行make

$ make

$ make install

4. 修改myql使用者的環境變數,增加MYSQL_HOME,並把bin加到PATH:

$ vi ~/.bash_profile

在檔案中增加藍色字型部分:

# User specific environment and startup programs

# MySQL home目錄

export MYSQL_HOME=/usr/local/mysql-5.6.19

 

PATH=$PATH:$HOME/bin:$MYSQL_HOME/bin

 

export PATH









$ source ~/.bash_profile

初始化MySQL

1. 安裝service指令碼

# cp /usr/local/mysql-5.6.19/support-files/mysql.server/etc/init.d/mysqld

# chown mysql:mysql/etc/init.d/mysqld

# chmod 700 /etc/init.d/mysqld

2. 建立mysql許可權資料庫

$ $MYSQL_HOME/scripts/mysql_install_db--basedir=$MYSQL_HOME --datadir=/data/mysql

3. 建立PID檔案目錄

$ mkdir $MYSQL_HOME/var

4. 為master配置my.cnf

my.cnf格式不正確,很容易在啟動時錯誤,最好在原有檔案的基礎上通過vi工具在linux上修改。

如果檔案已經損壞,可以通過預設的模板中拷貝:

$ cp $MYSQL_HOME/support-files/my-default.cnf$MYSQL_HOME/my.cnf

編輯my.cnf

$ vi $MYSQL_HOME/my.cnf

在檔案中增加藍色字型部分:

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

 

[mysqld]

 

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

innodb_buffer_pool_size = 256M

innodb_flush_log_at_trx_commit=1

 

# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

log_bin=master-bin

log_bin_index=master-bin.index

 

# These are commonly set, remove the # and set as required.

# basedir = .....

datadir = /data/mysql

port = 3306

# first master server id

server_id = 1

socket = /tmp/mysql.sock

pid-file =  /usr/local/mysql-5.6.19/var/master.pid

 

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

# join_buffer_size = 128M

# sort_buffer_size = 2M

# read_rnd_buffer_size = 2M

 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


































5. 為slave配置my.cnf

和master的設定檔一樣進行修改,但注意檔案中的log檔案名稱、datadir和server_id等內容不同。

編輯my.cnf

$ vi $MYSQL_HOME/my.cnf

在檔案中增加藍色字型部分:

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

 

[mysqld]

 

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

innodb_buffer_pool_size = 256M

innodb_flush_log_at_trx_commit=1

 

# Replication

# relay-log=slave-relay-bin

# relay-log-index=slave-relay-bin.index

 

# These are commonly set, remove the # and set as required.

# basedir = .....

datadir = /data/mysql

port = 3306

# first slave server id of master 1

server_id = 101

socket = /tmp/mysql.sock

pid-file =  /usr/local/mysql-5.6.19/var/slave.pid

 

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

# join_buffer_size = 128M

# sort_buffer_size = 2M

# read_rnd_buffer_size = 2M

 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

































6. 啟動MySQL

啟動和停止master和slave都完全一樣。

$ service mysqldstart

通過下面命令查看是否啟動成功:

$ service mysqldstatus

7. 停止MySQL

$ service mysqld stop

管理MySQL安全性

預設MySQL的root的使用者密碼為空白,為了提高安全性,應該設定root使用者一個安全的密碼。

在伺服器上通過mysql使用者開啟MySQL用戶端:

$ mysql –u root

設定一個安全的密碼:

mysql> SET PASSWORD FORroot@localhost=PASSWORD('secret');

配置Master和Slave之間的複製

1. 建立執行複製的MySQL使用者

在Master上建立一個複製使用者,其中secret為使用者的密碼:

mysql> CREATEUSER repl_user IDENTIFIED BY 'secret';

並賦給複製許可權:

mysql>GRANTREPLICATION SLAVE ON *.* TO repl_user;

2. 鎖定Master並擷取二進位日誌位置值

擷取讀鎖:

mysql> FLUSH TABLES WITHREAD LOCK;

顯示當前二進位檔案名及位置值:

mysql> SHOW MASTER STATUS;

3. 通過mysqldump工具擷取Master資料快照

在另外一個會話中在Master上執行:

$ mysqldump -u root-p --all-databases --master-data > data_dump.sql

執行後,儲存在目前的目錄中。

4. 釋放Master上的讀鎖

在擷取讀鎖的會話中,執行釋放鎖命令:

mysql> UNLOCK TABLES;

5. 匯入dump資料到Slave中

通過scp拷貝data_dump.sql到Slave的機器上。

$ scp data_dump.sqlmysql@<ip of slave>:/home/mysql

在Slave上通過下面的命令匯入到Slave的MySQL中。

$ mysql –u root -p

mysql>source data_dump.sql;

6. 配置Master和Slave之間的複製

mysql> CHANGE MASTER TO MASTER_HOST='<ip or hostame of master>',

MASTER_USER='repl_user',

MASTER_PASSWORD='secret',

MASTER_PORT = 3306,

MASTER_LOG_FILE='master-bin.000003',

MASTER_LOG_POS=881;

上面的MASTER_LOG_FILE和MASTER_LOG_POS需要配置6.2中查詢到的資訊。<ipor hostame of master>是Master的IP或主機名稱。

啟動Slave:

mysql> START SLAVE;

7. 驗證Master和Slave的狀態

在Master上執行:

mysql> SHOW MASTER STATUS;

在Slave上執行:

mysql> SHOW SLAVE STATUS;

上面資訊顯示沒有任何錯誤,Slave_IO_State資訊為‘Waitigfor master to send event’,說明複製串連配置OK。


至此,兩個MySQL的Master/Slave模式的叢集已經部署成功,可以在Master上執行資料更新操作,發現可以正常複製到Slave上。


mysql 的master/slave 大量匯入時會同步成功

會啊,master有數後,slave會自動同步,如果設定了叢集。
 
mysql叢集配置

下載
Source Code 的
Red Hat/Oracle Enterprise Linux ver. 6 (Architecture Independent), RPM Package
參考資料:自己
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.