感兴趣的话大家可以关注一下公众号 : 猿人刘先生 , 欢迎大家一起学习 , 一起进步 , 一起来交流吧!
将主服务器的binlog日志复制到从服务器上执行一遍,达到主从数据的一致状态,称之为主从复制。通俗一点说就是主服务器做什么 , 从服务器也做什么
其实数据库的主从同步,就是为了要保证多个数据库之间的数据保持一致。最简单的方式就是使用数据库的导入导出工具,定时将主库的数据导出,再导入到从库当中。这是一种很常见,也很简单易行的数据库集群方式。也有很多的工具帮助我们来做这些事情。但是这种方式进行数据同步的实时性比较差。
而如果要保证数据能够实时同步,对于MySQL,通常就要用到他自身提供的一套通过Binlog日志在多个MySQL服务之间进行同步的集群方案。基于这种集群方案,一方面可以提高数据的安全性,另外也可以以此为基础,提供读写分离、故障转移
等其他高级的功能。
即在主库上打开Binlog日志,记录对数据的每一步操作。然后在从库上打开RelayLog日志,用来记录跟主库一样的Binlog日志,并将RelayLog中的操作日志在自己数据库中进行重演。这样就能够更加实时的保证主库与从库的数据一致
需要注意的是 : MySQL的Binlog默认是不打开的。
他的实现过程是在从库上启动一系列IO线程,负责与主库建立TCP连接,请求主库在写入Binlog日志时,也往从库传输一份。这时,主库上会有一个IO Dump线程,负责将Binlog日志通过这些TCP连接传输给从库的IO线程。而从库为了保证日志接收的稳定性,并不会立即重演Binlog数据操作,而是先将接收到的Binlog日志写入到自己的RelayLog日志当中。然后再异步的重演RelayLog中的数据操作
1.从库的IO线程,读取master.info信息,获取主库信息并连接主库
2.主库接收从库的链接请求后,会生成一个准备binlog dump的线程,来响应从库
3.主库一旦有新的日志生成,会发送“信号”给主库的binlog dump线程,然后binlog dump线程会读取binlog日志的更新
4.TP(传送)给从库的IO线程
5.IO线程将收到的日志存储到了TCP/IP 缓存
6.写入TCP/IP缓存后,立即返回ACK给主库 ,此时主库工作完成
7.IO线程更新master.info文件binlog 文件名和postion
8.IO线程将缓存中的数据,存储到relay-log日志文件,此时io线程工作完成
9.从库SQL线程读取relay-log.info文件,获取到上次执行到的relay-log的位置,作为起点
10.从库SQL线程基于从步骤9中获取到的起点,去中继日志relay-log.000001获取后续操作,在从库回放relay-log
11.SQL线程回放完成之后,会更新relay-log.info文件,把当前操作的位置记入,作为下一次操作的起点。
12. relay-log会有自动清理的功能
rpm -qa|grep mysql
rpm -e mysql mysql-libs
yum -y remove mysql-server mysql mysql-libs
获取源地址 http://dev.mysql.com/downloads/repo/yum/ 添加源命令
rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
[root@localhost ~]# yum repolist all | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 启用: 24
mysql-connectors-community-source MySQL Connectors Community - Sourc 禁用
mysql-tools-community/x86_64 MySQL Tools Community 启用: 38
mysql-tools-community-source MySQL Tools Community - Source 禁用
mysql-tools-preview/x86_64 MySQL Tools Preview 禁用
mysql-tools-preview-source MySQL Tools Preview - Source 禁用
mysql55-community/x86_64 MySQL 5.5 Community Server 禁用
mysql55-community-source MySQL 5.5 Community Server - Sourc 禁用
mysql56-community/x86_64 MySQL 5.6 Community Server 禁用
mysql56-community-source MySQL 5.6 Community Server - Sourc 禁用
mysql57-community/x86_64 MySQL 5.7 Community Server 启用: 146
mysql57-community-source MySQL 5.7 Community Server - Sourc 禁用
mysql80-community/x86_64 MySQL 8.0 Community Server 禁用
mysql80-community-source MySQL 8.0 Community Server - Sourc 禁用
[root@localhost ~]# yum-config-manager --disable mysql57-community
[root@localhost ~]# yum-config-manager --enable mysql56-community
如果 yum-config-manager: 未找到命令,就需要安装 yum-utils
[root@localhost ~]# yum install -y yum-utils
[root@localhost ~]# yum install mysql-community-server
[root@localhost ~]# systemctl start mysqld.service
[root@localhost ~]# service mysqld start/status/stop
[root@localhost ~]# systemctl stop mysqld.service
[root@localhost ~]# systemctl status mysqld.service
[root@localhost ~]# systemctl restart mysqld.service
获取默认密码
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
设置新密码
mysql> set password = password('liuxs');
生产环境一般禁止 MySQL 更新,修改 /etc/yum.conf,添加内容
exclude=mysql-community-client,mysql-community-common,mysql-community-libs,mysql-community-server
如果需要更新
[root@localhost ~]# yum update mysql-server
注意: 在使用 yum 更新之后,MySQL服务器会自动重启。
默认配置文件路径 /etc/my.cnf
[client]
# 端口号
port=3306
# 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
character-set-server=utf8
collation-server=utf8_general_ci
lower_case_table_names=1
max_allowed_packet=100M
net_buffer_length=30M
max_connections=80
CREATE USER 'GreatOne'@'%' IDENTIFIED BY 'GreatOne1!';
GRANT ALL PRIVILEGES ON *.* TO '用户名'@'指定ip' IDENTIFIED BY '用户密码' ;
FLUSH PRIVILEGES;
show grants for 'user'@'%';
# 权限回收(不包含赋权权限)
REVOKE ALL PRIVILEGES ON *.* FROM cacti;
REVOKE ALL PRIVILEGES ON cacti.* FROM cacti;
# 收回赋权权限
REVOKE GRANT OPTION ON *.* FROM cacti;
FLUSH PRIVILEGES;
MySql 官方提供了一种思路,通过在启动 mysqld 服务时指向 init-file 来实现重置密码 参考
https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
实际操作没有成功
修改 MySQL 配置,不用密码登录 MySQL 在 [mysqld] 下面添加 skip-grant-tables
[mysqld]
skip-grant-tables
#
# 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
#
重启 mysqld.service 后可以用 mysql 命令免密码登录,此时通过 UPDATE 语句便可修改 root 密码
[root@localhost ~]# systemctl restart mysqld.service
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
MySQL 5.7 之后 user 表里用 authentication_string 字段代替 password 字段
mysql> use mysql;
mysql> DESC user;
mysql> UPDATE user SET authentication_string=password('ABCabc123!');
修改好之后把配置改回去,再重启 mysqld.service
在另外一台机器也参考以上步骤安装Mysql , 让然后选择一台在当作主服务
#复制集群中的各节点的id均必须唯一
server-id = 1
开启二进制日志
log-bin = master-bin
开启中继日志
relay-log = relay-log
关闭域名解析
skip_name_resolve
service mysqld restart
#复制集群中的各节点的id均必须唯一;第二台slave节点改为3
server-id = 2
#开启中继日志
relay-log = relay-log
#开启二进制日志
log-bin = master-bin
#启用只读属性
read_only = ON
#是否自动清空不再需要中继日志
relay_log_purge = 0
#关闭名称解析(非必须)
skip_name_resolve
#使得更新的数据写进二进制日志中
log_slave_updates = 1
grant replication slave,replication client on *.* to 'slave'@'192.168.%.%' identified by 'keer';
查看主库状态
show master status;
注意 : master_log_pos=120;的属性一定要和master节点的对应
change master to master_host='192.168.154.138',
master_user='slave',
master_password='keer',
master_log_file='master-bin.000001',
master_log_pos=120;
启动slave
start slave;
查看slave状态
show slave status;
另外一个从节点也是这样配置 , 查询slave节点状态之后看到如下两个属性都为yes就表示成功
Slave_IO_Running和Slave_sql_Running状态必须都为yes才可 , 只要有一个为no就不行 , 那么就需要检查配置
如果出现主从不同的原因 , 可以查看我的这篇博客 : https://blog.csdn.net/qq_45001002/article/details/128343697