Mysql 配置主从

1.备份生产数据库:

 mysqldump -uroot -p'1234567'  -R --single-transaction --master-data=2 quartz>quartz.sql
 mysqldump -uroot -p'1234567'  -R --single-transaction --master-data=2 zjzc>zjzc.sql

--master-data 参数:
Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. It causes the dump output to include a CHANGE MASTER 

TO statement that indicates the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates from which the slave should start replicating.
If the option value is 2, the CHANGE MASTER TO statement is written as an SQL comment, and thus is informative only; it has no effect when the dump file is reloaded. If the option value is 1, the 

statement takes effect when the dump file is reloaded. If the option value is not specified, the default value is 1.


使用这个选项来dump 一个master的复制server 来产生一个dump文件,用于创建另一个slave server. 它使dump调用一个

CHANGE MASTER TO 语句来表明binary log 记录的位置(file name 和position)  这些是master server 记录 slvae开始复制的起点




mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000009 | 56763853 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)


test:/root#  grep -i "CHANGE MASTER TO" zjzc.sql 
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=56677224;


注意:这里记住File的值:mysql-bin.000009和Position的值:56677224,后面会用到。


2.在master上创建同步用户
#建立MySQL主从数据库同步用户backup密码1234567

flush privileges;   #刷新系统授权表

#授权用户backup只能从192.168.32.116这个IP访问主服务器192.168.32.6上面的数据库,并且只具有数据库备份的权限
grant replication slave  on *.* to 'backup'@'192.168.32.116' identified by '1234567' with grant option;


3.从服务器设置:

导入数据库
| quartz             |
| zjzc               |

CREATE DATABASE `zjzc` /*!40100 DEFAULT CHARACTER SET utf8 */

CREATE DATABASE `quartz` /*!40100 DEFAULT CHARACTER SET utf8 */

mysql -uroot -p'1234567' -D quartz<quartz.sql

mysql -uroot -p'1234567' -D zjzc<zjzc.sql


change master to master_host='192.168.32.6',master_user='backup',master_password='1234567',master_log_file='mysql-bin.000009',master_log_pos=56677224;



4.


1、什么是relay log

The relay log, like the binary log, consists of a set of numbered files containing events that describe database changes,

relay log 像binary log ,包含了改变数据库的事件的文件

and an index file that contains the names of all used relay log files.

index 文件包含所有使用的relay log文件的名字

---relay-log 默认数据目录:




(stop slave)后再打开(start slave)



错误信息:
                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --

replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).


原因:
| server_id     | 135   |server_id一致了,修改server_id


修改slave的/etc/my.conf 的server-id=136


重启mysql数据库,启动slave


slave:/data01/mysql# ls -ltr *relay*
-rw-rw---- 1 mysql mysql    174 Jun  4 15:08 mysqld-relay-bin.000002
-rw-rw---- 1 mysql mysql     52 Jun  4 15:08 mysqld-relay-bin.index
-rw-rw---- 1 mysql mysql     67 Jun  4 15:13 relay-log.info
-rw-rw---- 1 mysql mysql 989783 Jun  4 15:13 mysqld-relay-bin.000003


mysql> show variables like 'relay_log_info_file';
+---------------------+----------------+
| Variable_name       | Value          |
+---------------------+----------------+
| relay_log_info_file | relay-log.info |
+---------------------+----------------+
1 row in set (0.01 sec)


从数据库过滤:
basedir=/usr/local/mysql
datadir=/data01/mysql
socket=/data01/mysql/mysql.sock
user=mysql

slow_query_log=ON
long_query_time=2

server-id=136
log-bin=/data01/mysqllog/binlog/mysql-bin
binlog_format=MIXED
max_binlog_size = 512M
binlog_cache_size = 128K

character-set-server=utf8
default-storage-engine=INNODB 
innodb_file_per_table=1 

symbolic-links=0
max_connections=1024
log-error=/var/log/mysql/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

relay-log-purge=1
replicate-ignore-db = mysql
replicate-ignore-db = information_schema

skip-slave-start

slave复制进程不随mysql启动而启动skip-slave-start参数

                      
replicate-ignore-db =performance_schema  --不需要过滤


replicate-do-db配置在MySQL从库的my.cnf文件中,可以指定只复制哪个库的数据


mysql主从同步
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

如果上面为yes表示同步成功。



如果磁盘空间满了,那么slave就会报:

2015-06-04 15:49:00 21224 [Warning] Disk is full writing './mysqld-relay-bin.000003' (Errcode: 28 - No space left on device). Waiting for someone to free space...
2015-06-04 15:49:00 21224 [Warning] Retry in 60 secs. Message reprinted in 600 secs




grant replication slave  on *.* to 'backup'@'%' identified by 'a2p13mvh' with grant option;



slave:/root/20150604/zjzc# grep -i "CHANGE MASTER TO" zjzc_20150604.sql 
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=82701905;

slave:/root/20150604/quartz# grep -i "CHANGE MASTER TO" quartz_20150604.sql 
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=82701249;

一个MASTER库里的两个SCHEMA的 MASTER_LOG_POS不一致:


这种是由于备份的时候分开了,没法搞了,需要备份在一起:

  mysqldump --user root --password=myrootpassword zjzc quartz > db_test.sql 

导出:
 mysqldump -uroot -p'1234567'  -R --single-transaction --master-data=2 --all-database>slave.sql

导入:
mysql -uroot -p'1234567' <slave.sql

  






change master to master_host='10.2.6.5',master_user='backup',master_password='a2p13mvh',master_log_file='mysql-bin.000010',master_log_pos=109578882;



-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=109578882;

原文地址:https://www.cnblogs.com/hzcya1995/p/13351631.html