mysql 主从备份

主节点
my.cnf增加配置
log-bin=mysql-bin
log-bin-index=mysql-bin.index  
server-id       = 1
连接mysql,执行如下语句
create user repl_user
GRANT REPLICATION SLAVE on *.* to repl_user IDENTIFIED by 'fuwenchao'



从节点
server-id=2
relay-log=slave-relay-bin
relay-log-index=slave-ralay-bin.index
master-host=pacteralinux.chinacloudapp.cn
master-user=repl_user    #同步用户帐号      
master-password=fuwencaho_hot



主节点

启动A服务器mysql服务。

输入show master status;          #查看主服务器的状态


mysql> show master status;

+---------------------+----------+--------------+------------------+

| File         |  Position  |  Binlog_Do_DB   | Binlog_Ignore_DB |
+-----------------+----------+--------------+------------------+
| mysql-bin.000008 |     106   | cdn      |    manual,mysql   |
+-----------------+----------+--------------+------------------+


从节点
重启mysql服务器 service mysqld restart
失败:报错如下:
[root@pacteralinux etc]# service mysqld restart
Shutting down MySQL..[  OK  ]
Starting MySQL....The server quit without updating PID file (/mnt/resource/mysqldate/pacteralinux.pid).[FAILED]
注释掉
master-host=pacteralinux.chinacloudapp.cn
master-user=repl_user    #同步用户帐号      

master-password=fuwencaho_hot

见代码:

[root@pacteralinux //]# more /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
socket =/mnt/resource/mysqldate/mysql.sock
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir =/usr/local/mysql
datadir =/mnt/resource/mysqldate
port =3306
socket =/mnt/resource/mysqldate/mysql.sock
default-storage-engine=MyISAM
server-id=2
relay-log=slave-relay-bin
relay-log-index=slave-ralay-bin.index
replicate-do-db=mysqldb
#master-host=A's hostname or IP
#master-user=repl_user    #��骁�ㄦ�峰���
#master-password=fuwenchao
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
lower_case_table_names= 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
innodb_use_sys_malloc =1


重启成功

show slave status返回为空


从节点指定主节点
mysql> change master to
   -> master_host='A'IP',
   -> master_port=3306,
   -> master_user='repl_user',
   -> master_password='fuwenchao';
Query OK, 0 rows affected, 2 warnings (3.51 sec)
mysql> start slave;

Query OK, 0 rows affected (0.25 sec)


至此,主从备份完成,两个数据库保持同步,延迟几秒


同步故障解决

MySQL同步故障:" Slave_SQL_Running:No" 两种解决办法

1.首先停掉Slave服务:slave stop
到主服务器上查看主机状态:
记录File和Position对应的值。
3.到slave服务器上执行手动同步:

mysql> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000020 | 135617781 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)

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

注:这种办法可能会导致从服务器上的数据不完整,如从服务器一直出错,但主服务器日志文件一直在增加,过好长时间,再直接从主服务器上取日志位置,可能会造成错误期间的数据无法更新到从服务器中.这里建议采用下面的这种办法(将错误语句直接跳过).

解决办法II:

mysql> slave stop;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> slave start;

set GLOBAL SQL_SLAVE_SKIP_COUNTER=N,用来跳过备机的一条或N条出错的复制语句。然后重新start slave即可。




你可能感兴趣的:(mysql,高可用,主从备份)