MySQL双主搭建

基础环境

CentOS Linux release 7.7 两台
IP:192.168.1.123(01)
IP:192.168.1.187(02)

配置/etc/hosts

[root@v123 etc]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.123    masterA    v123
192.168.1.187     masterB   v187

安装mysql
删除自带的数据库

rpm -qa | grep -i mariadb
rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64

上传 mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar

wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
yum install perl -y
yum install net-tools -y
rpm -ivh mysql-community-common-5.7.27-1.el7.x86_64.rpm 
rpm -ivh mysql-community-libs-5.7.27-1.el7.x86_64.rpm 
rpm -ivh mysql-community-client-5.7.27-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.27-1.el7.x86_64.rpm 

修改密码

vi /etc/my.cnf
skip-grant-tables添加
数据库刷新下权限
flush privileges;
mysql> grant all on *.* to '用户名'@'登录主机' identified by '密码';
mysql> alter user '用户名'@'登录主机' identified by '密码(自定义)';
mysql> SET PASSWORD FOR '用户名'@'登录主机' = PASSWORD('密码');
flush privileges;(修改密码再次刷新权限)

配置(主01)my.cnf

[root@26 mysql]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


log-bin=mysql-bin
server-id=1
binlog-ignore-db = mysql,information_schema
binlog_cache_size = 1M
auto_increment_increment = 2
auto_increment_offset = 1
relay_log = mysql-relay-bin

主02

[root@v43 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


log-bin=mysql-bin
server-id=2
binlog-ignore-db = mysql,information_schema
binlog_cache_size = 1M
auto_increment_increment = 2
auto_increment_offset = 2
relay_log = mysql-relay-bin

创建复制账号
主msql1

 mysql>GRANT  REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED  BY 'Abcd@666888';
 mysql> flush  privileges;
 mysql> change  master to
    ->  master_host='192.168.1.43',
    ->  master_user='repl1',
    ->  master_password='Abcd@666888',
    ->  master_log_file='mysql-bin.000001',
    ->  master_log_pos=106;  
mysql> start  slave;
SHOW MASTER STATUS;

在这里插入图片描述

mysql>GRANT  REPLICATION SLAVE ON *.* TO 'repl1'@'%' IDENTIFIED  BY 'Abcd@666888';
 mysql> flush  privileges;
 mysql> change  master to
    ->  master_host='192.168.1.26',
    ->  master_user='repl',
    ->  master_password='Abcd@666888',
    ->  master_log_file='mysql-bin.000001',
    ->  master_log_pos=106;  
mysql> start  slave;

完成如图
图一
在这里插入图片描述
图二
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值