zhy2_rehat6_mysql02 - 5.7主从搭建.txt

 


1.0------------锁库:

mysql>FLUSH TABLES WITH READ LOCK;

这个命令是全局读锁定,执行了命令之后所有库所有表都被锁定只读。一般都是用在数据库联机备份,这个时候数据库的写操作将被阻塞,读操作顺利进行。
解锁的语句也是

mysql> unlock tables;

mysql> show databases;


mysqldump -uroot -p123456 --set-gtid-purged=off --routines --default-character-set=utf8 --lock-all-tables --add-drop-database -A > /data/backup/bai_all.sql

scp -r bai_all.sql [email protected]:/data/backup


mysql> source /data/backup/bai_all.sql

 


======================================

还原数据库,用户权限要授权,刷新。


GRANT REPLICATION SLAVE,REPLICATION CLIENT,SELECT ON *.* TO 'repl'@'10.1.1.%' IDENTIFIED BY '123456';


mysql> grant replication slave on *.* to 'repl'@'192.168.66.%' identified by '123456' WITH GRANT OPTION;
mysql> DELETE FROM mysql.user WHERE user='repl';
mysql> flush privileges;
mysql> select host ,user from mysql.user;


然后再slave 上:


CHANGE MASTER TO
MASTER_HOST='10.1.1.13',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql_bin.000004',
MASTER_LOG_POS=1710;

mysql> show master status\G
mysql> show master status;
mysql> start slave;
mysql> show slave status\G
mysql> stop slave;
mysql> start slave io_thread;
mysql> start slave sql_thread;
mysql> show slave hosts;
mysql>select Host,User from user where User='root';

 

 

[root@bxdddbb backup]# service mysqld stop
Shutting down MySQL.. [??????]
[root@bxdddbb backup]# service mysqld start
Starting MySQL... [??????]
[root@bxdddbb backup]#


mysql> DELETE FROM mysql.user WHERE user='repl';
mysql> flush privileges;
mysql> select host ,user from mysql.user;

-----------------------------

mysql> flush logs;
Query OK, 0 rows affected (0.16 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000009 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> show master status\G
mysql> show slave hosts;

+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 142 | | 3306 | 143 | e7c7bc5b-fa79-11e7-b980-0050569b4d14 |
| 141 | | 3306 | 143 | eab836fc-f9f8-11e7-b637-0050569b092e |
+-----------+------+------+-----------+--------------------------------------+

mysql> show variables like '%server_uuid%';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | af056dd8-6986-11e9-a1af-000c29063835 |
+---------------+--------------------------------------+
1 row in set (0.01 sec)
-------------------------------------------------
问题1:
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

原因分析:
mysql 5.6的复制引入了uuid的概念,各个复制结构中的server_uuid得保证不一样,但是查看到直接copy data文件夹后server_uuid是相同的,show variables like '%server_uuid%';

解决方法:
找到data文件夹下的auto.cnf文件,修改里面的uuid值,保证各个db的uuid不一样,重启db即可
-------------------------------------------------
问题2:
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

解决方法:
在source那边,执行:

mysql> flush logs; ---------mysql 刷新日志
mysql> reset master; ----------清理所有binlog(谨慎)


show master status;
记下File, Position。

在target端,执行:
CHANGE MASTER TO MASTER_LOG_FILE='testdbbinlog.000008',MASTER_LOG_POS=107;
slave start;
show slave status \G
一切正常。
-------------------------------------------------

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> slave reset;
mysql> reset slave all; --------------清楚主库的(从配置)

mysql> show slave status\G

 


CHANGE MASTER TO MASTER_LOG_FILE='mysql_bin.000009',MASTER_LOG_POS=154;

 

 


root@bxdddbb 17:28: [(none)]> set global gtid_purged='3b932723-c855-11e7-96f3-0050568a17e9:1-159';
Query OK, 0 rows affected (0.00 sec)


root@bxdddba 15:34: [(none)]> show master status\G
*************************** 1. row ***************************
File: mybinlog.000006
Position: 709

 


[mysqld]
server-id = 248
binlog-ignore-db=mysql
replicate-do-db = ordersys
auto-increment-increment = 2
auto-increment-offset = 2
log-bin = /data/mysql/mybinlog
expire_logs_days = 7

 

[mysqld]
server-id = 249
binlog-ignore-db=mysql
replicate-do-db = ordersys
auto-increment-increment = 2
auto-increment-offset = 1
log-bin = /data/mysql/mybinlog
expire_logs_days = 7


=====================================================
切换测试

杀掉主库mysql进程,模拟主库发生故障,进行自动failover操作。

[[email protected] ~]# pkill -9 mysqld

truncate table t_dsf;
truncate table t_dsf_summary;

truncate table t_pay_cups;

SELECT COUNT(*) from t_dsf
SELECT COUNT(*) from t_dsf_summary


SELECT COUNT(1) FROM t_payment;


1.停止141:-------------------------------------

[root@ums-data ~]# service mysql stop
[root@ums-data ~]# ip add

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
恢复演示: 144上

ifconfig 主要是可以手动启动、观察与修改网络接口的相关参数

ifconfig {interface} {up|down}
-----------------------------------------

ifconfig eth0 down
ifconfig eth0 up

 

10.在监控服务器上查看日志:------------------

[[email protected] ~]# tail -f /var/log/messages


==============================
测试:

mysql> create database db2;
Query OK, 1 row affected (0.00 sec)

mysql> show databases ;

mysql> use baibai;


mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000017 | 2218 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+

 

mysql> show slave status\G
mysql> show databases ;
mysql> show tables;

 

你可能感兴趣的:(zhy2_rehat6_mysql02 - 5.7主从搭建.txt)