Mysql主从复制
2021/6/18 19:27:24
本文主要是介绍Mysql主从复制,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Mysql主从复制
1.前提条件
拥有两台或以上的mysql服务器,一台为主服务器一台为从服务器
服务器参数:
主服务器(master):192.168.1.2 端口:3306 从服务器(slave) : 192.168.1.3 端口:3306
2.修改配置
2.1 主服务器配置
#vim /etc/my.cnf [mysqld] log-bin=mysql-bin //[必须]启用二进制日志 server-id=2 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
2.2 从服务器配置
#vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[不是必须]启用二进制日志 server-id=3 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
2.3 重启两台服务器
/etc/init.d/mysql restart
2.4 在主服务器上建立帐户并授权slave:
#登录mysql mysql -uroot -p123456 #创建新用户 create user 'test'@'%' identified by '123456'; #主从复制的权限: grant replication slave on *.* to 'test'@'%' identified by '123456'; #(@后面的ip地址为允许连接的客户端的ip地址,如果改为 '%',就表示客户端没有ip地址的限制)
2.5 查看主服务器状态
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 1234 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) #执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
2.6 配置从服务器Slave
#配置同步信息 mysql> change master to master_host='192.168.1.2',master_user='test',master_password='123456', master_log_file='mysql-bin.000002',master_log_pos=1234; #//启动从服务器复制功能 mysql> start slave;
2.7 检查从服务器复制功能状态
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 47.242.12.66 Master_User: test Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 967 Relay_Log_File: DESKTOP-82QLLRC-relay-bin.000002 Relay_Log_Pos: 1133 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes #必须为YES否则有错误 Slave_SQL_Running: Yes #必须为YES否则有错误 Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates 1 row in set (0.00 sec)
3 主从服务器测试
#主服务器 mysql> use demo; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+ | Tables_in_demo | +----------------+ | t_user | +----------------+ 1 row in set (0.00 sec) mysql> create table test(id int(3),name char(10)); Query OK, 0 rows affected (0.01 sec) mysql> insert into test values(001,'小明'); Query OK, 1 row affected (0.00 sec) #从服务器 mysql> use demo; Database changed mysql> show tables; +----------------+ | Tables_in_demo | +----------------+ | t_user | | test | +----------------+ 2 rows in set (0.00 sec) mysql> select * from test; +------+--------+ | id | name | +------+--------+ | 1 | 小明 | +------+--------+ 1 row in set (0.00 sec)
这篇关于Mysql主从复制的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-25MySQL报错Duplicate entry '0' for key 'PRIMARY'
- 2024-05-29阿里 Canal 实时同步 MySQL 增量数据至 ClickHouse 数据库
- 2024-05-24在Linux下管理MySQL的大小写敏感性
- 2024-04-26MySQL查出时间比实际晚8小时的解决方案
- 2024-04-01JPA不识别MySQL的枚举类型
- 2024-03-30mysql数据库表卡死解决方法
- 2024-03-15MySQL多数据源笔记5-ShardingJDBC实战
- 2024-03-11natural join mysql
- 2024-03-11关于VS2017,VS2015 中利用 EF使用Mysql 不显示数据源问题解决方案
- 2024-02-26mysql 阿里云xb后缀备份文件恢复-icode9专业技术文章分享