当前位置:网站首页>MYSQL主从搭建
MYSQL主从搭建
2022-07-17 01:02:00 【Doc_ACwhite】
MYSQL主从的原理:

1.首先需要两台服务器【可以和同事/朋友一起搭建】
找到mysql的配置文件进行编辑修改:( /etc/my.cnf)
主库修改:
在主库的mysqld下面追加这一段
#主服务器唯一ID
server-id=1
#启用二进制日志
log-bin=mysql-bin
#设置logbin格式
binlog_format=STATEMENT
# 设置不要复制的数据库,可以设置多个
binlog-ignore-db=mysql
#设置需要同步的数据库(未创建的数据库)
binlog-do-db=table1
#binlog-do-db设置后,可以不设置binlog-ignore-db
退出并重载配置:
#重载mysql配置
service mysqld restart
进入mysql终端,在主库的mysql上注册从库的账号及密码:
#创建远程连接账号密码
GRANT REPLICATION SLAVE ON *.* TO 'slave_username'@'%' IDENTIFIED BY 'slave_password';
#刷新mysql系统权限相关表
flush privileges;
#查看是否建立完成
select user from mysql.user where user='slave_password';【slave_username从库账号,slave_password从库密码】

配置完后可以检查主库的配置状态
show master status;
- File:binlog日志的名字
- Position :接入点
- Binlog_Do_DB :需要复制的数据库
- Binlog_Ignore_DB :不需要复制的数据库
- Executed_Gtid_Set:写入binlog日志的事务产生的Gtid的集合

切换从服务器打开从服务器的mysql配置
在mysqld下面加上这一段,
#主服务器唯一ID
server-id=2
#启用中继日志
relay-log=mysql-relay
#启用二进制日志
log-bin=mysql-bin
重载配置:
show master status;
如果曾经配置过主从,则需要先重启服务器再进行配置
#停止从服务
stop slave;
#重置master
reset master;

然后进行主从连接
CHANGE MASTER TO MASTER_HOST='主服务器IP地址', //服务器ip地址
MASTER_USER='远程连接账号', //slave_username
MASTER_PASSWORD='远程连接密码', //slave_password
MASTER_LOG_FILE='主库binlog日志名称', //FIle
MASTER_LOG_POS=主库接入点; //postion

主从连接成功后开启同步功能
# 开启同步功能
start slave;
#查看从库状态
show slave status\G;


注意:Slave_IO_Running和Slave_SQL_Running的状态都为Yes时,说明从库配置成功。
主从测试
(1)、在主库上创建jgyw模式,即:
create schema jgyw;

(2)、在主库上的jgyw模式里面创建comm_config表,即:
use jgyw;
CREATE TABLE comm_config (configId varchar(200) NOT NULL ,configValue varchar(1024) DEFAULT NULL ,description varchar(2000) DEFAULT NULL ,PRIMARY KEY (configId)) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

(3)、在主库上往comm_config表中插入一条记录,即:
insert into comm_config(configId, configValue, description) values('name', '架构与我', '测试一下');
(4)、在从库上查看模式,即:
show schemas;
结果为:
+--------------------+
| Database |
+--------------------+
| information_schema |
| jgyw |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
说明jgyw模式同步到从库了
(5)、在从库上查看jgyw模式下的表及数据,即:
use jgyw;
show tables;
结果为:
+----------------+
| Tables_in_jgyw |
+----------------+
| comm_config |
+----------------+
1 row in set (0.00 sec)
说明表也同步好了,再查看数据,即:
select * from comm_config;
结果为:
+----------+--------------+--------------+
| configId | configValue | description |
+----------+--------------+--------------+
| name | 架构与我 | 测试一下 |
+----------+--------------+--------------+
1 row in set (0.00 sec)
说明数据也同步过来了。
边栏推荐
- 04_服务注册Eureka
- Bisenetv2 face segmentation
- [template record] string hash to judge palindrome string
- Rewrite equals why rewrite hashcode
- oracle 查询非自增长分区的最大分区
- Face key point detection
- Using gatekeeper to restrict kubernetes to create specific types of resources
- ES6学习笔记——B站小马哥
- Powertor500t reports an error 0x01806803
- Leetcode: dynamic programming [basic problem solving]
猜你喜欢

ES6 learning notes - brother Ma at station B
![[MySQL] MHA high availability](/img/d3/d9830f3c331193fd40b8f00ebe35fa.png)
[MySQL] MHA high availability

Data source object management (third-party object resources) & load properties file

Automatic assembly & set injection

Vscode+ros2 environment configuration

MySQL interview questions (2022)
![2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )](/img/e4/ff7f1e19583f42377307de7291f870.png)
2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )
![[MCU simulation] (XX) org - set start address](/img/9e/4e44dd779b0de28a190d86fbb1c2c0.png)
[MCU simulation] (XX) org - set start address

JDBC connection to MySQL database

05_ Service call ribbon
随机推荐
Note: light source selection and Application
Specifications、多表查询基础
Vs code problem: launch:program '... \ vscode\launch. exe‘ dose not exist
leetcode:78. 子集
MySQL面试题(2022)
[MCU simulation] (VI) addressing mode - index addressing and relative addressing
MySQL log management and full backup incremental backup and recovery
论文阅读:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
[MCU simulation] (XVIII) control transfer instructions - empty operation instructions
05 central processing unit
mysqldump: [Warning] Using a password on the command line interface can be insecure.
367. 有效的完全平方数(入门必会)
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
Data source object management (third-party object resources) & load properties file
【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表
Comparison between redis and other databases
04_ Service registration Eureka
[template record] string hash to judge palindrome string
Shell script receives and returns parameters
Unity解决同材质物体重叠产生Z-Fighting的问题