一、主从复制工作架构:
1、特点:
从服务器:有且只能有一个主服务器;
主服务器:可以有多从;
异步:从服务器的数据可能会落后于主服务器;
主服务器进行写操作,从服务器只进行读操作;
2、提高缓存命中率的解决思路:
(1)、在程序中的解决:使用取模算法、使用一致性哈希算法
(2)、使用公共缓存:memcached
3、主从复制架构mysql版本要求:
(1)、双版本一致:从节点版本低于主节点可能会引起错误;
4、复制起点:
(1)、从0开始:适用主从均为新建立的服务器;
(2)、主服务器已经运行一段时间且存不小的数据量:
1)、完全备份主服务上的数据,并将数据恢复至从服务器;
2)、从服务器从备份时主服务器二进制日志所在位置二始服务;
二、主从配置过程:
1、主节点配置过程:master
(1)、启用二进制日志,
[mysqld]
log_bin=master-bin
#binlog dump: 将从服务的io thread发出读取二进制日志事件的请求对应的数据发送给对方
(2)、为当前节点设置一个惟一的server-id
[mysqld]
server_id=#
(3)、创建有复制权限的用户账号;
REPLICATION SLAVE,REPLICATION CLIENT
GRANT REPLICATION SLAVE,REPLICATION CLIENTON *.* TO 'repluser'@'HOST' IDENTIFIED BY 'replpass';
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO repluser@'172.16.%.%' IDENTIFIED BY 'replp@ss';
Query OK, 0 rows affected (0.03 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
2、从节点配置过程:slave:
(1)、启用中继日志,并闭二进制日志(建议)
[mysqld]
#log_bin=slave-bin
relay_log=relay-log
relay_log_index=relay_log.index
(2)、为当前节点设置一个惟一的server-id
[mysqld]
server_id=#
(3)、使用有复制权限的用户账号连接至主服务器,并启动复制线程;
连接master:
mysql>HELP CHANGE MASTER TO
Name: 'CHANGE MASTER TO'
Description:
Syntax:
CHANGE MASTER TO option [, option]...
option:
MASTER_BIND = 'interface_name'
| MASTER_HOST = 'host_name' # 指明要连接的主节点,值类型字串
| MASTER_USER = 'user_name' # 具有复制权限的账号,值类型为字串
| MASTER_PASSWORD = 'password' # 上述用户的密码,值类型为字串
| MASTER_PORT = port_num
| MASTER_CONNECT_RETRY = interval
| MASTER_HEARTBEAT_PERIOD = interval
| MASTER_LOG_FILE = 'master_log_name' # 复制起点,主节点上二进制日志,值类型为字串
| MASTER_LOG_POS = master_log_pos # 复制起点,主节点上二进制日志中起始事件的位置,值类型为数值
| RELAY_LOG_FILE = 'relay_log_name'
| RELAY_LOG_POS = relay_log_pos
| MASTER_SSL = {0|1}
| MASTER_SSL_CA = 'ca_file_name'
| MASTER_SSL_CAPATH = 'ca_directory_name'
| MASTER_SSL_CERT = 'cert_file_name'
| MASTER_SSL_KEY = 'key_file_name'
| MASTER_SSL_CIPHER = 'cipher_list'
| MASTER_SSL_VERIFY_SERVER_CERT = {0|1}
| IGNORE_SERVER_IDS = (server_id_list)
mysql> CHANGE MASTER TOMASTER_HOST='192.168.88.131',MASTER_USER='repluser',MASTER_PASSWORD='testpass',MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=358;
(4)启动io thread以及sql thread
IOthread:向master请求二进制日志中的事件
SQLthread:从中继日志中读取事件并在本地执行
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
3、查看从节点的状态信息
mysql> SHOW SLAVE STATUS\G
三、配置过程实例演示:
环境:mysql主服务器:192.168.88.131
mysql从服务器:192.168.88.130
1、主服务器配置:
[root@wwwmysql]# service mysqld stop
停止 mysqld: [确定]
[root@wwwmysql]# rm -rf /var/lib/mysql/*
[root@wwwmysql]# [root@www mysql]# service mysqld stop
停止 mysqld: [确定]
[root@wwwmysql]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disablingsymbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log_bin=master-bin
#启用二进制日志
#relay_log=relay-log
server-id=1
#为当前节点设置一个惟一的server-id
innodb_file_per_table=1
skip_name_resolve
#auto_increment_offset=1
#auto_increment_increment=2
default-storage-engine=InnoDB
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
~
[root@wwwmysql]# service mysqld restart
停止 mysqld: [确定]
初始化 MySQL 数据库: Installing MySQL system tables...
OK
Filling helptables...
OK
To start mysqldat boot time you have to copy
support-files/mysql.serverto the right place for your system
PLEASE REMEMBERTO SET A PASSWORD FOR THE MySQL root USER !
To do so, startthe server, then issue the following commands:
/usr/bin/mysqladmin-u root password 'new-password'
/usr/bin/mysqladmin-u root -h www.field.com password 'new-password'
Alternativelyyou can run:
/usr/bin/mysql_secure_installation
which will alsogive you the option of removing the test
databases andanonymous user created by default. Thisis
stronglyrecommended for production servers.
See the manualfor more instructions.
You can startthe MySQL daemon with:
cd /usr ;/usr/bin/mysqld_safe &
You can test theMySQL daemon with mysql-test-run.pl
cd /usr/mysql-test; perl mysql-test-run.pl
Please reportany problems with the /usr/bin/mysqlbug script!
[确定]
正在启动 mysqld: [确定]
[root@wwwmysql]# mysql
Welcome to theMySQL monitor. Commands end with ; or\g.
Your MySQLconnection id is 4
Server version: 5.1.73-logSource distribution
Copyright (c)2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is aregistered 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> showglobal VARIABLES LIKE '%log%';
+-----------------------------------------+---------------------------------+
|Variable_name |Value |
+-----------------------------------------+---------------------------------+
| back_log |50 |
|binlog_cache_size |32768 |
|binlog_direct_non_transactional_updates | OFF |
|binlog_format |STATEMENT |
|expire_logs_days |0 |
| general_log |OFF |
|general_log_file |/var/run/mysqld/mysqld.log |
|innodb_flush_log_at_trx_commit |1 |
|innodb_locks_unsafe_for_binlog |OFF |
|innodb_log_buffer_size |1048576 |
|innodb_log_file_size |5242880 |
|innodb_log_files_in_group |2 |
|innodb_log_group_home_dir |./ |
|innodb_mirrored_log_groups |1 |
| log | OFF |
| log_bin | ON |
|log_bin_trust_function_creators |OFF |
|log_bin_trust_routine_creators |OFF |
| log_error |/var/log/mysqld.log |
|log_output |FILE |
|log_queries_not_using_indexes |OFF |
|log_slave_updates | OFF |
|log_slow_queries |OFF |
|log_warnings |1 |
|max_binlog_cache_size |18446744073709547520 |
|max_binlog_size |1073741824 |
|max_relay_log_size |0 |
| relay_log | |
|relay_log_index | |
|relay_log_info_file |relay-log.info |
|relay_log_purge |ON |
|relay_log_space_limit |0 |
|slow_query_log |OFF |
|slow_query_log_file |/var/run/mysqld/mysqld-slow.log |
|sql_log_bin |ON |
|sql_log_off |OFF |
|sql_log_update |ON |
|sync_binlog |0 |
+-----------------------------------------+---------------------------------+
38 rows in set(0.02 sec)
mysql> showvariables like '%skip_name_resolve%' ;
+-------------------+-----------+
|Variable_name | Value |
+-------------------+-----------+
|skip_name_resolve | ON |
+-------------------+------------+
1 row in set(0.00 sec)
mysql> showdatabases;
+--------------------------+
| Database |
+--------------------------+
|information_schema |
| mysql |
| test |
+--------------------------+
3 rows in set(0.00 sec)
mysql> showglobal variables like '%server%';
+----------------------+-------------------+
|Variable_name | Value |
+----------------------+-------------------+
|character_set_server | latin1 |
|collation_server | latin1_swedish_ci|
| server_id | 1 |
+----------------------+-------------------+
3 rows in set(0.00 sec)
#创建有复制权限的用户账号
mysql> GRANTREPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.%.%'IDENTIFIED BY 'testpass';
Query OK, 0 rowsaffected (0.03 sec)
#刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rowsaffected (0.00 sec)
mysql>
mysql> showmaster status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB |Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 358 | | |
+-------------------+----------+--------------+------------------+
1 row in set(0.00 sec)
2、从服务器配置
[root@testmysql]# service mysqld stop
停止 mysqld: [确定]
[root@testmysql]# rm -rf /var/lib/mysql/*
[root@testmysql]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disablingsymbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#log_bin=master-bin建议关闭
relay_log=relay-log
relay_log_index=relay_log.index
#启用中继日志,并闭二进制日志
server-id=7
#为当前节点设置一个惟一的server-id
innodb_file_per_table=1
skip_name_resolve
#笔者所用版本为centos6默认的mysql5.1,该选项只需添加即可启动,无需设置值,此外,设置值会报错无法启动
#auto_increment_offset=2
#auto_increment_increment=2
default-storage-engine=InnoDB
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
~
[root@testmysql]# service mysqld restart
停止 mysqld: [确定]
初始化 MySQL 数据库: Installing MySQL system tables...
OK
Filling helptables...
OK
To start mysqldat boot time you have to copy
support-files/mysql.serverto the right place for your system
PLEASE REMEMBERTO SET A PASSWORD FOR THE MySQL root USER !
To do so, startthe server, then issue the following commands:
/usr/bin/mysqladmin-u root password 'new-password'
/usr/bin/mysqladmin-u root -h test.field.com password 'new-password'
Alternativelyyou can run:
/usr/bin/mysql_secure_installation
which will alsogive you the option of removing the test
databases andanonymous user created by default. Thisis
stronglyrecommended for production servers.
See the manualfor more instructions.
You can startthe MySQL daemon with:
cd /usr ;/usr/bin/mysqld_safe &
You can test theMySQL daemon with mysql-test-run.pl
cd/usr/mysql-test ; perl mysql-test-run.pl
Please reportany problems with the /usr/bin/mysqlbug script![确定]
正在启动 mysqld: [确定]
[root@testmysql]# mysql
Welcome to theMySQL monitor. Commands end with ; or\g.
Your MySQLconnection id is 2
Server version:5.1.73 Source distribution
Copyright (c)2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is aregistered 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> showvariables like '%log%';
+-----------------------------------------+---------------------------------+
|Variable_name |Value |
+-----------------------------------------+---------------------------------+
| back_log | 50 |
|binlog_cache_size |32768 |
|binlog_direct_non_transactional_updates | OFF |
|binlog_format |STATEMENT |
|expire_logs_days |0 |
|general_log |OFF |
|general_log_file |/var/run/mysqld/mysqld.log |
|innodb_flush_log_at_trx_commit |1 |
|innodb_locks_unsafe_for_binlog |OFF |
| innodb_log_buffer_size | 1048576 |
|innodb_log_file_size |5242880 |
|innodb_log_files_in_group |2 |
|innodb_log_group_home_dir |./ |
|innodb_mirrored_log_groups |1 |
| log | OFF |
| log_bin | OFF |
|log_bin_trust_function_creators |OFF |
|log_bin_trust_routine_creators |OFF |
| log_error |/var/log/mysqld.log |
|log_output |FILE |
|log_queries_not_using_indexes |OFF |
|log_slave_updates |OFF |
| log_slow_queries | OFF |
|log_warnings |1 |
|max_binlog_cache_size |18446744073709547520 |
|max_binlog_size | 1073741824 |
|max_relay_log_size |0 |
| relay_log | relay-log |
| relay_log_index | relay_log.index |
| relay_log_info_file | relay-log.info |
|relay_log_purge |ON |
|relay_log_space_limit |0 |
|slow_query_log |OFF |
|slow_query_log_file |/var/run/mysqld/mysqld-slow.log |
|sql_log_bin |ON |
|sql_log_off | OFF |
|sql_log_update |ON |
|sync_binlog |0 |
+-----------------------------------------+---------------------------------+
38 rows in set(0.00 sec)
mysql> showvariables like '%skip_name_resolve%' ;
+-------------------+------------+
|Variable_name | Value |
+-------------------+------------+
| skip_name_resolve | ON |
+-------------------+------------+
1 row in set(0.00 sec)
#可以看到,添加即可启动
mysql> showglobal variables like '%server%';
+----------------------+-----------------------+
|Variable_name | Value |
+----------------------+-----------------------+
|character_set_server | latin1 |
|collation_server | latin1_swedish_ci|
| server_id | 7 |
+----------------------+-----------------------+
3 rows in set(0.00 sec)
#使用有复制权限的用户账号连接至主服务器
mysql>CHANGE MASTER TOMASTER_HOST='192.168.88.131',MASTER_USER='repluser',MASTER_PASSWORD='testpass',MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=358;
Query OK, 0 rowsaffected (0.03 sec)
#查看从服务启状态
mysql> SHOW SLAVE STATUS\G
***************************1. row ***************************
Slave_IO_State:
Master_Host: 192.168.88.131
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 358
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:master-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: No
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: 358
Relay_Log_Space: 106
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:
1 row in set(0.00 sec)
####并启动复制线程
mysql> startslave;
Query OK, 0 rowsaffected (0.00 sec)
mysql> SHOWSLAVE STATUS\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.88.131
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 358
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 252
Relay_Master_Log_File: master-bin.000003
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: 358
Relay_Log_Space: 401
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:
1 row in set(0.00 sec)
mysql>
3、测试主从同步:
主服务器:
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
| test |
+--------------------+
3 rows in set(0.00 sec)
mysql>CREATE DATABASE testslave;
Query OK, 1 rowaffected (0.00 sec)
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
| test |
| testslave |
+--------------------+
4 rows in set(0.00 sec)
mysql> showmaster status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB |Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 451 | | |
+-------------------+----------+--------------+------------------+
1 row in set(0.00 sec)
mysql>
从服务器:
mysql> SHOWSLAVE STATUS\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.88.131
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 451
#确认masterstatus是否一致
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 345
Relay_Master_Log_File:master-bin.000003
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:451
Relay_Log_Space: 494
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:
1 row in set(0.00 sec)
#确查看主服务器新增的数据是否能复制过来
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
| test |
| testslave |
+--------------------+
4 rows in set(0.01 sec)
mysql>
主服务器:
mysql> usetestslave
Database changed
mysql> showtables;
Empty set (0.00sec)
mysql>CREATE TABLE id(id int);
Query OK, 0 rowsaffected (0.08 sec)
mysql>insert into id values (666),(13888);
Query OK, 2 rowsaffected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql>select * from id;
+--------+
| id |
+--------+
| 666 |
| 13888 |
+--------+
2 rows in set(0.00 sec)
mysql> showmaster status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB |Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 745 | | |
+-------------------+----------+--------------+------------------+
1 row in set(0.00 sec)
mysql>
从服务器:
mysql> usetestslave
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
mysql> showtables;
+-------------------------+
|Tables_in_testslave |
+-------------------------+
| id |
+-------------------------+
1 row in set(0.00 sec)
mysql>select * from id;
+-------+
| id |
+-------+
| 666 |
| 13888 |
+-------+
2 rows in set(0.00 sec)
mysql> SHOWSLAVE STATUS\G;
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.88.131
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 745
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 639
Relay_Master_Log_File:master-bin.000003
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: 745
Relay_Log_Space: 788
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:
1 row in set(0.00 sec)
ERROR:
No query specified
#出现此错误是sql不合法原因:\G后面不需要再加分号;
mysql> SHOWSLAVE STATUS\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.88.131
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:master-bin.000003
Read_Master_Log_Pos: 745
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 639
Relay_Master_Log_File:master-bin.000003
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: 745
Relay_Log_Space: 788
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:
1 row in set(0.00 sec)
四、复制的管理与维护:
1、监控复制
SHOW MASTER STATUS
SHOW BINLOG EVENTS
SHOW BINARY LOGS
SHOW SLAVE STATUS
2、slave是否发了复制延迟
mysql> SHOW SLAVE STATUS\G查看以下项:
Seconds_Behind_Master: 0
3、确定master/slave节点数据是否一致
(1)表自身的checksum
mysql> help checksum table
(2)使用percona-tools中的pt-table-checksum
4、数据不一致时的修改方法
重复设定复制机制
使用mysqldump从master导出slave不同的数据
参考mysql备份和恢复
5、为从库设定新的主库
在从库停止复制线程,而后重新设定CHANGE MASTER TO命令即可;
mysql> stop slave;
Query OK, 0 rows affected (0.10 sec)
mysql> GRANT REPLICATION SLAVE,REPLICATIONCLIENT ON *.* TO 'repluser'@'HOST' IDENTIFIED BY 'REPLPASS';
6、跟复制功能相关的文件:
master.info:保存slave连接master时所需要信息;纯文本文件;
relay-log.info:保存了当前slave节点上(主节点上的)二进制日志和当前节点中继日志的对应关系;
[root@test mysql]# cat master.info
15
master-bin.000003
745
192.168.88.131
repluser
testpass
3306
60
0
0
[root@test mysql]# cat relay-log.info
./relay-log.000003
252
master-bin.000003
745
7、清理二进制日志:PURGE命令
实例:
mysql> show binary logs;
+-------------------+-----------------+
|Log_name | File_size |
+-------------------+-----------------+
|master-bin.000001 | 19924 |
|master-bin.000002 | 775387 |
|master-bin.000003 | 387 |
|master-bin.000004 | 527 |
+-------------------+-----------------+
4rows in set (0.00 sec)
mysql>purge binary logs to 'master-bin.000002';
QueryOK, 0 rows affected (0.08 sec)
mysql>show binary logs;
+-------------------+-----------------+
|Log_name | File_size |
+-------------------+-----------------+
|master-bin.000002 | 775387 |
|master-bin.000003 | 387 |
|master-bin.000004 | 527 |
+-------------------+-----------------+
3rows in set (0.00 sec)
mysql>quit
Bye
[root@testmysql]# ls
ibdata1 master-bin.000002 master-bin.index mysql relay-log.000006 test ib_logfile0 master-bin.000003 master.info mysql.sock relay-log.index ib_logfile1 master-bin.000004 mydb relay-log.000005 relay-log.info
[root@test mysql]# catmaster-bin.index
./master-bin.000002
./master-bin.000003
./master-bin.000004
8、提升从服务器为主服务器
计划内提升一个从库为主库:
(1) 停止向老的主库写入数据;
(2) 让计划提升为主库的从库赶上主库;
(3) 提升从库为主库
(4) 修改其它从库的指向
计划外提升一个从库为主库:
(1) 确定哪个从库的数据为最新最全;
Master_Log_File: master1-bin.000002
Read_Master_Log_Pos:245
(2) 等待所有的从库执行从主库那复制而来的生成的中继日志;
(3) 在提升为主库的从库上STOP SLAVE;而后,让各从库指向新的主库;
(4) 再次比较主库和各从库上的两个参数:
Master_Log_File:master1-bin.000002
Read_Master_Log_Pos:245
9、相对理想的主从复制配置:
(1)、master上配置:
sync_binlog= 1
确保每次事务提前之前都能将二进制日志同步磁盘上;
对于InnoDB存储引擎的场景:
innodb_flush_logs_at_trx_commit
innodb_support_xa=1
(2)、slave上配置:
read_only= 1
sync_master_info= 1
sync_relay_log = 1
sync_relay_log_info= 1