In M A S T E R & lt establish a server S L A V E account server replicated data
[root@master ~]# mysql
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;
Query OK, 0 rows affected (0.00 sec)
113
mysql> show grants for 'allentuns'@'192.168.2.200'; # View authorized users
+-----------------------------------------------------------------------------------------------------------------------------------
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------------------------
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'allentuns'@'192.168.2.200' IDENTIFIED BY PASSWORD '*
7B8E3D52A612E2CB04E31B43FCDC20A07317E332 '|
+-----------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
In S L A V E authorized user on the server connection test
[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
Modify M A S T E R & lt on the server My S Q L D main configuration file
[Root @ master ~] # vim /usr/local/mysql/my.cnf log-bin = master-bin # binary log files binlog_format = mixed # binary log file index server-id = 1 # for identification ID
port = 3306 #Mysql default port number basedir = / usr / local / mysql / #Mysql source directory datadir = / mydata / data # data storage directory path
socket = /var/lib/mysql/mysql.sock # socket file path
innodb_file_per_table = 1 # 1 per table file
114
[root@master ~]# service mysqld restart
Modified S L A V E on the server Mys QL D main configuration file
[root@slave ~]# vim /usr/local/mysql/my.cnf
# Log-bin = master-bin # Comment binary log file, if and when the other server from the primary server, otherwise closed
#binlog_format = mixed # Comment this line
From the service does not start automatically when the thread skip_slave_start = 1 # Start Service
read_only = 1 # Set Slave Server is read only
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
See M A S T E R & lt server binary log log events and locations for the binary S L A V E
Server replication
mysql> RESET MASTER; # empty binary log 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)
Notes: File: representation from the log start copying Position: indicates the start of copying bit offset from this event
115
In S L A V E synchronization server M A S T E R & lt server data above
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 # get help 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;
Start S L A V E replicated server and view the status of the thread
mysql> start slave; # Slave server thread start 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 server address Master_User: allentuns # connection Master server user name Master_Port: listening port Connect_Retry 3306 #Master server: 60 # retry interval
Master_Log_File: binary log files Read_Master_Log_Pos master-bin.000003 # I / O threads to read: 120 # I / binary event log file location Relay_Log_File O thread reads: the relay log file relay_log.000002 #SQL thread is read Relay_Log_Pos: 284 #SQL threads read and execute the relay event log file location Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: IO thread state Slave_SQL_Running Yes #Slave server: SQL Server thread status Yes #Slave
Replicate_Do_DB: # Replicate following representation is used to indicate the beginning of the database or table which does not require synchronization in Replicate_Ignore_DB replication:
Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table:
Last_Errno: 0 #SQL thread reads log parameters number of errors Last_Error: #SQL thread reads log parameter error message
Recently Exec_Master_Log_Pos 0 # Value for the SQL_SLAVE_SKIP_COUNTER:: Skip_Counter 120
Relay_Log_Space: 451
Until_Condition: None # all existing relay logs the total size of 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 # behind the Master server time 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
In S L A V E threads started View Server
[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 |
In M A S T E R & lt server creates a database and S L A V E to verify the existence on the server
### server and create a database Master View
[root@master ~]# mysql -e 'create database mydbtest;' [root@master ~]# mysql -e 'show databases;'
| Database |
+--------------------+
| information_schema |
| mydbtest |
| mysql |
| performance_schema |
| test |
### View in Slave server has 'mydbtest' database
[root@slave ~]# mysql -e 'show databases;'
| Database |
+--------------------+
| information_schema |
| Mydbtest | # database has been successfully synchronized to the slave server
| mysql |
| performance_schema |
| test |
In M A S T E R & lt and S L A V E server View binary event log has been updated location
### View Master Server
[root@master ~]# mysql -e 'show master status;'
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
### View Slave server
[root@slave ~]# mysql -e 'show slave status\G;' |grep 'Read_Master_Log_Pos' Read_Master_Log_Pos: 226
5 . Master-Slave monitor
5.1Linux sendmail mail system 139 to an external mailbox
1, the installation and sendmail mailx
# yum -y install sendmail mailx
2, modify the configuration file
# cp /etc/mail.rc /etc/mail.rc.bak
# cat >> /etc/mail.rc << EOF
set [email protected] smtp=smtp.139.com
set [email protected] smtp-auth-password=yi15093547036 smtp- auth=login
EOF
3, restart the service
# service sendmail restart
4, send a test message
echo "I Love You" | mail -s " Message Subject: MIS you" [email protected]
5.2 were created on login from the server (Slave), only limited local operation to ensure safety
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 monitor script
#!/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" [email protected] 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
5.4 Timing script execution monitoring
[root@slave ~]# crontab -l
*/1 * * * * root /bin/sh /mydata/check_mysql_health.sh
5.5 Test: stop slave process, see if you can receive e-mail
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 thread has stopped Slave_SQL_Running: No #SQL thread has stopped 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)
Semi-synchronous replication master from