发布网友 发布时间:2022-04-23 05:44
共3个回答
懂视网 时间:2022-04-29 22:04
开始搭建前有个mysql复制原理的基础知识需要补充:
mysql进行主备复制使用到了三个线程:
1.主库上的转存储线程:
会将mysql server提交的事务写入到二进制文件中,这个二进制文件就叫做binlog。
2.备库上的连接线程:
备库启动后,负责和主库通信,读取binlog,同时,将binlog存储进自己的一个叫中继日志的relaylog中。
3.备库上的relaylog重放线程:
此线程会将relaylog中的事件在备库上进行回放,说白点就是重新执行一次
二、搭建步骤
1./etc新增文件mysqld_multi.cnf
将/user/share/mysql/my-innodb-heavy-4G.cnf文件复制到/etc,重新命名为xxx.cnf(任何你想要的名字)
在配置文件中新增三个实例
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = multi_admin
password = multipass
log = /var/log/mysqld_multi.log
[mysqld3307]
port = 3307
pid-file = /var/lib/mysql3307/mysql3307.pid
socket = /var/lib/mysql3307/mysql3307.sock
datadir=/var/lib/mysql3307
user=mysql
set-variable=max_connections=27000
log_bin = mysql-bin
server_id = 3307
[mysqld3308]
port = 3308
pid-file = /var/lib/mysql3308/mysql3308.pid
socket = /var/lib/mysql3308/mysql3308.sock
datadir=/var/lib/mysql3308
user=mysql
set-variable=max_connections=28000
log_bin = mysql-bin
server_id = 3308
relay_log = /var/lib/mysql3308/mysql-relay-bin
log_slave_updates = 1
read_only = 1
[mysqld3309]
port = 3309
pid-file = /var/lib/mysql3309/mysql3309.pid
socket = /var/lib/mysql3309/mysql3309.sock
datadir=/var/lib/mysql3309
user=mysql
set-variable=max_connections=29000
log_bin = mysql-bin
server_id = 3309
relay_log = /var/lib/mysql3309/mysql-relay-bin
log_slave_updates = 1
read_only = 1
这里我会将将mysqld3307这个实例做为主库,mysqld3308和mysql3309这个实例作为备库
2.做好了配置之后开启两个实例:
mysqld_multi --defaults-file=/etc/mysql/mysqld_muti.cnf start
3.开启复制前主库的准备工作:
1.在主库上增加一个复制账号:
使用sock文件登陆mysql:
mysql -uroot -p -S /var/lib/mysql3307/mysql3307.sock(这个套接字文件还记得吗,是在之前定义实例的时候定义的)
mysql>grant replication slave,replication client on *.* to replication@‘localhost‘ identified by ‘replication‘;
mysql>flush privileges;
查看主库上的binlog是否开启:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 1001 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
4.开启复制:
登陆到备库:
mysql -uroot -p -S /var/lib/mysql3308/mysql3308.sock
change master to master_host = "localhost",
master_user = ‘replication‘,
master_password = ‘replication‘,(你之前在主库上创建复制账号时指定的)
master_port = 3306;
start slave;
查看复制是否开始工作:
mysql> show slave status G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: localhost
Master_User: replication
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 622
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 767
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 622
Relay_Log_Space: 922
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:
1 row in set (0.00 sec)
当看到Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes证明io通信线程和sql回放线程都已经启动。至此,主备复制结构配置完成
5.进行正常主从测试:
在mysql3308数据库停止复制
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: localhost
Master_User: replication
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 408
Relay_Log_File: mysql-relay-bin.000012
Relay_Log_Pos: 553
Relay_Master_Log_File: mysql-bin.000005
在mysql3309数据库停止复制
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: localhost
Master_User: replication
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 316
查看mysql日志情况:
150510 1:33:39 [Note] Error reading relay log event: slave SQL thread was killed
150510 1:33:39 [Note] Slave I/O thread killed while reading event
150510 1:33:39 [Note] Slave I/O thread exiting, read up to log ‘mysql-bin.000005‘, position 408
150510 1:35:41 [Note] Error reading relay log event: slave SQL thread was killed
150510 1:35:41 [Note] Slave I/O thread killed while reading event
150510 1:35:41 [Note] Slave I/O thread exiting, read up to log ‘mysql-bin.000006‘, position 316
在这期间,主库mysql3307进行了flush logs操作,重新生成了mysql-bin日志,并对表进行添加,删除操作。然后启动从库的复制,进行查看。
150510 1:42:48 [Note] Slave SQL thread initialized, starting replication in log ‘mysql-bin.000005‘ at position 408, relay log ‘/var/lib/mysql3308/mysql-relay-bin.000012‘ position: 553
150510 1:42:48 [Note] Slave I/O thread: connected to master ‘replication@localhost:3307‘,replication started in log ‘mysql-bin.000005‘ at position 408
150510 1:43:04 [Note] Slave SQL thread initialized, starting replication in log ‘mysql-bin.000006‘ at position 316, relay log ‘/var/lib/mysql3309/mysql-relay-bin.000015‘ position: 461
150510 1:43:04 [Note] Slave I/O thread: connected to master ‘replication@localhost:3307‘,replication started in log ‘mysql-bin.000006‘ at position 316
说明:从库从停止的时间点重新补回了停止期间的所有数据。
6.进行异常测试:
停掉从库mysql3308和mysql3309的复制,并对主库进行日志清除操作:
flush logs
delete from sky where id=‘2000‘;
purge binary logs to ‘mysql-bin.000010‘;
然后重新启动从库的复制,日志进行报错,数据丢失,主从失败。
150510 1:50:53 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file‘, Error_code: 1236
解决方法:
只能停止从数据库,然后重新在从数据库中CHANGE MASTER TO 开始,指向正确的二进制文件及偏移量
change master to master_log_file=‘mysql-bin.000001‘,master_log_pos=106;
之后的数据会恢复正常主从同步。
本文出自 “秋天的童话” 博客,请务必保留此出处http://wushank.blog.51cto.com/34095/19929
mysql主备复制搭建(使用mysqld_muti)
标签:mysql
热心网友 时间:2022-04-29 19:12
这种架构一般用在以下三类场景
1. 备份多台 Server 的数据到一台如果按照数据切分方向来讲,那就是垂直切分。比如图 2,业务 A、B、C、D 是之前拆分好的业务,现在需要把这些拆分好的业务汇总起来备份,那这种需求也很适用于多源复制架构。实现方法我大概描述下:业务 A、B、C、D 分别位于 4 台 Server,每台 Server 分别有一个数据库来隔离前端的业务数据,那这样,在从库就能把四台业务的数据全部汇总起来,而不需要做额外的操作。那没有多源复制之前,要实现这类需求,只能在汇总机器上搭建多个 MySQL 实例,那这样势必会涉及到跨库关联的问题,不但性能急剧下降,管理多个实例也没有单台来的容易。
2. 用来聚合前端多个 Server 的分片数据。
同样,按照数据切分方向来讲,属于水平切分。比如图 3,按照年份拆分好的数据,要做一个汇总数据展现,那这种架构也非常合适。实现方法稍微复杂些:比如所有 Server 共享同一数据库和表,一般为了开发极端透明,前端配置有分库分表的中间件,比如爱可生的 DBLE。
3. 汇总并合并多个 Server 的数据
第三类和第一种场景类似。不一样的是不仅仅是数据需要汇总到目标端,还得合并这些数据,这就比第一种来的相对复杂些。比如图 4,那这样的需求,是不是也适合多源复制呢?答案是 YES。那具体怎么做呢?
热心网友 时间:2022-04-29 20:30
一、准备3台 linux安装好mysql5.7
master1:可以5.6,其他版本没测
master2:可以5.6,其他版本没测
slave:必须是5.7版本
二、配置master1
1、修改my.cnf
[mysql]
default-character-set=utf8
[mysqld]
server_id = 1
log-bin=master1-bin
binlog-do-db=dy_test
2、create user repl;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'youngsun';
flush privileges;
3、重启mysql 服务:service mysqld restart
4、show master status;//这里在后面change master的时候要用到
master-bin.000001 106
三、配置master2
1、my.cnf
[mysql]
default-character-set=utf8
[mysqld]
server_id =2