当前位置:网站首页>从零开始搭建MySQL主从复制架构
从零开始搭建MySQL主从复制架构
2022-08-03 16:01:00 【InfoQ】
概述
MySQL安装

- Product Version: MySQL数据库版本号
- Operating System: 操作系统类型
- OS Version: 操作系统版本号

删除历史版本
rpm -qa|grep mysql
rpm -e –nodeps filename
find / -name mysql
rm -rf filename解压安装包
# 首先通过cd命令进入到安装包所在的目录,然后执行
cd /opt
tar –zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz[[email protected] opt]# ll
total 649172
drwxr-xr-x. 9 root root 129 Aug 2 08:20 mysql-5.7.29-linux-glibc2.12-x86_64
-rw-r--r--. 1 root root 664749587 Aug 2 07:58 mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz移动文件目录
[[email protected] opt]# mv mysql-5.7.29-linux-glibc2.12-x86_64 /usr/local/mysql
[[email protected] opt]# ll
total 649172
-rw-r--r--. 1 root root 664749587 Aug 2 07:58 mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
[[email protected] opt]# ll /usr/local/mysql/
total 288
drwxr-xr-x. 2 root root 4096 Aug 2 08:20 bin
drwxr-xr-x. 2 root root 55 Aug 2 08:20 docs
drwxr-xr-x. 3 root root 4096 Aug 2 08:19 include
drwxr-xr-x. 5 root root 230 Aug 2 08:20 lib
-rw-r--r--. 1 7161 31415 276202 Dec 18 2019 LICENSE
drwxr-xr-x. 4 root root 30 Aug 2 08:19 man
-rw-r--r--. 1 7161 31415 587 Dec 18 2019 README
drwxr-xr-x. 28 root root 4096 Aug 2 08:20 share
drwxr-xr-x. 2 root root 90 Aug 2 08:20 support-files
[[email protected] opt]# 创建目录
[[email protected] opt]# cd /usr/local/mysql
[[email protected] mysql]# mkdir data创建用户
[[email protected] mysql]# userdel mysql
userdel: user 'mysql' does not exist
[[email protected] mysql]#
[[email protected] mysql]# groupdel mysql
groupdel: group 'mysql' does not exist
[[email protected] mysql]#
[[email protected] mysql]# groupadd mysql
[[email protected] mysql]#
[[email protected] mysql]# useradd -g mysql mysql安装MySQL
[[email protected] mysql]# cd /usr/local/mysql
[[email protected] mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2022-08-02T11:48:39.597275Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-08-02T11:48:40.515360Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-08-02T11:48:40.741128Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-08-02T11:48:40.836357Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 0da07605-1259-11ed-8f9c-08002720936f.
2022-08-02T11:48:40.878466Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-08-02T11:48:43.727323Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-02T11:48:44.182192Z 1 [Note] A temporary password is generated for [email protected]: syheRm_:j5tb移动文件
[[email protected] etc]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld配置文件
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnftouch my.cnf[[email protected] etc]# vi my.cnf
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M[[email protected] etc]# chown 777 /etc/my.cnf
[[email protected] etc]# chmod +x /etc/init.d/mysqld配置环境变量
[[email protected] etc]# vi /etc/profile
#mysql environment
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
[[email protected] etc]# source /etc/profile添加软链接
[[email protected] etc]# ln -fs /usr/local/mysql/bin/mysql /usr/bin/mysql
[[email protected] etc]# systemctl stop firewalld设置防火墙
# 关闭服务
systemctl stop firewalld
# 关闭开机启动
systemctl disable firewalld
# 查看服务状态
systemctl status firewalld启动MySQL
[[email protected] etc]# cd init.d/
[[email protected] init.d]# ./mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/mysql-slave.err'.
. SUCCESS!登录MySQL
[[email protected] init.d]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 修改root密码
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)添加访问权限
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> 重启MySQL
mysql> exit;
Bye
[[email protected] init.d]# /etc/init.d/mysqld restart
Shutting down MySQL..... SUCCESS!
Starting MySQL. SUCCESS!
[[email protected] init.d]# 设置开机启动
[[email protected] init.d]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
service mysqld supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add mysqld')
[[email protected] init.d]# chkconfig --add mysqld主从复制
master配置
[[email protected] etc]# vi /etc/my.cnf
[mysqld]
#开启binlog日志
log-bin=mysql-bin
#设置服务id,主从不能一致
server-id=1
#屏蔽系统库同步
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
#设置需要同步的数据库
binlog-do-db=monomer_order[[email protected] init.d]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT REPLICATION SLAVE ON *.* TO 'db_sync'@'%' IDENTIFIED BY 'db_sync';
Query OK, 0 rows affected, 1 warning (1.74 sec)mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)
mysql> SHOW master STATUS;
+------------------+----------+---------------+---------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+---------------+---------------------------------------------+-------------------+
| mysql-bin.000001 | 592 | monomer_order | mysql,information_schema,performance_schema | |
+------------------+----------+---------------+---------------------------------------------+-------------------+
1 row in set (0.00 sec)
mysql> slave配置
[[email protected] etc]# vi /etc/my.cnf
[mysqld]
#开启binlog日志
log-bin=mysql-bin
#设置服务id,主从不能一致
server-id=2
# 设置忽略的库
replicate_wild_ignore_table=mysql.%
replicate_wild_ignore_table=information_schema.%
replicate_wild_ignore_table=performance_schema.%
# 设置同步的库
replicate_wild_do_table=monomer_order.%[[email protected] etc]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO
-> master_host='192.168.1.111',
-> master_port=3306,
-> master_user='db_sync',
-> master_password='db_sync',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=592;
Query OK, 0 rows affected, 2 warnings (0.10 sec)
mysql> start slave;
Query OK, 0 rows affected (0.33 sec)mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.111
Master_User: db_sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 592
Relay_Log_File: mysql-slave-relay-bin.000002
Relay_Log_Pos: 320
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: mysql.%,information_schema.%,performance_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 592
Relay_Log_Space: 533
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_UUID: 9edae037-11f7-11ed-854c-080027a21804
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.01 sec)
ERROR:
No query specified测试主从同步
- 创建数据库
mysql> CREATE DATABASE IF NOT EXISTS monomer_order
-> DEFAULT CHARACTER SET utf8mb4;- 创建数据表
CREATE TABLE order_info (
`id` bigint(32) NOT NULL AUTO_INCREMENT,
`order_no` varchar(32) NOT NULL COMMENT '订单号',
`order_amount` decimal(8,2) NOT NULL COMMENT '订单金额',
`merchant_id` bigint(32) NOT NULL COMMENT '商户ID',
`user_id` bigint(32) NOT NULL COMMENT '用户ID',
`order_freight` decimal(8,2) NOT NULL DEFAULT '0.00' COMMENT '运费',
`order_status` tinyint(3) NOT NULL DEFAULT '0' COMMENT '订单状态,10待付款,20待接单,30已接单,40配送中,50已完成,55部分退款,60全部退款,70取消订单',
`trans_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '交易时间',
`pay_status` tinyint(3) NOT NULL DEFAULT '2' COMMENT '支付状态,1待支付,2支付成功,3支付失败',
`recharge_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '支付完成时间',
`pay_amount` decimal(8,2) NOT NULL DEFAULT '0.00' COMMENT '实际支付金额',
`pay_discount_amount` decimal(8,2) NOT NULL DEFAULT '0.00' COMMENT '支付优惠金额',
`address_id` bigint(32) NOT NULL COMMENT '收货地址ID',
`delivery_type` tinyint(3) NOT NULL DEFAULT '2' COMMENT '配送方式,1自提。2配送',
`delivery_status` tinyint(3) DEFAULT '0' COMMENT '配送状态,0 配送中,2已送达,3待收货,4已送达',
`delivery_expect_time` timestamp NULL DEFAULT NULL COMMENT '配送预计送达时间',
`delivery_complete_time` timestamp NULL DEFAULT NULL COMMENT '配送送达时间',
`delivery_amount` decimal(8,2) NOT NULL DEFAULT '0.00' COMMENT '配送运费',
`coupon_id` bigint(32) DEFAULT NULL COMMENT '优惠券id',
`cancel_time` timestamp NULL DEFAULT NULL COMMENT '订单取消时间',
`confirm_time` timestamp NULL DEFAULT NULL COMMENT '订单确认时间',
`remark` varchar(512) DEFAULT NULL COMMENT '订单备注留言',
`create_user` bigint(32) DEFAULT NULL COMMENT '创建用户',
`update_user` bigint(32) DEFAULT NULL COMMENT '更新用户',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '逻辑删除标记',
PRIMARY KEY (`id`,`order_no`),
KEY `inx_user_id` (`user_id`),
UNIQUE KEY `uinx_order_no` (`order_no`),
KEY `inx_merchant_id_update_time` (`merchant_id`,`update_time`),
KEY `inx_update_time` (`update_time`,`order_no`) USING BTREE,
KEY `inx_create_time` (`create_time`,`order_no`)
) ENGINE=InnoDB AUTO_INCREMENT=84656407 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='订单表';
CREATE TABLE order_item_detail (
`id` bigint(32) NOT NULL AUTO_INCREMENT,
`order_no` varchar(32) NOT NULL COMMENT '订单号',
`product_id` bigint(32) NOT NULL COMMENT '商品ID',
`category_id` bigint(32) NOT NULL COMMENT '商品分类ID',
`goods_num` int(8) NOT NULL DEFAULT '1' COMMENT '商品购买数量',
`goods_price` decimal(8,2) NOT NULL DEFAULT '0.00' COMMENT '商品单价',
`goods_amount` decimal(8,2) NOT NULL DEFAULT '0.00' COMMENT '商品总价',
`product_name` varchar(64) DEFAULT NULL COMMENT '商品名',
`discount_amount` decimal(8,2) NOT NULL DEFAULT '0.00' COMMENT '商品优惠金额',
`discount_id` bigint(32) DEFAULT NULL COMMENT '参与活动ID',
`product_picture_url` varchar(128) DEFAULT NULL COMMENT '商品图片',
`create_user` bigint(32) DEFAULT NULL COMMENT '创建用户',
`update_user` bigint(32) DEFAULT NULL COMMENT '更新用户',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`delete_flag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '逻辑删除标记',
PRIMARY KEY (`id`) USING BTREE,
KEY `inx_item_order_no` (`order_no`),
KEY `inx_create_time` (`create_time`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=218311238 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='订单明细表';边栏推荐
- AI+BI+可视化,Sugar BI架构深度剖析
- 元宇宙系列--Value creation in the metaverse
- Interpretation of the 2021 Cost of Data Breach Report
- Leetcode76. 最小覆盖子串
- Windows 事件查看器记录到 MYSQL
- 【QT】Qt 给已经开发好的程序快速封装成动态库
- Reptile attention
- TCP 可靠吗?为什么?
- 机器人开发--Universal Scene Description(USD)
- How to get the 2 d space prior to ViT?UMA & Hong Kong institute of technology & ali SP - ViT, study for visual Transformer 2 d space prior knowledge!.
猜你喜欢
随机推荐
How to get the 2 d space prior to ViT?UMA & Hong Kong institute of technology & ali SP - ViT, study for visual Transformer 2 d space prior knowledge!.
高可用版 主数据库数据结构改变 备数据库会自动改变吗
Small Tools (4) integrated Seata1.5.2 distributed transactions
Yuan xiaolin: Volvo focus on travel security, and put it perfectly
leetcode-268.丢失的数字
不可忽略!户外LED显示屏的特点及优势
甲方不让用开源【监控软件】?大不了我自己写一个
CopyOnWriteArrayList details
【899. Ordered Queue】
想进阿里?先来搞懂一下分布式事务
如何启动 NFT 集合
请问下阿里云全托管flink能执行两条flink sql命令么?
泰山OFFICE技术讲座:段落边框的绘制难点在哪里?
常见分布式理论(CAP、BASE)和一致性协议(Gosssip、Raft)
1、实例开启无锁表结构变更以后,在任务编排中通过“单实例SQL”节点进行的结构变更,是优先采用无锁表
红蓝对抗经验分享:CS免杀姿势
TCP 可靠吗?为什么?
Introduction to spark learning - 1
使用Make/CMake编译ARM裸机程序(基于HT32F52352 Cortex-M0+)
【QT】Qt项目demo:数据在ui界面上显示,鼠标双击可弹窗显示具体信息








![[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01](/img/8e/fcf79d150af4384c14a118fb209725.png)
