Mysql数据库实现主从同步,看这一篇就够了( 二 )
[root@localhost ~]# mysql -uroot -p123qqq...A...Server version: 5.7.29-log MySQL Community Server (GPL)...mysql> grant replication slave on *.* to mysqluser@"%" identified by "123qqq...A"; //为主数据库授权用户mysqluser,权限为replication、slave,允许客户端地址为所有主机,允许访问所有库所有表*.*,授权用户密码为123qqq...A
3.查看日志信息
[root@localhost ~]# mysql -uroot -p123qqq...A...Server version: 5.7.29-log MySQL Community Server (GPL)...mysql> show master status; //查看binlog记录日志信息的偏移量position+--------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+--------------+----------+--------------+------------------+-------------------+| db128.000001 | 704787 | | | |+--------------+----------+--------------+------------------+-------------------+
配置从服务器1.修改主配置文件
-指定server_id,不允许与主库的server_id值相同
[root@test2 ~]# vim /etc/my.cnf[mysqld]server_id=129...[root@test2 ~]# systemctl restart mysqld
2.确保与主库数据一致
-在主库上备份数据,备份文件拷贝给从库
-在从库上使用备份文件恢复数据
-从库查看备份数据对应的binlog日志信息
主数据库操作:
--master-data: 在备份文件中添加这次备份的数据对应的binlog日志名以及备份后数据的节点编号(偏移量),以便从库同步数据时,可以知道从哪个节点开始同步数据,保证主库与从库数据完全一致.
[root@localhost ~]# mysqldump -uroot -p123qqq...A --master-data test > /root/test.sql //在主库上备份数据库test,并且记录当前备份数据对应的binlog日志信息,备份文件名为test.sql[root@localhost ~]# ls test.sqltest.sql[root@localhost ~]# scp test.sql root@192.168.2.129:/root/
从数据库操作:
[root@test2 ~]# ls test.sqltest.sql[root@test2 ~]# mysql -uroot -p123qqq...A;mysql> create database test; //从库上必须有需要恢复的库,因为要恢复test库,所以先创建空库test[root@test2 ~]# mysql -uroot -p123qqq...A test < /root/test.sql[root@test2 ~]# mysql -uroot -p123qqq...A;mysql> use test;+----------------+| Tables_in_test |+----------------+| lss || money |+----------------+
3.指定主库信息
命令格式:
change master to
master_host="主库IP地址",
master_user="用户名",
master_password="密码",
master_log_file="binlog日志文件名",
master_log_poss=偏移量;
主数据库查看binlog日志名及偏移量:
[root@localhost ~]# mysql -uroot -p123qqq...A...mysql> show master status; //主库查看binlog记录日志信息的日志名及偏移量+--------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+--------------+----------+--------------+------------------+-------------------+| db128.000001 | 2261338 | | | |+--------------+----------+--------------+------------------+-------------------+
从库指定主库信息:
[root@test2 ~]# mysql -uroot -p123qqq...A;...mysql> show slave status;Empty set (0.00 sec)mysql> change master to -> master_host="192.168.2.128", //指定主库IP地址 -> master_user="mysqluser", //主库授权用户 -> master_password="123qqq...A", //授权用户的密码 -> master_log_file="db128.000001", //主库binlog日志文件名 -> master_log_pos=2261338; //备份文件的日志偏移量Query OK, 0 rows affected, 2 warnings (0.02 sec)mysql> start slave; //启动slave进程
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 使用mysqld_exporter监控MySQL并展示数据
- Mysql:下载、安装、部署、修改密码步骤
- 搞定MySQL安装难安装贵问题
- 使用Python+Fabric实现Linux自动化操作
- 什么是链路追踪?分布式系统如何实现链路追踪?
- 高级语言中的语句在汇编中是如何实现的
- MySQL 中 InnoDB 和 MyISAM 的联系与区别
- 双活无共享数据库架构
- 如何实现HTTPS服务器
- ETL数据库迁移工具Kettle的安装配置详解