Mysql 开启主从同步

一、安装mysql

##################下载mysql5.7
wget  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
##################rpm安装生成yum仓库
rpm -ivh mysql57-community-release-el7-9.noarch.rpm 
##################yum安装mysql
yum -y  install   mysql  mysql-server  --nogpgcheck

二、配置主从

2.1、修改初始化密码且打开远程登录

此操作需要在主从两库都进行操作
[root@database-2 tmp]# cat   /var/log/mysqld.log  | grep  pass
[root@database-2 tmp]# mysql  -uroot  -p'ERgdrBVl<57;'  ########## 'ERgdrBVl<57;'系统随机生成的密码
#############修改密码
alter user 'root'@'localhost' identified by 'youpassword';
#############开启远程登录
grant all privileges on *.* to root@"需要登录的IP" identified by "youpassword" WITH GRANT OPTION;

2.2、修改配置文件

vim  /etc/my.cnf
############主库配置文件master
server_id=1
log-bin=mysql-bin
############从库配置文件slave
log_bin=mysql-bin
server_id=2
relay_log=mysql-relay-bin
log_slave_updates=1
read_only=1

2.3、创建用户并且配置主从

主库操作

########################在master上操作
mysql> grant  replication  client,replication   slave  on  *.* to  rep@'172.16.18.%' identified  by 'password';						#从库IP,我这里写的是主库和从库的一个网段
mysql> flush  tables with read lock;			#锁表,因为我这是生产中配置的主从,所以要保持主从数据一致,先进行锁表然后再导出数据,然后将数据导入从库
####################查看master信息
mysql> show  master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |     5420 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@database-1 ~]# mysqldump -uroot -p'密码' -S /data/mysql/mysql.sock --all-databases >all.sql
-S 是指定mysql的sock是因为我的mysql没有在默认的路径下

从库操作

mysql> source   /tmp/all.sql			#导入数据,这时候要给主库解表
#################!!!!!!!!!!####################主库操作
mysql> unlock  tables;			        #######解表,主库操作
mysql> CHANGE MASTER TO 
MASTER_HOST='172.16.18.6', 				#########主库IP
MASTER_USER='rep', 						#########同步用户
MASTER_PASSWORD='Cw123!@#', 			#########同步用户的密码
MASTER_LOG_FILE='mysql-bin.000003',     #########主库的二进制文件
MASTER_LOG_POS=5420;					#########主库的pos
mysql> start slave;						#########启动从库
#############查看主从同步状态
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.18.6
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 27098
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 21998
        Relay_Master_Log_File: mysql-bin.000003
             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: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 27098
              Relay_Log_Space: 22205
              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: 32e9eb51-714c-11ed-b5b4-fa163e620961
             Master_Info_File: /var/lib/mysql/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.00 sec)
双yes代表配置成功,这时需要验证,验证很简单,修改主库的一个表,然后查看从库是否变化。
也可以选择观察主库pos是否变化。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL主从同步是一种常用的数据库复制技术,用于将一个MySQL数据库的数据和操作同步到其他MySQL数据库上。主从同步可以提高数据库的可用性、可扩展性和数据备份能力。下面是MySQL主从同步的基本原理和步骤: 1. 主从角色:在主从同步中,有一个主数据库(Master)和一个或多个从数据库Slave)。主数据库负责接收和处理所有的写操作,而从数据库则复制主数据库的数据和操作。 2. 二进制日志(Binary Log):主数据库会将所有的写操作记录在二进制日志中,包括插入、更新和删除等操作。二进制日志是主从同步的基础。 3. 主数据库配置:在主数据库上,需要开启二进制日志功能,并配置一个唯一的服务器ID。这个ID用于标识主数据库。 4. 从数据库配置:在从数据库上,需要配置主数据库的IP地址、端口号和二进制日志文件名等信息。从数据库会连接到主数据库,并请求复制二进制日志。 5. 复制过程:当从数据库连接到主数据库后,主数据库会将二进制日志中的数据和操作发送给从数据库。从数据库会解析并执行这些操作,以保持与主数据库的数据一致性。 6. 异步复制:默认情况下,MySQL主从同步是异步进行的,即主数据库不会等待从数据库执行完操作才返回。这样可以提高主数据库的性能,但也可能导致主从数据的稍许延迟。 7. 监控和故障处理:在主从同步中,需要定期监控主从数据库的状态,确保同步正常。如果出现故障或延迟,需要及时排查和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值