centos7 mysql 基本测试(6)主从简单测试

centos7 xtrabackup mysql 基本测试(6)主从简单测试

mysql -u etc -p
1234aA~1

参考:
centos7 时区设置 时间同步
https://blog.csdn.net/wowocpp/article/details/135931129

Mysql数据库:主从复制与读写分离
https://blog.csdn.net/m0_71815887/article/details/137112591

CentOS7-MySQL-主从
https://blog.csdn.net/super_lixiang/article/details/82595001

MySQL-主从架构的搭建
https://developer.aliyun.com/article/1409013

centos7搭建mysql主从
https://www.jianshu.com/p/cbfbee2a662b

如何在centos7中搭建传统的mysql主从
https://jingyan.baidu.com/article/380abd0aacb63d5c90192cad.html

MySQL主从复制《主库已经有数据的解决方案》《几种复制模式》
https://blog.csdn.net/weixin_41309035/article/details/136157363

需要两台 虚拟机
G:\centos7_mini_1810_server_zhu 192.168.99.40
G:\centos7_mini_1810_server_cong 192.168.99.172

mysql 版本

Server version: 5.7.44 MySQL Community Server (GPL)

时间 同步

timedatectl
centos7 时区设置 时间同步

timedatectl

sudo yum install chrony -y
sudo systemctl enable chronyd
sudo systemctl start chronyd
sudo systemctl status chronyd
对于较旧版本的CentOS系统,没有安装chrony,可以使用ntpdate来完成时间同步。

date // 查看当前时间
timedatectl set-timezone Asia/Shanghai // 设置时区为上海

主服务器设置

ip addr 查看 网卡,有时候 同一个网卡上面 会有两个ip地址。
原因可能是

/etc/sysconfig/network-scripts/ifcfg-ens33

设置 固定的ip地址 192.168.99.40

对 ifcfg-ens33 文件做备份的时候
不要 命名为ifcfg-ens33.bak ,而是要 命名为bak_ifcfg-ens33
ifcfg-ens33.bak 也会被解析,执行。给网卡增加了 第二地址。

查看mysql 数据库 的data地址
mysql -u etc -p
1234aA~1

show variables like '%dir' ;

datadir | /opt/datadir/mysql/

/opt/datadir/mysql

sudo cat 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
datadir=/opt/datadir/mysql/
socket=/opt/datadir/mysql/mysql.sock
#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
[client]
socket=/opt/datadir/mysql/mysql.sock

创建测试数据库 company_pro

mysql -u root -p
1234aA~1

show databases ;
create database if not exists company_pro;
use company_pro;
DROP TABLE IF EXISTS employee;

drop table if exists employee;
create table employee (
  id int unsigned primary key auto_increment comment 'id'
  , name varchar(50) not null comment '名称'
  , create_time datetime not null default current_timestamp comment '创建时间'
  , update_time datetime not null default current_timestamp on update current_timestamp comment '更新时间'
)  ENGINE=InnoDB charset=utf8 comment '测试表';

DESCRIBE employee ;
insert into employee(name) value ('张三');
insert into employee(name) value ('李四');

创建测试数据库 company_service

mysql -u root -p
1234aA~1

show databases ;
create database if not exists company_service;
use company_service;
DROP TABLE IF EXISTS customer;

drop table if exists customer;
create table customer(
  id int unsigned primary key auto_increment comment 'id'
  , name varchar(50) not null comment '名称'
  , create_time datetime not null default current_timestamp comment '创建时间'
  , update_time datetime not null default current_timestamp on update current_timestamp comment '更新时间'
)  ENGINE=InnoDB charset=utf8 comment '测试表';

DESCRIBE customer;

insert into customer(name) value ('中石化');
insert into customer(name) value ('中石油');

修改主服务器配置文件

sudo vi my.cnf

#for zhu and chong
server-id=40
log-bin=mysql-bin
binlog_format=mixed
binlog_do_db=company_pro
binlog_do_db=company_service

sudo systemctl restart mysqld

在这里插入图片描述

show databases;

show master status ;
show variables like '%log_bin%';
SHOW VARIABLES LIKE 'port';

在这里插入图片描述

sudo systemctl restart mysqld

授权一个用户,用于远程连接主机(relication)

Master的数据库中建立一个复制帐户并授权
在 Master 的数据库中建立一个备份帐户(user = mysqlsyn,pwd=1234aA~1):每个 slave 使用标准的 MySQL 用户名和密码连接 master 。
进行复制操作的用户会授予 REPLICATION SLAVE 权限。
mysql -u root -p
1234aA~1

create user 'mysqlsyn'@'%' identified by '1234aA~1';
grant replication slave,replication client on *.* to 'mysqlsyn'@'%' identified by '1234aA~1';
flush privileges;

可使用SHOW SLAVE STATUS\G;查看从服务器状态,
也可用show processlist \G;查看当前复制状态

放开防火墙权限

sudo systemctl status firewalld
#防火墙开放3306端口号
sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
sudo systemctl restart firewalld
#查看防火墙开放端口列表
sudo firewall-cmd --zone=public --list-ports

主服务器备份文件

mysqldump -uroot -p1234aA~1 --default-character-set=utf8 --flush-logs -R company_pro > company_pro.sql
mysqldump -uroot -p1234aA~1 --default-character-set=utf8 --flush-logs -R company_service > company_service.sql

得到文件
company_pro.sql company_service.sql
从机上面远程接收文件
scp jack@192.168.99.40:/home/jack/company_pro.sql .
scp jack@192.168.99.40:/home/jack/company_service.sql .

mysqldump -uroot -p --master-data=1 --single-transaction --routines --triggers --events --all-databases > all.sql

slave 机器查看

测试slave连接master数据库:
192.168.99.40 是master 的 ip地址

mysql -h 192.168.99.40 -u mysqlsyn -p
1234aA~1

show master status ;
确认可以连接

初始化数据

登录slave的 mysql
mysql -u root -p
1234aA~1
create database if not exists company_service;
use company_service;
source /home/jack/company_service.sql;

show databases ;
create database if not exists company_pro;
use company_pro;
source /home/jack/company_pro.sql;

修改slave的配置文件

# 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
datadir=/opt/datadir/mysql/
socket=/opt/datadir/mysql/mysql.sock
#socket=/var/lib/mysql/mysql.sock
# for zhu and chong
server-id=172
log-bin=mysql-bin

# 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
[client]
socket=/opt/datadir/mysql/mysql.sock

sudo systemctl restart mysqld
重启

登录slave的 mysql
mysql -u root -p
1234aA~1

初始化

mysql> change master to
    -> master_host='192.168.99.40',
    -> master_user='mysqlsyn',
    -> master_password='1234aA~1',
    -> master_log_file='mysql-bin.000003',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G

Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

/opt/datadir/mysql/auto.cnf
修改一下uuid

https://blog.csdn.net/co1590/article/details/120190659

暂停 从机,然后重启

mysql> stop slave;

重启

mysql> reset slave;
mysql> start slave;

测试主从是否可以

主机端
mysql -u root -p
1234aA~1

show databases ;
use company_pro;
show tables ;
insert into employee(name) value (‘从机测试’);
insert into employee(name) value (‘从机测试726’);
从机端 查看
mysql -u root -p
1234aA~1
show databases ;
use company_pro;
show tables ;
select * from employee ;

可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值