原創做品,容許轉載,轉載時請務必以超連接形式標明文章 原始出處 、做者信息和本聲明。不然將追究法律責任。http://zhaochj.blog.51cto.com/368705/1635982html
目錄mysql
一、概述linux
二、主從時間同步sql
三、主從服務器mysql部署數據庫
四、配置、啓動從服務vim
五、測試centos
六、總結安全
一、概述服務器
mysql內建的複製功能是mysql應用的「水平擴展」的架構,是mysql高性能運用的基礎,爲一個服務器配置一個或多個備庫來進行數據的同步(準備的說不該該說是「同步」,由於主從服務器的數據根本不可能作同步)。在這樣一個分佈式的架構中各個服務器間時間應該同步纔是,能爲後期日誌的統計分析提供更爲準備的信息,這樣才能準確反應出某個時間點咱們系統的訪問狀況。多線程
主從複製是一種異步複製的模型,說成「主從同步」是不精確的,應說是「主從複製」,先有主,再有從。因mysql主從複製原理的限定,在從庫上只有一個sq線程(即便在mysql5.6中支持多線程,但也是針對庫級別的)來重放中繼日誌中的事件,這種在主服務器能併發的查詢請求在從服務器就成了串行化來執行,這是一個從服務器會慢於主服務器的一個緣由。
對主從服務器在硬件選擇上的建議:若是在規劃主從時考慮到有可能會把從服務器提高爲主服務器,那主從服務器硬件配置建議徹底相同,應選擇核多、主頻高的CPU,頻率高、總容量大的內存,多塊高速的SAS硬盤(或SSD)及帶有BBU的RAID卡,在RAID級別上建議選擇raid10或raid50;若是從庫只是用來做爲主庫的一個副本,以便於數據的備份,那從服務器的CPU不必與主服務一個規格,但硬盤的IO這一塊仍是不要太差,IO高有助於事務的重放效率,在必定程度上能縮小與主庫的時間差。
二、主從時間同步
主服務器配置成一ntpd服務器,自身與外網的ntpd服務進行時間校準,而又對從服務器提供時間校準服務。在生產環境中最好不要用ntpdate來強行調整服務器的時間,這樣會對讓服務器的時間產生空白區,注意,ntpdate工具是調整時間,而不是像ntpd工具同樣是校準時間,二者的差異是挺大的。
主服務器基礎環境:
[root@master ~]
# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
[root@master ~]
# uname -r
2.6.32-358.el6.x86_64
[root@master ~]
# ifconfig | grep Bcast #從服務器的系統與主服務器相同,只是IP地址是192.168.0.202
inet addr:192.168.0.201 Bcast:192.168.0.255 Mask:255.255.255.0
安裝配置主服務器成爲ntpd時間服務器:
[root@master ~]
# yum -y install ntp
[root@master ~]
# service ntpd start #啓用服務後等個1-3分鐘,就可用ntpq -p命令查看是否能與遠程ntpd服務器鏈接
[root@master ~]
# vi /etc/ntp.conf #配置ntp配置文件,使其成爲從服務器的時間服務器
……略……
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap
#複製上邊一行並啓用,把ip修改爲本地地址段
#外部時間服務器不可用時,以本地時間做爲時間服務
server 127.127.0.0
fudge 127.127.0.0 stratum 10
……略……
[root@master ~]
# service ntpd restart #重啓服務
[root@master ~]
# chkconfig --level 235 ntpd on
在從服務器上安裝配置ntp:
[root@slave ~]
# yum -y install ntp
[root@slave ~]
# vim /etc/ntp.comf #註釋掉默認的時間服務器,加上本地時間服務器
……略……
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.0.201
#server 0.centos.pool.ntp.org iburst
#erver 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
……略……
[root@slave ~]
# service ntpd restart
[root@slave ~]
# chkconfig ntpd on
[root@slave ~]
# ntpq -p #監控時間校準的狀態信息
測試時能夠試着把從服務器的時間調慢幾分鐘,而後觀察從服務器的時間是否會慢慢的遇上來。
三、主從服務器mysql部署
3.一、主服務器安裝配置
[root@master software]
# pwd
/root/software
[root@master software]
# ls
mysql-5.6.24-linux-glibc2.5-x86_64.
tar
.gz
[root@master software]
# tar xf mysql-5.6.24-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@master software]
# cd /usr/local
[root@master
local
]
# ln -sv mysql-5.6.24-linux-glibc2.5-x86_64 mysql
[root@master
local
]
# cd mysql
[root@master mysql]
# chown -R root.mysql ./*
[root@master mysql]
# mkdir /mnt/mydata/dbdata #建立數據目錄
[root@master mysql]
# chown -R mysql.mysql /mnt/mydata/dbdata
[root@master mysql]
# scripts/mysql_install_db --user=mysql --datadir=/mnt/mydata/dbdata/ #初始化數據庫,若是沒有mysql用戶請自行建立
因mysql 5.6中的my.cnf文件太過簡單,因此複製mysql 5.5中的配置文件再做一些修改:
[root@master mysql]
# cp /opt/lamp/mysql55/support-files/my-large.cnf /etc/my.cnf
修改my.cnf配置文件
[root@master mysql]
# vim /etc/my.cnf
……
#thread_concurrency = 8 #註釋此選項,在5.6中已被放棄
#指定數據目錄
datadir =
/mnt/mydata/dbdata
#基於Innodb的表讓表使用單獨的表空間
innodb_file_per_table = 1
#啓用慢查詢日誌
slow_query_log = 1
#若是查詢時間長於「long_query_time」設定的值,那就把此查詢查詢到「slow_query_log_file」文件中
long_query_time = 1
slow_query_log_file =
/var/log/mysql/master-slow
.log
#定義錯誤日誌文件
log_error =
/var/log/mysql/master
.error
……
#定義二進制日誌文件
log-bin=
/var/log/mysql/mysql-bin
#每一次事件提交就使日誌同步到磁盤,默認「sync_binlog=0」,表示讓系統本身來flush使在內存的日誌刷到磁盤,設置爲「1」是一種安全的設置,由於當系統crash時你最多丟失一個事件,但相反會增長系統的IO,即便這樣也建議設置成「1」
sync_binlog = 1
……
#在5.6中已建議把二進制日誌的格式修改爲「row」
binlog_format=row
……
#設置服務器ID號
server-
id
= 100
[root@master mysql]
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld56 #複製啓動腳本
[root@master mysql]
# service mysqld56 start
[root@master mysql]
# ls /var/log/mysql
master.error master-slow.log mysql-bin.000001 mysql-bin.index
[root@master mysql]
# vim /etc/profile.d/mysql56.sh #導出二進制文件路徑
export
PATH=
/usr/local/mysql/bin
:$PATH
[root@master mysql]
# source /etc/profile.d/mysql56.sh
[root@master ~]
# mysql #數據庫鏈接測試
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 22
Server version: 5.6.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
3.二、從服務器安裝配置
從服務器的安裝方法與主服務器同樣,只是在my.cnf配置文件有些不一樣:
[root@slave mysql]
# vim /etc/my.cnf
……
#thread_concurrency = 2
datadir =
/mnt/mydata/dbdata
innodb_file_per_table = 1
log_error =
/var/log/mysql/slave
.error
#relay_log
relay_log =
/var/log/mysql/mysql-relay-bin
sync_relay_log = 1
#設置讓sql thread讀取中繼日誌中的語句並在本地運用時記錄到本地的二進制日誌,固然從服務器要打開「log-bin」啓用二進制日誌,若是此從服務器還能夠做爲其餘從服務器的主服務器,應啓用「log_slave_updates = 1」
log_slave_updates = 1
#在從服務器的數據中會多出兩個文件,一個是master.info和relay-log.info,master.info文件記錄了當前從服務器請求主服務器的二進制文件名與偏移量等信息,而relay.info文件記錄了從服務器當前所使用的中繼日誌文件名與偏移量等信息,若是主服務器比較繁忙,文件中記錄的偏移量這樣的信息是時刻在變化的,惋惜的是這些數據不是時時寫入磁盤的,是先寫進內存,再同步到磁盤,這樣可能會對數據帶來不一致的問題。在新版本中引入了下邊兩個變量來控制此行爲,表示當每一次事件後就讓數據更新到磁盤,也就是更新master.info和relay-log.info兩個文件,這樣儘量讓數據及時刷新到磁盤,讓數據持久化,固然相應增長了系統的IO,但爲了數據的安全就是值得的。
sync_master_info = 1
sync_relay_log_info =1
#security
#設置從庫爲只讀模式,但具備supper權限的用戶不受此限制
read_only = 1
#跳過當mysqld啓用時自動啓用slave線程
skip_slave_start
……
log-bin=
/var/log/mysql/mysql-bin
sync_binlog = 1
……
server-
id
= 200
[root@slave mysql]
# service mysqld56 start
Starting MySQL.. SUCCESS!
[root@slave mysql]
# ls /var/log/mysql #中繼日誌尚未生成,因從服務器線程沒有啓動
mysql-bin.000001 mysql-bin.index slave.error
[root@slave mysql]
# ls /mnt/mydata/dbdata/ #master.info和relay-log.info兩個文件沒有生成,由於從服務線程沒有啓動
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema slave.pid
test
[root@slave mysql]
# mysql #測試
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 3
Server version: 5.6.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
四、配置、啓動從服務
上邊已把主從服務器各自的my.cnf配置文件已設置完畢,要想啓動mysql的主從複製功能,如今還差一個從從服務器到主服務器的鏈接賬戶,再啓用從服務器上的複製線程便可。
4.一、在主服務器上建立擁有複製權限的賬戶
[root@master ~]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 191
Server version: 5.6.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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> GRANT replication slave,replication client ON *.* TO
'repuser'
@
'192.168.0.%'
IDENTIFIED BY
'111111'
;
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW MASTER STATUS;
#記錄下當前主服務器所使用的二進制文件及position
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 430 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row
in
set
(0.00 sec)
4.二、從服務器鏈接主服務器、啓動複製線程
[root@slave mysql]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 3
Server version: 5.6.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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> CHANGE MASTER TO
-> MASTER_HOST=
'192.168.0.201'
,
-> MASTER_USER=
'repuser'
,
-> MASTER_PASSWORD=
'111111'
,
-> MASTER_LOG_FILE=
'mysql-bin.000001'
,
-> MASTER_LOG_POS=0;
#這裏的偏移量我指定爲「0」,零表示從開二進制日誌的開頭開始進行復制
mysql> SHOW SLAVE STATUS\G
#查看slave的狀態信息,IO thread和sql thread線程都尚未啓動
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.201
Master_User: repuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 4
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
……
mysql> START SLAVE;
#啓動複製線程
mysql> SHOW SLAVE STATUS\G
#兩個線程已啓動
*************************** 1. row ***************************
Slave_IO_State: Waiting
for
master to send event
Master_Host: 192.168.0.201
Master_User: repuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 430
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 593
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> SHOW GRANTS FOR
'repuser'
@
'192.168.0.%'
;
#這個複製所用的賬戶已經從主服務器複製到了從服務器
+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants
for
repuser@192.168.0.% |
+--------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO
'repuser'
@
'192.168.0.%'
IDENTIFIED BY PASSWORD
'*FD571203974BA9AFE270FE62151AE967ECA5E0AA'
|
+--------------------------------------------------------------------------------------------------------------------------------------------------+
1 row
in
set
(0.00 sec)
五、測試
在主服務器上建立、修改數據:
mysql> CREATE DATABASE mydb1;
Query OK, 1 row affected (0.00 sec)
mysql> USE mydb1;
Database changed
mysql> CREATE TABLE tb1 (
id
INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name CHAR(20) NOT NULL,age TINYINT UNSIGNED);
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO tb1 (name,age) VALUES (
'tom'
,12),(
'jem'
,23);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
在從服務器上查看在主服務器的修改:
[root@slave mysql]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 6
Server version: 5.6.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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> SELECT * FROM mydb1.tb1;
+----+------+------+
|
id
| name | age |
+----+------+------+
| 1 | tom | 12 |
| 2 | jem | 23 |
+----+------+------+
2 rows
in
set
(0.00 sec)
測試證實:在主服務器上的修改已複製到了從服務器上。
六、總結
mysql的主從複製已搭建完畢,但這種複製並不能替代數據庫的備份。因爲從服務器是單進程模型(mysql 5.6的多線程也只能是庫級別的),因此在一個比較繁忙的mysql系統上,從服務器可能會落後主服務器,這是mysql主從複製架構原生帶來的特性,並不能真正作到主從的同步,因此在後期的維護工做中咱們要用到一些監控工具來及時發現從服務器是否真正落後主服務器。在博文的最開始已提到在這種分佈式的架構中時間同步的重要性,在用「show slave status\G」查看從服務器狀態時有一個變量「Seconds_Behind_Master」這個值就是反應從服務器落後主服務器的時間,詳細一點就是說明從服務器的SQL thread處理中繼日誌中的事件時,會把主服務器上日誌中的timestamp與從服務器運行sql thread時的時間進行相關運算,這個差值就是「Seconds_Behind_Master」的值,因此主從服務器間的同步顯得格外的重要。
「Seconds_Behind_Master」的值爲0也不表明主服務器與從服務器已經同步,只能說sql thread已把relay log中的事件執行完了,由於主服務器的bin log不會實時的同步到從服務器上,從服務器上的relay log總會落後於主服務器的bin log,因此「Seconds_Behind_Master」這個值不具備真正意義上的參考價值。要監控主從是否一致,能夠用percona-tools工具集中的pt-heartbeat工具。
本文出自 「專一運維,與Linux共舞」 博客,請務必保留此出處http://zhaochj.blog.51cto.com/368705/1635982