主從複製搭建:一個master對應2個slavemysql
##########################################linux
//環境規劃sql
linux版本:centos6.4 mysql5.5.30 master:10.10.54.154 slave: 10.10.54.155 10.10.54.156 #master服務器不是新搭建的,因此須要把master數據庫中數據備份到從服務器
//master修改配置文件,建立用戶
shell
1.修改配置文件數據庫
shell> vim /etc/my.cnf [mysqld] server_id=1 log_bin=master_bin shell> /et/init.d/mysqld restart
2.建立用戶,授予複製權限
vim
mysql> grant replication slave on *.* to 'repl'@'10.10.54.%' identified by 'mysql'; mysql> flush privileges;
//slave修改配置文件
centos
兩個從服務器上操做同樣服務器
shell> vim /etc/my.vnf server_id=8 log_bin=slave-bin
//mysqldump導入數據到從服務器
session
1.master操做架構
mysql> flush tables with read lock; mysql> mysqldump -uroot -p --all-databases --flush-logs --master-data=2 > alldatabases.sql mysql> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000017 | 107 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> unlock tables;
2.slave操做
兩臺從服務器上操做同樣
shell> mysql -uroot -pmysql < alldatabases.sql //slave上執行change master操做,查看主從架構 mysql> change master to master_host='10.10.54.154',master_user='repl',master_password='mysql',master_log_file='master-bin.000006',master_log_pos=307; mysql> start slave; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.10.54.154 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000017 Read_Master_Log_Pos: 107 Relay_Log_File: mycentos5-relay-bin.000002 Relay_Log_Pos: 254 Relay_Master_Log_File: master-bin.000017 Slave_IO_Running: Yes Slave_SQL_Running: Yes
//測試主從架構是否正常運行
1.master 上建立表
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | d1 | | mysql | | performance_schema | | test | +--------------------+ mysql> use d1; mysql> create table t1(id int NOT NULL,name varchar(20) NOT NULL); mysql> insert into t1 values(1,'hello');
2.從服務器上查看錶是否同步
mysql> use d1; msyql> select * from t1; +----+-------+ | id | name | +----+-------+ | 1 | hello | +----+-------+
上面主從複製環境已經搭建成功,下面搭建mysql半複製(Semisynchronous Replication)
//環境規劃
mysql版本:5.5.30 master:10.10.54.154 slave:10.10.54.155
#主從環境已經搭建好
//master上安裝半同步複製插件
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so'; Query OK, 0 rows affected (0.02 sec) mysql> set global rpl_semi_sync_master_enabled=1; Query OK, 0 rows affected (0.00 sec) mysql> set global rpl_semi_sync_master_timeout=1000; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%semi%'; +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | rpl_semi_sync_master_enabled | ON | | rpl_semi_sync_master_timeout | 1000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | ON | +------------------------------------+-------+ #要想下次重啓mysql自動生效,能夠加入配置文件 sehll> vim /etc/my.cnf [msyqld] set global rpl_semi_sync_master_enabled=1 set global rpl_semi_sync_master_timeout=1000
//slave上安裝插件
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so'; mysql> set global rpl_semi_sync_slave_enabled = 1; mysql> stop slave; mysql> start slave; mysql> show variables like '%semi%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | ON | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ #同理,要想下次重啓mysql自動生效,能夠加入配置文件 sehll> vim /etc/my.cnf [msyqld] set global rpl_semi_sync_slave_enabled = 1
//測試半同步是否生效
mysql> show global status like 'rpl_semi%'; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | | Rpl_semi_sync_master_net_avg_wait_time | 0 | | Rpl_semi_sync_master_net_wait_time | 0 | | Rpl_semi_sync_master_net_waits | 0 | | Rpl_semi_sync_master_no_times | 0 | | Rpl_semi_sync_master_no_tx | 0 | | Rpl_semi_sync_master_status | ON | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 0 | | Rpl_semi_sync_master_tx_wait_time | 0 | | Rpl_semi_sync_master_tx_waits | 0 | | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 0 | +--------------------------------------------+-------+
#測試成功
搭建mysql replication支持ssl,一主一從
//環境說明:
master:10.10.54.156 slave:10.10.54.157
1.主服務器上生成私鑰 shell> cd /etc/pki/CA/ shell> umask 077;openssl genrsa -out private/cakye.pem 2048 Generating RSA private key, 2048 bit long modulus ..........................................................................+++ ..............................+++ 2.主服務器上生成自簽證書 shell> openssl req -new -x509 -key private/cakye.pem -out cacert.pem -days 365 3.主服務器上申請證書 shell> cd /usr/local/mysql/ shell> mkdir ssl shell> chown mysql.mysql ssl shell> umask 077; openssl genrsa -out /usr/local/mysql/ssl/master.key 2048 shell> openssl req -new -key /usr/local/mysql/ssl/master.key -out /usr/local/mysql/ssl/master.csr shell> openssl ca -in /usr/local/mysql/ssl/master.csr -out /usr/local/mysql/ssl/master.crt -days 365 4.配置文件中添加ssl支持 shell> vim /etc/my.cnf [msyqld] ssl ssl-ca=/etc/pki/CA/cacert.pem ssl-cert=/usr/local/mysql/ssl/master.crt ssl-key=/usr/local/mysql/ssl/master.key
5.從服務器上 shell> cd /usr/local/mysql/ shell> mkdir ssl shell> chown mysql.mysql ssl shell> umask 077; openssl genrsa -out /usr/local/mysql/ssl/mysql.key 2048 shell> openssl req -new -key /usr/local/mysql/ssl/master.key -out /usr/local/mysql/ssl/mysql.csr 6.把證書傳給主服務器,在主服務器上籤署證書後在傳給從服務器 shell> cd /usr/local/mysql/ shell> scp mysql.csr root@10.10.54.156:/etc/pki/CA 7.主服務器上籤署證書 shell> openssl ca -in mysql.csr -out mysql.crt -days 365 8.傳回給從服務器 shell> scp cacert.pem mysql.crt root@10.10.54.157:/usr/local/mysql/ssl shell> chown -R mysql.mysql 9.配置my.cnf文件 在my.cnf中添加ssl支持 [mysqld] ssl #添加這一行 10.主服務器上建立ssl連接用戶 mysql> grant replication client,replication slave on *.* to ssl@10.10.54.157 identified by 'ssl'; 11.從服務器上配置 mysql > change master to master_host='10.10.54.156', master_user='ssl', master_password='ssl', master_log_file='mysql-bin.000001',master_port=3306,master_log_pos=112, master_ssl=1,master_ssl_ca='/usr/local/mysql/ssl/cacert.pem', master_ssl_cert='/usr/local/mysql/ssl/mysql.crt',master_ssl_key='/usr/local/mysql/ssl/mysql.key'; 12.查看從上slave狀態 mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.10.54.156 Master_User: ssl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 503 Relay_Log_File: mysql-relay.000002 Relay_Log_Pos: 263 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes .... Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: Yes Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem Master_SSL_CA_Path: Master_SSL_Cert: /usr/local/mysql/ssl/mysql.crt Master_SSL_Cipher: Master_SSL_Key: /usr/local/mysql/ssl/mysql.key Seconds_Behind_Master: 0