双机热备概念:
保持两个数据库的状态自动同步。对任何一个数据库的操做都自动应用到另一个数据库,始终保持两个数据库数据一致。mysql
使用场景/解决问题
能够作灾备,其中一个坏了能够切换到另外一个。
能够作负载均衡,能够将请求分摊到其中任何一台上,提升网站吞吐量。sql
msyql热备工做原理图数据库
配置要求:
1.须要服务器两台A,B
2.服务器上安装最好是相同版本的mysql数据库(建议)安全
安装步骤
[假定A服务器的ip为10.1.2.131] A做为主(master)
[假定B服务器的ip为10.1.2.132] B做为从(slave)bash
A主服务器的配置:服务器
1.登录A主数据库 -》建立数据库 -》 添加受权用户架构
[root@10-1-2-131 ~]# mysql -uroot -p #进入MySQL控制台 mysql>grant replication slave on *.* to 'user'@'10.1.2.132' identified by '10isp.com' with grant option; #受权用户user只能从B从10.1.2.132这个IP访问主服务器10.1.2.131上面的数据库,而且只具备数据库备份的权限 mysql>flush privileges; #刷新系统受权表
10.1.2.132是B从服务器的ip地址。 只容许B登陆,安全。
用户名: user
密 码: 10isp.com
用户名和密码在B上面要用。负载均衡
2.在mysql的配置文件/etc/my.cnf中有添加以下参数。ide
[mysqld] log-bin=mysql-bin #开启主服务器的binarylog server-id=1 #服务器id #binlog-do-db=demo #须要备份的数据库 若是备份多个数据库就添加多条,不添加就表明所有数据库备份 binlog-ignore-db=information_schema #忽略的数据库 information_schema #binlog-ignore-db=performance_schema #忽略的数据库 performance_schema #binlog-ignore-db=sys #忽略的数据库 sys binlog-ignore-db=mysql #忽略的数据库 mysql
3.查看A主数据库状态网站
mysql>show master status
查询结果显示:
+----------------+--------+-------------+------------------------+ | File |Position| Binlog_Do_DB| Binlog_Ignore_DB | +----------------+--------+-------------+------------------------+ |mysql-bin.000001| 154 | |mysql,information_schema| +----------------+--------+-------------+------------------------+
以上的FIEL二进制文件和文件位置position 这两项在配置从服务器时候要用
B从服务器的配置:
1.在mysql的配置文件/etc/my.cnf中有添加以下参数。
[mysqld] log-bin=mysql-bin #开启主服务器的binarylog server-id=2 #从服务器id 若是是多机备份就注意这里的id的指定 #replicate-do-db=demo #须要备份的数据库 若是备份多个数据库就添加多条,不添加就表明所有数据库备份 replicate-ignore-db=information_schema #忽略的数据库 information_schema #replicate-ignore-db=performance_schema #忽略的数据库 performance_schema #replicate-ignore-db=sys #忽略的数据库 sys replicate-ignore-db=mysql #忽略的数据库 mysql
2.重启mysql服务
[root@10-1-2-132 ~]# /etc/init.d/mysqld restart
3.登录B从数据库-》配置主A的mysql用户信息
[root@10-1-2-132 ~]# mysql -uroot -p #进入MySQL控制台 mysql>change master to master_host='10.1.2.131',master_port=3306,master_user='user',master_password='10isp.com',master_log_file='mysql-bin.000001',master_log_pos=154;
在B数据库上配置主数据库的信息 IP:10.1.2.131 帐号:user 密码:10isp.com
master_host : A主服务器的ip地址
master_log_file :A主服务器上的二进制文件明
master_user : A主服务器受权的用户名
master_password : A主服务器受权的密码
master_log_pos :二进制文件开始备备份的起始位置
以上参数配置A主的时候都有提到。
4.启动B从服务器的两条线程
mysql>start slave;
5.查询B从的线程状态
mysql>show slave status
如下为B从的数据库线程的日志信息,
主要查看两条标红的线程是否开启,若是两条线程都YES 基本就完成了全部的配置
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.1.2.131 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 1067 Relay_Master_Log_File: mysql-bin.000001 SLAVE_IO_RUNNING: YES SLAVE_SQL_RUNNING: YES Replicate_Do_DB: Replicate_Ignore_DB: information_schema,mysql 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: 154 Relay_Log_Space: 1275 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: 7c681299-8e5b-11e8-925b-facd2dcb4a00 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates 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 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
以上是全部主从配置步骤,具体实施遇到问题可参考:http://py.10isp.com/?p=634
在主从配置中遇到的各类奇葩问题的汇总和解决方案
第一种
报错: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
解决方法:
这个错误提示。即主从架构中使用了相同的UUID。检查server_id系统变量,已是不一样的设置,那缘由是?接下来为具体描述。
master_mysql> show variables like 'server_id';
slave_mysql> show variables like 'server_id';
查看是不一样的。
可是查看/mysql/data/auto.cnf发现里面的UUID是哦相同的。缘由是mysql是直接从节点1上拷贝过来而致使。
解决:mv /mysql/data/auto.cnf /mysql/data/auto.cnf.bak 重启mysql解决 或者修改 server UUID