linux MySql 在 Master 主從複製配置

Master 服務器上創建用於 Slave 服務器複製數據的賬戶mysql

 

 

 

[root@master ~]# mysqlsql

 

mysql> grant replication slave,replication client on *.* to 'allentuns'@'192.168.2.200' identified by '1234@6@'; Query OK, 0 rows affected (0.02 sec)數據庫

mysql> flush privileges;vim

Query OK, 0 rows affected (0.00 sec)安全


113bash

 

mysql> show grants for 'allentuns'@'192.168.2.200';  #查看受權用戶服務器

+-----------------------------------------------------------------------------------------------------------------------------------socket

| Grants for allentuns@192.168.2.200                                                                                                                |ide

+-----------------------------------------------------------------------------------------------------------------------------------測試

| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'allentuns'@'192.168.2.200' IDENTIFIED BY PASSWORD '*

7B8E3D52A612E2CB04E31B43FCDC20A07317E332' |

+-----------------------------------------------------------------------------------------------------------------------------------

1 row in set (0.00 sec)

 

 

 Slave 服務器上使用受權用戶鏈接測試

 

 

 

[root@slave ~]# mysql -uallentuns -p1234@6@ -h 192.168.2.100

 

Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 6

 

Server version: 5.6.12-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2013, 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

 

修改 Master 服務器上的 Mysqld 主配置文件

 

 

 

[root@master ~]# vim /usr/local/mysql/my.cnf log-bin = master-bin                          #二進制日誌文件 binlog_format = mixed  #二進制日誌文件索引 server-id = 1     #用於識別的 ID

port = 3306            #Mysql 的默認端口號 basedir = /usr/local/mysql/ #Mysql 源程序目錄 datadir = /mydata/data      #數據存儲目錄路徑

socket = /var/lib/mysql/mysql.sock #套接字文件路徑

 

innodb_file_per_table = 1    #每表一個文件


114

 

[root@master ~]# service mysqld restart

 

 

 

修改 Slave 服務器上的 Mysqld 主配置文件

 

 

 

[root@slave ~]# vim /usr/local/mysql/my.cnf

 

#log-bin = master-bin  #註釋二進制日誌文件,若是當其它從服務器的主服務器,不然關閉

 

#binlog_format = mixed #註釋此行

 

skip_slave_start = 1    #啓動服務時不自動啓動從服務線程

 

read_only = 1           #設置 Slave 服務器爲只讀

 

server-id = 10 relay_log = relay_log relay_log_index = relay_log.index port = 3306

basedir = /usr/local/mysql/

 

datadir = /mydata/data

 

socket = /var/lib/mysql/mysql.sock innodb_file_per_table = 1

[root@slave ~]# service mysqld restart

 

 

 

 查看 Master 服務器的二進制日誌及二進制日誌事件位置用於 Slave

 

服務器複製

 

 

mysql> RESET MASTER;   #清空二進制日誌 Query OK, 0 rows affected (0.05 sec) mysql> show master status;

+-------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+-------------------+----------+--------------+------------------+-------------------+

| master-bin.000003 |     120 |             |                  |                   |

+-------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

 

註釋:File:表示今後日誌開始複製    Position:表示從這個事件開始複製 偏移位


115

 

 Slave 服務器上同步 Master 服務器上面的數據

 

 

 

mysql> CHANGE MASTER TO

 

->  MASTER_HOST='192.168.2.100',

 

->  MASTER_USER='allentuns',

 

->  MASTER_PASSWORD='1234@6@',

 

->  MASTER_PORT=3306,

 

->  MASTER_LOG_FILE='master-bin.000003',

 

->  MASTER_LOG_POS=120;

 

Query OK, 0 rows affected, 2 warnings (0.38 sec) mysql> help change master to #獲取幫助信息 Name: 'CHANGE MASTER TO'

Description: Syntax:

