一、需求:
主库为 192.168.92.108 ,上面运行多个数据库,其中meibi数据库下的几个业务表需要搭建从库并同步;
从库为 192.168.92.86 ,安装了msyql软件,还没有创建数据库,需要建立slave从库同步主库的几张业务表;
二、操作步骤:
主库master 192.168.92.108 上操作内容:
确定需要建立哪些:
t1
t2
t3
确认数据库 meibi建库语句:
show create databases test;
CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */;
确认主库此时的binlog状态:
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000076 | 192410626 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)
导入需要同步的表的数据:
mysqldump -uroot -p' 123' -S /data/mysql/3307/mysql.sock test t1 t2 t3 > /tmp/
test-20150130.sql
确认主库的server_id参数:
show global variables like 'server_id';
给从库进行授权:
grant replication slave on *.* to 'repl'@'192.168.92.86' identified by 'qZBd0Rzpe3';
flush privileges;
从库master 192.168.92.86 上操作内容:
确认my.cnf配置,并加入下列选项:
replicate-do-table = test.t1
replicate-do-table = test.t2
在从库上创建空数据库,并进行基本设置:
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysqldata/3307/data
创建基本文件:
mysqlplus.sh
mysql_db_start.sh
mysql_db_shutdown.sh
mysql_env.ini
启动从库数据库:
mysql
update mysql.user set password=password('123') where user='root';
delete from mysql.user where user='';
flush privilegs;
exit
mysqlplus.sh
建立数据库并同步;
mysqlplus.sh
创建meibi数据库:
use test;
show tables;
source /hbdata/mysqldata/test-20150130.sql
show tables;
创建主从复制:
| mysql-bin.000076 | 192410626 |
change master to
master_host='192.168.92.108',
master_port=3307,
master_user='repl',
master_password='qZBd0Rzpe3',
master_log_file='
mysql-bin.000076',
master_log_pos=
192410626;
start slave;
show slave status\G
主从同步问题确认与解决
启动主从同步后,过段时间发生的报错;
确认是一些sql执行超过的范围,不影响数据同步,于是采用先跳过,后加入my.cnf中的跳过错误号的方式,避免类似错误,最终确认主从同步正常。