一:复制过程
Mysql主服务器接受客户端的更新数据请求,在本地数据库中保存下数据的更改,然后将更新操作记录在本地的二进制日志中,记录为更改事件.每当主服务器接受备用服务器的请求后,会通过3306端口,将其所请求的二进制事件文件,发送给备用服务器.备用服务器接受到二进制日志文件,保存到自己的中继日志中.然后备用服务器的SQL线程重新回放所有的中继日志.进行sql重演.保证和主服务器数据一致.整个复制的过程是一个异步操作过程.
二:工作线程
主服务器:IO Thread
从服务器:IO Thread ,SQL Thread
三:部署案例
对于部署数据库主从复制来讲,分为新安装和添加从.下面先接受新安装,然后再介绍添加从.
新安装部署
步骤:
(1).主库创建复制账号 (2).配置主库和从库 (3).通知备库连接到主库进行数据复制
1.服务器规划
192.168.0.138 master 192.168.0.137 slave01
2.安装mysql-2.6.27
http://yujianglei.blog.51cto.com/7215578/1725585
3.主库创建复制账号
mysql> grant replication slave,replication client on *.* to rep@'192.168.0.%' identified by 'rep123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
4.配置主库和备库
主库:(192.168.0.138)
[mysqld] server-id=1 #唯一服务器ID log_bin=/mydata/bin_log/mysql-bin #二进制日志的文件位置和命名方式 #binlog_format=ROW binlog_format=STATEMENT #二进制日志格式 sync_binlog=1 # max_binlog_size=512M #最大二进制文件 binlog_cache_size=2M #二进制日志缓存 expire_logs_days = 7 #二进制日志缓存时间
备库:(192.168.0.137)
[mysqld] server-id=2 #唯一服务器ID relay_log=/mydata/relay_log/mysql-relay #中继日志的文件位置和命名方式 log_slave_updates=1 #允许备库将重放的事件记录到自身的二进制日志中去 read_only=1 #阻止任何没有特权的线程修改数据
5.启动复制,告诉从库如何连接主库,配置操作在从库(192.168.0.137)中配置
(1)执行change操作
mysql> change master to master_host='192.168.0.138',master_user='rep',master_password='rep123',master_log_file='mysql-bin.000001',master_log_pos=120; Query OK, 0 rows affected, 2 warnings (0.34 sec)
(2)开启slave模式
mysql> start slave; Query OK, 0 rows affected (0.04 sec)
(3)查看slave状态
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.138 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 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: 452 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: 1 Master_UUID: 085ed1f9-a87a-11e5-9822-b8ca3af274df 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 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)
6.查看主库和从库的工作线程
(1)查看slave服务器上得IO和SQL两个线程
mysql> show processlist; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 5 Current database: *** NONE *** +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+-------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+-------------------+ | 3 | system user | | NULL | Connect | 146 | Waiting for master to send event | NULL | | 4 | system user | | NULL | Connect | 146 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | | 5 | root | localhost | NULL | Query | 0 | init | show processlist | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+-------------------+ 3 rows in set (0.00 sec)
(2)查看slave服务器上的IO一个线程
mysql> show processlist; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 3 Current database: mysql +----+------+---------------------+-------+-------------+------+-----------------------------------------------------------------------+-------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+---------------------+-------+-------------+------+-----------------------------------------------------------------------+-------------------+ | 2 | rep | 192.168.0.137:32524 | NULL | Binlog Dump | 546 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | | 3 | root | localhost | mysql | Query | 0 | init | show processlist | +----+------+---------------------+-------+-------------+------+-----------------------------------------------------------------------+-------------------+ 2 rows in set (0.01 sec)
7.测试主从复制
主库:
mysql> create database red_packet; Query OK, 1 row affected (0.02 sec) mysql> show create database red_packet; +------------+---------------------------------------------------------------------+ | Database | Create Database | +------------+---------------------------------------------------------------------+ | red_packet | CREATE DATABASE `red_packet` /*!40100 DEFAULT CHARACTER SET utf8 */ | +------------+---------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> grant all privileges on red_packet.* to red_packet@'localhost' identified by '123456'; Query OK, 0 rows affected (0.03 sec) mysql> select User,Host,Password from mysql.user; +------------+-----------------------+-------------------------------------------+ | User | Host | Password | +------------+-----------------------+-------------------------------------------+ | root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | localhost.localdomain | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | rep | 192.168.0.% | *08F5BB4670F148DB0376A1EE646F0C67EAE138CE | | red_packet | localhost | *6081F775BC114C5673018A1B14DFB40CDB9B4AE2 | +------------+-----------------------+-------------------------------------------+
从库:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | red_packet | +--------------------+ 4 rows in set (0.00 sec) mysql> select User,Host,Password from mysql.user; +------------+-----------------------+-------------------------------------------+ | User | Host | Password | +------------+-----------------------+-------------------------------------------+ | root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | localhost.localdomain | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | root | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | red_packet | localhost | *6081F775BC114C5673018A1B14DFB40CDB9B4AE2 | +------------+-----------------------+-------------------------------------------+ 6 rows in set (0.00 sec) 结论:在主库上创建的书库和用户在从库上都可以查到,MYSQL主从复制OK!