CHANGE MASTER TO option [, option] ... CHANGE MASTER TO MASTER_HOST='master2.mycompany.com', MASTER_USER='replication', MASTER_PASSWORD='bigs3cret', MASTER_PORT=3306, MASTER_LOG_FILE='master2-bin.001', MASTER_LOG_POS=4, MASTER_CONNECT_RETRY=10;

 

 啓動 Slave 服務器的複製線程並查看狀態

 

 

 

mysql> start slave;    #啓動 Slave 服務器線程 Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G;


116

 

*************************** 1. row ***************************

 

Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.100                                               #Master 服務器地址 Master_User: allentuns      #鏈接 Master 服務器用戶名 Master_Port: 3306     #Master 服務器的監聽端口 Connect_Retry: 60                   #重試時間間隔

Master_Log_File: master-bin.000003    #I/O 線程讀取的二進制日誌文件 Read_Master_Log_Pos: 120                                                             #I/O 線程讀取的二進制日誌文件事件位置 Relay_Log_File: relay_log.000002                      #SQL 線程正在讀取的中繼日誌文件 Relay_Log_Pos: 284    #SQL 線程讀取和執行的中繼日誌文件事件位置 Relay_Master_Log_File: master-bin.000003

Slave_IO_Running: Yes                  #Slave 服務器的 IO 線程狀態 Slave_SQL_Running: Yes                                                      #Slave 服務器的 SQL 線程狀態

Replicate_Do_DB: #下面 Replicate 開頭的表示用來指明哪些庫或者表在複製時不須要同步 Replicate_Ignore_DB:

Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table:

Last_Errno: 0                    #SQL 線程讀取日誌參數的錯誤數量 Last_Error:       #SQL 線程讀取日誌參數的錯誤消息

Skip_Counter: 0                    #最近被用於 SQL_SLAVE_SKIP_COUNTER 的值 Exec_Master_Log_Pos: 120

Relay_Log_Space: 451

 

Until_Condition: None                 #全部原有中繼日誌的總大小 Until_Log_File:

Until_Log_Pos: 0


117

 

 

Master_SSL_CA_File:

 

Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key:

Seconds_Behind_Master: 0                   #落後於 Master 服務器的時間 Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

 

Last_IO_Error: Last_SQL_Errno: 0

Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1

Master_UUID: 84774a86-3ee8-11e4-a268-000c29ad35d7

 

Master_Info_File: /mydata/data/master.info

 

SQL_Delay: 0

 

SQL_Remaining_Delay: NULL

 

Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to u pdate it

Master_Retry_Count: 86400

 

Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0

 

 

ERROR:

 

No query specified

 

 

 

Slave 服務器查看啓動的線程

 

 

[root@slave ~]# mysql -e "show processlist;"

+----+-------------+-----------+------+---------+------+------------------------------------------------------------------------

| Id | User       | Host      | db  | Command | Time | State                                                                   | Info            |

+----+-------------+-----------+------+---------+------+------------------------------------------------------------------------

|  3 | system user |         | NULL | Connect |  533 | Waiting for master to send event                                           | NULL

|

|  4 | system user |          | NULL | Connect |  533 | Slave has read all relay log; waiting for the slave I/O thread to update i t | NULL                 |

|  5 | root      | localhost | NULL | Query  |   0 | init                                                                  | show processlist |

 

 

 Master 服務器建立數據庫並在 Slave 服務器上驗證是否存在

 

 

 

###在 Master 服務器建立數據庫並查看

 

[root@master ~]# mysql -e 'create database mydbtest;' [root@master ~]# mysql -e 'show databases;'

| Database           |

+--------------------+

| information_schema |

| mydbtest           |

| mysql              |

| performance_schema |

| test                |

 

###在 Slave 服務器查看是否有'mydbtest'數據庫

 

[root@slave ~]# mysql -e 'show databases;'

| Database           |

+--------------------+

| information_schema |

| mydbtest           | #數據庫已經成功同步到 slave 服務器

| mysql              |

| performance_schema |

| test                |

 

 

 Master Slave 服務器查看二進制日誌事件位置已更新

 

 

 

###查看 Master 服務器

 

