[mariadb] log-bin server_id=1 log-basename=master1 binlog-format=mixed max_binlog_size=200M expire_logs_days=7
server_id 必须惟一。
log-basename 是指定binlog 的命名规则, binlog 会以它为前缀生成日志,如 master1-bin.000001。
max_binlog_size=200M 生成的log最大值,到达最大值,会从新建立一个,如 master1-bin.000002。
expire_logs_days binlog 过时天数。mysql
而后重启数据库即生效。sql
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'bigs3cret'; GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
用户名: replication_user, 密码: bigs3cret,能够修改为你想要的。数据库
在导出数据库时,先加锁,避免在导出时修改了数据库致使数据不一致。bash
FLUSH TABLES WITH READ LOCK;
MariaDB [(none)]> show master status; +--------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +--------------------+----------+--------------+------------------+ | master1-bin.000001 | 330 | | | +--------------------+----------+--------------+------------------+ 1 row in set (0.000 sec)
当前master 生成的日志文件: master1-bin.000001,
日志位置: 330。网络
mysqldump -uroot -proot --databases cqrs --master-data2 >cqrs_db.sql
这里只导出了须要同步的数据库 cqrs, 固然你也能够导数全部数据库。 cqrs_db.sql 的默认保存位置在 mariadb 程序所在目录的bin 目录下。性能
数据库已导出,同步的位置也记录了,如今解锁数据库,接下来对 master 数据库的修改,都会记录到 binlog。日志
UNLOCK TABLES ;
slave 的 server_id, 必须是惟一的,上面咱们配置了 master 的 server_id=1, 这里的slave 设成2code
[mysqld] datadir=D:/projects/db/mariadb-10.5.8-winx64/data port=3307 character-set-server=utf8 server_id=2
重启 slave 数据库orm
把从 master 导出的 cqrs_db.sql 复制到 slave 程序所在的bin目录下, 而后登陆客户端,执行server
MariaDB [(none)]> source cqrs_db.sql;
查看数据库
MariaDB [cqrs]> show databases; +--------------------+ | Database | +--------------------+ | cqrs | | information_schema | | mysql | | performance_schema | | test | +--------------------+
可看到 cqrs 已经建立了。
CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_USER='replication_user', MASTER_PASSWORD='bigs3cret', MASTER_PORT=3306, MASTER_LOG_FILE='master1-bin.000001', MASTER_LOG_POS=330, MASTER_CONNECT_RETRY=10;
MASTER_LOG_FILE,MASTER_LOG_POS 是在 Master 第4步记录的日志文件名和开始同步的位置信息
start slave;
MariaDB [(none)]> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 127.0.0.1 Master_User: replication_user Master_Port: 3306 Connect_Retry: 10 Master_Log_File: master1-bin.000001 Read_Master_Log_Pos: 1055 Relay_Log_File: 18Q7GVR3CS15BS9-relay-bin.000003 Relay_Log_Pos: 1282 Relay_Master_Log_File: master1-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: 1055 Relay_Log_Space: 1601 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: optimistic SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Slave_DDL_Groups: 5 Slave_Non_Transactional_Groups: 0 Slave_Transactional_Groups: 0 1 row in set (0.000 sec)
须要关注的属性,Slave_IO_Running, Slave_SQL_Running,Slave_SQL_Running_State,和上面一致,说明已经成功了,接下来全部的master数据库的修改都会同步到slave,依赖网络和硬件性能,几乎是毫秒级别的延迟,能知足绝大部分的查询业务。