內容:mysql
一、mysql的複製類型sql
二、mysql的主從複製、主主複製示例數據庫
三、mysql的讀寫分離centos
1、mysql複製類型bash
1 複製概述服務器
Mysql內建的複製功能是構建大型,高性能應用程序的基礎。將Mysql的數據分佈到多個系統上去,這種分佈的機制,是經過將Mysql的某一臺主機的數據複製到其它主機(slaves)上,並從新執行一遍來實現的。複製過程當中一個服務器充當主服務器,而一個或多個其它服務器充當從服務器。主服務器將更新寫入二進制日誌文件,並維護文件的一個索引以跟蹤日誌循環。這些日誌能夠記錄發送到從服務器的更新。當一個從服務器鏈接主服務器時,它通知主服務器從服務器在日誌中讀取的最後一次成功更新的位置。從服務器接收從那時起發生的任何更新,而後封鎖並等待主服務器通知新的更新。異步
請注意當你進行復制時,全部對複製中的表的更新必須在主服務器上進行。不然,你必需要當心,以免用戶對主服務器上的表進行的更新與對從服務器上的表所進行的更新之間的衝突。socket
二、MySQL Replication Cluster的幾種同步數據方式:ide
Synchronous Replication 同步複製工具
Asynchronous Replication 異步複製
Semisynchronous Replication 半同步複製
同步複製: 指的是客戶端鏈接到MySQL主服務器寫入一段數據, MySQL主服務器同步給MySQL從服務器須要等待從服務器發出同步完成的響應才返回客戶端OK, 這其中等待同步的過程是阻塞的, 若是有N臺從服務器, 效率極低
異步複製: 指的是客戶端鏈接到MySQL主服務器寫入一段數據, MySQL主服務器將寫入的數據發送給MySQL從服務器, 而後直接返回客戶端OK, 可能從服務器的數據會和主服務不一致
半同步複製:指的是客戶端鏈接到MySQL主服務器寫入一段數據, MySQL主服務器只將數據同步複製給其中一臺從服務器, 半同步複製給其餘的從服務器, 來達到其中一臺從服務器徹底同步的效果
2、mysql複製示例
一、mysql的主從複製:
master端:
(1)、在[mysqld]段啓用二進制日誌,並選擇惟一的server-ID
[root@localhost ~]# cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql log-bin = master log #啓動bin日誌 log-bin-index = log.index server-id = 1 #設置serverID # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
(2)建立具備複製權限REPLICATION SLAVE,REPLICATION CLIENT的用戶
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'test'@'%' IDENTIFIED BY '123456';
(3)查看當前msater的binary-log信息
mysql> SHOW MASTER STATUS/G -> \G ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/G' at line 1 mysql> SHOW MASTER STATUS\G *************************** 1. row *************************** File: master log.000003 #設置從服務器是須要用到此參數 Position: 343 #設置從服務器是須要用到此參數 Binlog_Do_DB: Binlog_Ignore_DB: 1 row in set (0.00 sec)
salve端:
(1)修改mysql的配置文件,在[mysqld]段啓用relay中繼日誌,並選擇惟一的server-ID,同時設置mysql只讀,注意不須要開啓binary-log:
[root@localhost ~]# cat !$ cat /etc/my.cnf [mysqld] datadir=/mysql/mydata socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 relay-log = relay-log #開啓中繼日誌 read-only = 1 #設置只讀 server-id = 12 #設置serverID [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
(2)鏈接至主服務器,並登陸賬號開始複製數據
mysql> CHANGE MASTER TO MASTER_HOST='10.1.252.235', MASTER_LOG_FILE='master-log.000001', MASTER_LOG_POS=106, MASTER_USER='test', MASTER_PASSWORD='123456'; Query OK, 0 rows affected (0.02 sec) Query OK, 0 rows affected (0.30 sec)
(3)查看slave狀態,此時slave的複製還沒啓動
mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 10.1.252.235 Master_User: test Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000001 Read_Master_Log_Pos: 106 Relay_Log_File: relay-log.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: master-log.000001 Slave_IO_Running: No #IO-thread還沒啓動 Slave_SQL_Running: No #SQL-thread還沒啓動 Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 106 Relay_Log_Space: 106 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec)
(4)啓動slave
mysql> START SLAVE; Query OK, 0 rows affected (0.00 sec) mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.1.252.235 Master_User: test Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000001 Read_Master_Log_Pos: 106 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 252 Relay_Master_Log_File: master-log.000001 Slave_IO_Running: Yes #已經啓動複製 Slave_SQL_Running: Yes #已經啓動複製 Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 106 Relay_Log_Space: 401 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec)
(5)測試:
在主服務器建立一個數據庫:
mysql> SHOW MASTER STATUS\G *************************** 1. row *************************** File: master-log.000001 Position: 106 Binlog_Do_DB: Binlog_Ignore_DB: 1 row in set (0.00 sec) mysql> CREATE DATABASE mysqlreplicaton; Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysqlreplicaton | | test | +--------------------+ 4 rows in set (0.00 sec)
查看從服務器已然成功同步
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysqlreplicaton | | test | +--------------------+ 4 rows in set (0.00 sec)
二、mysql的半同步複製示例
mysql的半同步複製是google提供的semisync_master來提供,因此須要安裝次模塊:
On Master
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; #加載模塊 mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1; #啓動該模塊 mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000; #設置鏈接超時時間 查看主服務器上的semi_sync是否開啓,注意clients 變爲1 ,證實主從半同步複製鏈接成功: mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';
On Slave
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; #加載模塊 mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1; #啓動該模塊 mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD; #重啓線程才能生效 查看從服務器上的semi_sync是否開啓: mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';
以上的加載模塊只是暫時加載,要想永久加載,要在Master和Slave的my.cnf中編輯:
# On Master
[mysqld] rpl_semi_sync_master_enabled=1 rpl_semi_sync_master_timeout=1000 # 1 second
# On Slave
[mysqld] rpl_semi_sync_slave_enabled=1
三、mysql主主複製示例:
(1)修改配置文件:
# 主服務器上
[mysqld] server-id = 10 log-bin = mysql-bin relay-log = relay-mysql relay-log-index = relay-mysql.index auto-increment-increment = 2 auto-increment-offset = 1
查看bin日誌信息
server1|mysql> SHOW MASTER STATUS\G ************************** 1. row *************************** File: mysql-bin.000001 Position: 710 Binlog_Do_DB: Binlog_Ignore_DB: 1 row in set (0.00 sec)
# 從服務器上
[mysqld] server-id = 20 log-bin = mysql-bin relay-log = relay-mysql relay-log-index = relay-mysql.index auto-increment-increment = 2 auto-increment-offset = 2
查看bin日誌信息:
server2|mysql> SHOW MASTER STATUS\G mysql> SHOW MASTER STATUS\G *************************** 1. row *************************** File: mysql-bin.000003 Position: 811 Binlog_Do_DB: Binlog_Ignore_DB: 1 row in set (0.00 sec)
(2)各服務器接下來指定對另外一臺服務器爲本身的主服務器便可:
MASTER:
mysql> CHANGE MASTER TO MASTER_HOST='10.1.252.235', MASTER_LOG_FILE='mysql-log.000003', MASTER_LOG_POS=811, MASTER_USER='test', MASTER_PASSWORD='123456';
SLAVE:
mysql> CHANGE MASTER TO MASTER_HOST='10.1.252.235', MASTER_LOG_FILE='mysql-log.000001', MASTER_LOG_POS=710, MASTER_USER='test', MASTER_PASSWORD='123456';
更多文章請關注個人博客
3、mysql讀寫分離示例:
一、mysql的讀寫分離實驗示意圖:
二、實現mysql的讀寫分離的工具備不少,如mysql-mmm,amoeba,mysql-proxy等,這裏演示的mysql官方提供的mysql-proxy。注意,mysql-proxy自己並不支持讀寫分離,mysql-proxy依附了lua讀寫分離的腳本才能真正實現讀寫分離,因此,啓動proxy時必須指定lua腳本。
三、安裝mysql-proxy,mysql-proxy已在epel中提供,因此能夠直接yum安裝:
[20:34 root@centos6.8~]# yum info mysql-proxy Loaded plugins: fastestmirror, refresh-packagekit, security Repository 'base' is missing name in configuration, using id Repository epel is listed more than once in the configuration Loading mirror speeds from cached hostfile base | 4.0 kB 00:00 epel | 4.3 kB 00:00 Available Packages Name : mysql-proxy Arch : i686 Version : 0.8.5 Release : 1.el6 Size : 218 k Repo : epel Summary : A proxy for the MySQL Client/Server protocol URL : http://forge.mysql.com/wiki/MySQL_Proxy License : GPLv2 Description : MySQL Proxy is a simple program that sits between your client and MySQL : server(s) that can monitor, analyze or transform their communication. : Its flexibility allows for unlimited uses, common ones include: load balancing, : fail-over, query analysis, query filtering and modification and many more. [20:42 root@centos6.8~]# yum install -y mysql-proxy
四、mysql-proxy的使用:
mysql-proxy的配置能夠直接嵌套在mysql的主配置文件my.cfg中,並以[mysql-proxy]段進行填寫,還能夠直接使用mysql-proxy命令來運行,
[20:45 root@centos6.8~]# mysql-proxy --help-all Usage: mysql-proxy [OPTION...] - MySQL Proxy Help Options: -h, --help Show help options --help-all Show all help options --help-proxy Show options for the proxy-module proxy-module -P, --proxy-address=<host:port> listening address:port of the proxy-server (default: :4040) -r, --proxy-read-only-backend-addresses=<host:port> address:port of the remote slave-server (default: not set) -b, --proxy-backend-addresses=<host:port> address:port of the remote backend-servers (default: 127.0.0.1:3306) --proxy-skip-profiling disables profiling of queries (default: enabled) --proxy-fix-bug-25371 fix bug #25371 (mysqld > 5.1.12) for older libmysql versions -s, --proxy-lua-script=<file> filename of the lua script (default: not set) --no-proxy don't start the proxy-module (default: enabled) --proxy-pool-no-change-user don't use CHANGE_USER to reset the connection coming from the pool (default: enabled) --proxy-connect-timeout connect timeout in seconds (default: 2.0 seconds) --proxy-read-timeout read timeout in seconds (default: 8 hours) --proxy-write-timeout write timeout in seconds (default: 8 hours) Application Options: -V, --version Show version --defaults-file=<file> configuration file --verbose-shutdown Always log the exit code when shutting down --daemon Start in daemon-mode --user=<user> Run mysql-proxy as user --basedir=<absolute path> Base directory to prepend to relative paths in the config --pid-file=<file> PID file in case we are started as daemon --plugin-dir=<path> path to the plugins --plugins=<name> plugins to load --log-level=(error|warning|info|message|debug) log all messages of level ... or higher --log-file=<file> log all messages in a file --log-use-syslog log all messages to syslog --log-backtrace-on-crash try to invoke debugger on crash --keepalive try to restart the proxy if it crashed --max-open-files maximum number of open files (ulimit -n) --event-threads number of event-handling threads (default: 1) --lua-path=<...> set the LUA_PATH --lua-cpath=<...> set the LUA_CPATH
五、根據上面的命令幫助,咱們來使用mysql-proxy來進行配置
[21:47 root@centos6.8~]# mysql-proxy --daemon -r 10.1.252.215:3306 -b 10.1.252.235:3306 --plugins="proxy" -s "/usr/share/oc/mysql-proxy-0.8.5/examples/rw-splitting.lua" [21:47 root@centos6.8~]# 2016-11-05 21:47:10: (critical) plugin proxy 0.8.5 started
六、查看端口,mysql-proxy的4040端口已然處於監聽狀態
[21:47 root@centos6.8/usr/share/doc/mysql-proxy-0.8.5/examples]# ss -tanl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:42142 *:* LISTEN 0 25 :::514 :::* LISTEN 0 25 *:514 *:* LISTEN 0 128 *:4040 *:* LISTEN 0 128 :::111 :::* LISTEN 0 128 *:111 *:* LISTEN 0 128 :::58800 :::* LISTEN 0 32 *:21 *:* LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 128 ::1:631 :::*
七、測試:
[root@localhost ~]# mysql -utest1 -h10.1.252.109 -p --port=4040 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.1.73-log Source distribution Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> exit #成功登陸mysql-proxy端 Bye [root@localhost ~]# mysql -utest1 -h10.1.252.109 -p --port=4040 -e "show databases;" Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysqlreplicaton | | nihao | | test | +--------------------+ [root@localhost ~]# mysql -utest1 -h10.1.252.109 -p123456 --port=4040 -e "create database mysqlproxy;" [root@localhost ~]# mysql -utest1 -h10.1.252.109 -p --port=4040 -e "show databases;" Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysqlproxy | | mysqlreplicaton | | nihao | | test | +--------------------+
固然,效果不是很明顯,咱們可使用mysql-proxy提供的管理接口admin.lua來進行觀察,其管理端口會監聽在4041端口,只不過這個在安裝時默認沒有提供這個腳本,在網上找了個來試,提示腳本錯誤,因此無法進行測試。
OK,mysql複製暫到這裏,更多文章請關注個人博客 。