• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MySQL主从搭建(多主一从)的实现思路与步骤

mysql 搞代码 4年前 (2022-01-09) 24次浏览 已收录 0个评论

背景:

由于最近公司项目好像有点受不住并发压力了,优化迫在眉睫。由于当前系统是单数据库系统原因,能优化的地方也尽力优化了但是数据库瓶颈还是严重限制了项目的并发能力。所以就考虑了添加数据库来增大项目并发能力。

思路:

1: 创建集中库: 主要就是存储历史数据。作为查询使用。

2:创建多个业务库:满足项目高并发的能力。

demo环境:

1: VM ware 虚拟机 – centOS 7

centOS-1: 192.168.194.3 主 100——-业务库

centOS-2: 192.168.194.4 主 200——-业务库

centOS-3: 192.168.194.5 从 300——-相当于集中库

2:mysql 5.7

步骤

1: 主库100

设置my.cnf。

[mysqld]
lower_case_table_names = 1 # 表名不区分大小写
server-id = 100
log_bin = mysql-bin  #开始binlog记录
binlog_format = MIXED 
#每次事务提交,MySQL都会把binlog刷下去,是最安全但是性能损耗最大的设置。
#这样的话,在数据库所在的主机操作系统损坏或者突然掉电的情况下,系统才有可能丢失1个事务的数据
#但是binlog虽然是顺序IO,但是设置sync_binlog=1,多个事务同时提交,
#同样很大的影响MySQL和IO性能。按需设置。
sync_binlog = 1 
# 二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
expire_logs_days = 7
#binlog_cache_size = 128m
#max_binlog_cache_size = 512m
#max_binlog_size = 256M
# 需要同步库
binlog-do-db = dev
# 不需要同步库
binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys
datadir=/var/lib/mysql

设置slave用户

# mysql -uroot -p 
# password: xxxxxx
# mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'root1234';
# mysql> flush privileges;
# quit;
# systemctl restart mysqld;
# show master status \G
#*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 886
     Binlog_Do_DB: dev
 Binlog_Ignore_DB: mysql,information_schema,performation_schema,sys
Executed_Gtid_Set: 
1 row in set (0.00 sec)

2: 主库200

设置my.cnf。

[mysqld]
lower_case_table_names = 1 # 表名不区分大小写
server-id = 200
log_bin = mysql-bin  #开始binlog记录
binlog_format = MIXED 
#每次事务提交,MySQL都会把binlog刷下去,是最安全但是性能损耗最大的设置。
#这样的话,在数据库所在的主机操作系统损坏或者突然掉电的情况下,系统才有可能丢失1个事务的数据
#但是binlog虽然是顺序IO,但是设置sync_binlog=1,多个事务同时提交,
#同样很大的影响MySQL和IO性能。按需设置。
sync_binlog = 1 
# 二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
expire_logs_days = 7
#binlog_cache_size = 128m
#max_binlog_cache_size = 512m
#max_binlog_size = 256M
# 需要同步库
binlog-do-db =dev
# 不需要同步库
binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys
datadir=/var/lib/mysql

设置slave用户

# mysql -uroot -p 
# password: xxxxxx
# mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'root1234';
# mysql> flush privileges;
# quit;
# systemctl restart mysqld;
# show master status \G
#*************************** 1. row ***************************
             File: mysql-bin.000001
         Pos<strong style="color:transparent">来源gaodai#ma#com搞@代~码$网</strong>ition: 154
     Binlog_Do_DB: dev
 Binlog_Ignore_DB: mysql,information_schema,performation_schema,sys
Executed_Gtid_Set: 
1 row in set (0.00 sec)

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MySQL主从搭建(多主一从)的实现思路与步骤

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址