来源:互联网
在大型网站架构中,MySQL主从配置是必不可少的,尤其对于Drupal这样数据库访问频繁的框架极为重要。基本的MySQL主从配置大家都比较熟悉,但是在主从配置的结构中,由于多种原因,主从配置经常出现无法同步,以及MySQL由于主从配置的原因而崩溃或者Down机,这里就本人的经验做一些简单介绍。
MySQL主从配置的搭建
具体步骤:
1. 安装MySQL。首先要在两台服务器上安装MySQL,完成之后应该确认能否两台服务器能否互相访问。 这是因为缺省的my.cnf设置有
bind-address = 127.0.0.1,这条语句应该被注释掉。
2. 创建账号。数据库中缺省的帐户的host值是localhost,所以应该创建一个可以远端访问的帐号。比如:root@’%’ or root@10.x.x.x
3. 配置主Server。
在主服务器(数据库源)上的my.cnf配置[mysqld]之后加入如下条目:
log_bin = /var/log/mysql/mysql-bin.log server-id = 1 //1代表主数据库(源) 2代表辅数据库(目的) binlog_do_db = testmirror //要做同步的数据库名字,可以是多个数据库,之间用分号分割。
4. 配置从Server。
在辅服务器上的my.cnf中加入如下内容
server-id = 2 //2代表辅 master-host = xx.xx.xx.xx //主服务器的IP地址 master-user = //主服务器的用户名 master-password = //数据库密码 master-port = 3306 master-connect-retry = 10 //每个10秒钟同步一次 replicate-do-db = testmirror //需要同步的库名字
5. 账号权限设置。
辅服务器将使用主服务器上的root@%帐户登陆主服务器与其发生通信,除了这个账户应该在主上真实存在,
这个账户应该有两个属性:REPLICATION SLAVE 和 READ。
GRANT REPLICATION SLAVE ON *.* TO root@'%' IDENTIFIED BY 'password';
6.主服务器上运新mysql命令
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000015 | 106 | testmirror | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
记住这些参数。
7. 在辅服务器上运新如下命令
mysql> show slave status; ----------------------------------------------------------------------------------------------------------+ Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed| Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert |Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | ---------------------------------------------------------------------------------------------------------+ | Waiting for master to send event | xx.xx.xx.xx | root | 3306 | 10 | mysql-bin.000015 | 384 | ubuntudev-relay-bin.000002 | 529 | mysql-bin.000015 |Yes | Yes | testmirror | | | | | | 0 | | 0 | 384 | 688 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | --------------------------------------------------------------------------------------------------------+ mysql> show processlist; +-----+-------------+-----------+------+---------+------+---------------------------------------------+ | Id | User | Host | db | Command | Time | State |Info +-----+-------------+-----------+------+---------+------+----------------------------------------------+ | 405 | root | localhost | NULL | Query | 0 | NULL |show processlist | | 423 | system user | | NULL | Connect | 332 | Waiting for master to send event | NULL | 424 | system user | | NULL | Connect | 72 | Has read all relay log; waiting for the slave I/O thread to update it | NULL +-----+-------------+-----------+------+---------+------+--------------------------------------------------+
3 rows in set (0.00 sec)
重要的是查看 Slave_IO_Running | Slave_SQL_Running两个值,必须都是YES才可以, 但是通常不是YES。
MySQL主从不同步问题解决
1.首先停掉Slave服务:
mysql> slave stop
2.到主服务器上查看主机状态:
记录File和Position对应的值。
mysql> show master status; +------------------+-----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+-----------+--------------+------------------+ | mysql-bin.000020 | 135617781 | | | +------------------+-----------+--------------+------------------+ 1 row in set (0.00 sec)
3.到slave服务器上执行手动同步:
mysql> change master to > master_host='master_ip', > master_user='user', > master_password='pwd', > master_port=3307, > master_log_file='mysql-bin.000020', > master_log_pos=135617781; 1 row in set (0.00 sec) mysql> slave start; 1 row in set (0.00 sec)
再次查看slave状态发现:
Slave_IO_Running: Yes Slave_SQL_Running: Yes ... Seconds_Behind_Master: 0
我们知道,因为DATA REPLICATION机制完全是基于在主上执行的增量SQL要被传播到辅服务器上,并且被成功运行。这就势必要求:在运行此机制前,主辅数据库中数据是一致的;以及在运行此机制中,辅数据库禁止来自其他的SQL(非主上传播过来SQL)的写操作。但是在运行中仍然可能遇到不一致的产生,这会导致通信无法正常继续下去。因此一旦主从出现问题,首先应该解决同步位置的问题,修复丢失的数据。
MySQL主从配置的其他相关问题,后续文章中陆续跟大家再谈。
声明: 本站所有文章欢迎转载,所有文章未说明,均属于原创,转载均请注明出处。
本文有效链接: http://www.drupal001.com/2012/03/mysql-master-slave-troubles/
版权所有: Drupal与高性能网站架构 http://www.drupal001.com