17.1 MySQL主从介绍
·MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
·MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
·主从过程大致有3个步骤
1)主将更改操作记录到binlog里
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
3)从根据relaylog里面的sql语句按顺序执行
·主上有一个log dump线程,用来和从的I/O线程传递binlog
·从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地
备份库
读库,减轻主库的压力
17.2 准备工作
安装mysql
简单步骤:
·wget 免编译安装包
·tar 解压压缩包
·mv 解压出的目录到/usr/local/mysql下 确保/usr/local/mysql空
·./scripts/mysql_install_db --user=mysql --datadir=/data/mysql 初始化并定义用户和datadir(提前创建)
·编辑 /etc/my.cnf,定义datadir和socket位置
·cp support-files/mysql.server /etc/init.d/mysqld 复制启动脚本到/etc/init.d下名称为mysqld
·在mysqld中定义basedir,datadir
·启动程序/etc/init.d/mysqld start
·chkconfig mysqld on 可以添加到开机启动
确保进行主从的机器中,mysqld服务均已启运行
安装过程参考:http://blog.51cto.com/11530642/2073264
17.3 配置主
修改my.cnf,增加server-id=128(可以根据ip来定)和log_bin=alex(logbin的前缀)
[[email protected] mysql]# /etc/init.d/mysqld restart ##重启mysqld Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS!
把mysql库备份并恢复成aming库,作为测试数据
[[email protected] mysql]# mysqldump -uroot -p123456 mysql > /tmp/mysql.sql ##备份数据库 Warning: Using a password on the command line interface can be insecure. [[email protected] mysql]# mysql -uroot -p123456 -e'create database a4l' ##创建新库 Warning: Using a password on the command line interface can be insecure. [[email protected] mysql]# mysql -uroot -p123456 a4l < /tmp/mysql.sql ##导入数据库 Warning: Using a password on the command line interface can be insecure. [[email protected] mysql]# ll 总用量 177292 drwx------ 2 mysql mysql 4096 3月 31 20:51 a4l -rw-rw---- 1 mysql mysql 663420 3月 31 20:51 alex.000001 -rw-rw---- 1 mysql mysql 14 3月 31 20:34 alex.index -rw-rw---- 1 mysql mysql 56 3月 12 21:59 auto.cnf -rw-rw---- 1 mysql mysql 79691776 3月 31 20:51 ibdata1 -rw-rw---- 1 mysql mysql 50331648 3月 31 20:51 ib_logfile0 -rw-rw---- 1 mysql mysql 50331648 3月 12 21:47 ib_logfile1 -rw-rw---- 1 mysql mysql 111979 3月 31 20:34 localhost.localdomain.err -rw-rw---- 1 mysql mysql 5 3月 31 20:34 localhost.localdomain.pid drwx------ 2 mysql mysql 4096 3月 18 22:52 mysql drwx------ 2 mysql mysql 4096 3月 18 22:52 mysql2 drwx------ 2 mysql mysql 4096 3月 12 21:47 performance_schema drwx------ 2 mysql mysql 6 3月 12 21:47 test drwx------ 2 mysql mysql 324 3月 30 22:48 zrlog
创建用作主从同步的用户:
[[email protected] mysql]# mysql -uroot -p123456 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 5 Server version: 5.6.36-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> grant replication slave on *.* to 'repl'@192.168.65.129 identified by '123456'; Query OK, 0 rows affected (0.00 sec) ## 创建用作同步数据的用户 mysql> flush tables with read lock; ##锁表,防止数据变动 Query OK, 0 rows affected (0.01 sec) mysql> show master status; +-------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------+----------+--------------+------------------+-------------------+ | alex.000001 | 663631 | | | | +-------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
备份需要同步的数据库:
[[email protected] mysql]# mysqldump -uroot -p123456 a4l > /tmp/a4l.sql Warning: Using a password on the command line interface can be insecure. [[email protected] mysql]# mysqldump -uroot -p123456 mysql2 > /tmp/my2.sql Warning: Using a password on the command line interface can be insecure. [[email protected] mysql]# mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql Warning: Using a password on the command line interface can be insecure.
(mysql中有许多用户名和密码,没办法把权限全部复制,所以不需要同步)
17.4 配置从
安装mysql
[[email protected] ~]# vim /etc/my.cnf
(从不需要bin_log,只有主才需要)
[[email protected] ~]# /etc/init.d/mysqld restart ##重启mysqld Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [[email protected] ~]# ls /data/mysql/ auto.cnf ib_logfile0 localhost.localdomain.err mysql performance_schema ibdata1 ib_logfile1 localhost.localdomain.pid mysql2 test [[email protected] ~]# mysql -uroot -p123456 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 2 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> create database a4l; ##创建数据库 Query OK, 1 row affected (0.00 sec) mysql> create database mysql2; Query OK, 1 row affected (0.00 sec) mysql> create database zrlog; Query OK, 1 row affected (0.00 sec) mysql> quit Bye ##导入数据 [[email protected] ~]# mysql -uroot -p123456 a4l < /tmp/a4l.sql Warning: Using a password on the command line interface can be insecure. [[email protected] ~]# mysql -uroot -p123456 mysql2 < /tmp/my2.sql Warning: Using a password on the command line interface can be insecure. [[email protected] ~]# mysql -uroot -p123456 zrlog < /tmp/zrlog.sql Warning: Using a password on the command line interface can be insecure.
传输数据:
[[email protected] ~]# scp 192.168.65.128:/tmp/*.sql /tmp/ The authenticity of host '192.168.65.128 (192.168.65.128)' can't be established. ECDSA key fingerprint is SHA256:PXl0tJsOm3464x52jTxX7L+ToKm1Hb6AUMq6lh4ASX0. ECDSA key fingerprint is MD5:2e:9a:f9:25:b3:80:43:05:a4:3d:3c:9b:48:6e:a0:0e. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.65.128' (ECDSA) to the list of known hosts. [email protected]'s password: a4l.sql 100% 648KB 23.2MB/s 00:00 my2.sql 100% 642KB 27.3MB/s 00:00 zrlog.sql 100% 10KB 3.1MB/s 00:00
[[email protected] ~]# mysql -uroot -p123456 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 3 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_host='192.168.65.128', master_user='repl', master_password='123456', master_log_file='alex.000001', master_log_pos=663631; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.65.128 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: alex.000002 Read_Master_Log_Pos: 120 Relay_Log_File: localhost-relay-bin.000003 Relay_Log_Pos: 278 Relay_Master_Log_File: alex.000002 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: 120 Relay_Log_Space: 613 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 128 Master_UUID: bc3bf10d-34ed-11e8-b6a9-000c297e8b1b Master_Info_File: /data/mysql/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 update it 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)
status中两个yes才是成功:
回到主服务器上,unlock tables
[[email protected] mysql]# mysql -uroot -p123456 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 2 Server version: 5.6.36-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> unlock tables; Query OK, 0 rows affected (0.00 sec)
错误汇总:
1,Slave_IO_Running: No
查看错误问题:
Last_IO_Error: 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.
查看主从服务器两台机器/data/mysql/auto.cnf的UUID是否一样,如果一样,删除其中一个,并重启该服务器,再次进行从服务器mysql的change master操作就能成功了。
17.5 测试主从同步
在my.cnf中定义:可以在主上,也可以在从上
主服务器上
binlog-do-db= //仅同步指定的库,多个可以用 , 去分割
binlog-ignore-db= //忽略指定库
从服务器上
replicate_do_db=
replicate_ignore_db=
replicate_do_table=
replicate_ignore_table=
replicate_wild_do_table= //如alex.%, 支持通配符%
replicate_wild_ignore_table=
(最长用是最后两条)
从上删除comment表中的信息,主上也没了
主上删除了comment表,从上comment表也不存在了
主上 mysql -uroot aming
select count(*) from db;
truncate table db;
到从上 mysql -uroot aming
select count(*) from db;
主上继续drop table db;
从上查看db表
原文地址:http://blog.51cto.com/11530642/2093953