[root@master ~]# mysql -e 'show master status;'

+-------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

 

###查看 Slave 服務器

[root@slave ~]# mysql -e 'show slave status\G;' |grep 'Read_Master_Log_Pos' Read_Master_Log_Pos: 226

 

5.主從監控

 

 

5.1Linux 系統 sendmail 發郵件到 139 外部郵箱

 

 

 

一、安裝 sendmail 和 mailx

 

# yum -y install sendmail mailx

 

二、修改配置文件

 

# cp /etc/mail.rc /etc/mail.rc.bak

 

# cat >> /etc/mail.rc << EOF

 

set from=13260071987@139.com smtp=smtp.139.com

 

set smtp-auth-user=13260071987@139.com smtp-auth-password=yi15093547036 smtp- auth=login

EOF

 

三、從新啓動服務

 

# service sendmail restart

 

四、發送測試郵件

 

echo "I Love You" |mail -s "郵件主題:MIS you" 13260071987@139.com

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.2 分別在從服務器(Slave)上建立登錄用戶,只限定本地運行,保證安全

 

 

 

mysql> grant all privileges on *.* to "zhengyansheng"@"127.0.0.1" identified by "passwor

 

 

Query OK, 0 rows affected (0.04 sec)

 

mysql> grant all privileges on *.* to "zhengyansheng"@"localhost" identified by "passwor d123";

Query OK, 0 rows affected (0.00 sec)

 

 

 

5.3Mysql 監控腳本

 

 

 

#!/bin/bash

 

#check MySQL_Slave Status

 

#crontab time 00:10

 

MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $4}'` MYSQLIP=`ifconfig eth1|grep "inet addr" | awk -F[:" "]+ '{print $4}'` STATUS=$(/usr/local/mysql/bin/mysql -u zhengyansheng -ppassword123 -S /tmp/mysql. sock -e "show slave status\G" | grep -i "running")

IO_env=`echo $STATUS | grep IO | awk ' {print $2}'` SQL_env=`echo $STATUS | grep SQL | awk '{print $2}'` DATA=`date +"%y-%m-%d %H:%M:%S"`

if [ "$MYSQLPORT" == "3306" ]

 

then

 

echo "mysql is running" else

mail -s "warn!server: $MYSQLIP mysql is down" 13260071987@139.com fi

if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]

 

then

 

echo "Slave is running!" else

echo "####### $DATA #########">>     /mydata/check_mysql_log/check_mysql_slave.l

 

 

echo "Slave is not running!" >>    /mydata/check_mysql_log/check_mysql_slave.log

 

echo "Slave is not running!" | mail -s "warn! $MYSQLIP MySQL Slave is not running" 1326

 

0071987@139.com fi

 

5.4 定時執行監控腳本

 

 

 

[root@slave ~]# crontab -l

 

*/1 * * * * root /bin/sh /mydata/check_mysql_health.sh

 

 

 

5.5 測試:中止 slave 進程,看是否能收到郵件

 

 

 

mysql> stop slave;

 

Query OK, 0 rows affected (0.01 sec)

 

mysql> show slave status\G

 

*************************** 1. row *************************** Slave_IO_State:

Master_Host: 192.168.2.100

 

Master_User: allentuns

 

Master_Port: 3306

 

Connect_Retry: 60

 

Master_Log_File: master-bin.000003

 

Read_Master_Log_Pos: 226

 

Relay_Log_File: relay_log.000005

 

Relay_Log_Pos: 284

 

Relay_Master_Log_File: master-bin.000003

 

Slave_IO_Running: No #IO 線程已經中止 Slave_SQL_Running: No #SQL 線程已經中止 Replicate_Do_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: 226

 

Relay_Log_Space: 615

 

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: Replicate_Ignore_Server_Ids: Master_Server_Id: 1

 

 

Master_Info_File: /mydata/data/master.info

 

SQL_Delay: 0

 

SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400

Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0

1 row in set (0.00 sec)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

主從半同步複製

相關文章
相關標籤/搜索