首页 > 数据库 >MySQL主从复制修改复制用户及密码【转】

MySQL主从复制修改复制用户及密码【转】

时间:2023-08-10 17:25:04浏览次数:113  
标签:主从复制 slave Master 复制 master MySQL test MASTER localhost

  1. 在生产环境中有时候需要修改复制用户账户的密码,比如密码遗失,或者由于多个不同的复制用户想统一为单独一个复制账户。对于这些操作应尽可能慎重以避免操作不同导致主从不一致而需要进行修复。本文描述了修改复制账户密码以及变更复制账户。

1、更改复制账户密码

--演示环境,同一主机上的2个实例,主3406,从3506

--当前版本,注:master账户表明是对主库进行相关操作,slave则是对从库进行相关操作 master@localhost[(none)]> show variables like 'version'; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.6.12-log | +---------------+------------+
--主库上的记录 master@localhost[test]> select * from tb1; +------+-------+ | id | name | +------+-------+ | 1 | robin | +------+-------+
--从库上的记录 slave@localhost[test]> select * from tb1; +------+-------+ | id | name | +------+-------+ | 1 | robin | +------+-------+
--当前从库上的状态信息 slave@localhost[test]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.177 Master_User: repl Master_Port: 3406 Connect_Retry: 60 Master_Log_File: inst3406bin.000001 Read_Master_Log_Pos: 3296006 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 811 Relay_Master_Log_File: inst3406bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test,sakila --仅复制了test以及sakila数据库 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: 3296006 Relay_Log_Space: 978
--主库上复制账户的信息 master@localhost[test]> show grants for 'repl'@'192.168.1.177'; +----------------------------------------------------------------------------------------------------------------+ | Grants for [email protected] | +----------------------------------------------------------------------------------------------------------------+ | GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.177' IDENTIFIED BY PASSWORD '*A424E797037BF191C5C2038C039' | +----------------------------------------------------------------------------------------------------------------+
--修改复制账户密码 master@localhost[test]> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.177' IDENTIFIED BY 'replpwd';

--如下查询密码已更改 master@localhost[test]> select user,host,password from mysql.user where user='repl'; +------+---------------+-------------------------------------------+ | user | host | password | +------+---------------+-------------------------------------------+ | repl | 192.168.1.177 | *4A04E4FD524292A79E3DCFEBBD46094478F178EF | +------+---------------+-------------------------------------------+
--更新记录 master@localhost[test]> insert into tb1 values(2,'fred');

--重库上可以查询到刚刚被更新的记录 slave@localhost[test]> select * from tb1; +------+-------+ | id | name | +------+-------+ | 1 | robin | | 2 | fred | +------+-------+ slave@localhost[test]> stop slave; Query OK, 0 rows affected (0.02 sec) slave@localhost[test]> start slave; Query OK, 0 rows affected (0.01 sec)

--再次查看状态出现了错误提示 slave@localhost[test]> show slave status \G *************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 192.168.1.177 Master_User: repl Master_Port: 3406 Connect_Retry: 60 Master_Log_File: inst3406bin.000001 Read_Master_Log_Pos: 3296438 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 1243 Relay_Master_Log_File: inst3406bin.000001 Slave_IO_Running: Connecting Slave_SQL_Running: Yes Replicate_Do_DB: test,sakila .................... Last_IO_Errno: 1045 Last_IO_Error: error connecting to master '[email protected]:3406' - retry-time: 60 retries: 1
--更改重库连接密码,该信息记录在从库master.info文件中 slave@localhost[test]> stop slave; slave@localhost[test]> change master to -> master_user='repl', -> master_password='replpwd'; Query OK, 0 rows affected, 2 warnings (0.00 sec)
--修改密码后,从库状态正常,以下检查结果不再列出 slave@localhost[test]> start slave; --查看master.info,密码已更改且为名文 slave@localhost[(none)]> system grep repl /data/inst3506/data3506/master.info repl replpwd

 

2、更换复制账户及密码

master@localhost[test]> GRANT REPLICATION SLAVE ON *.* TO 'repl2'@'192.168.1.177' IDENTIFIED BY 'Repl2';
Query OK, 0 rows affected (0.00 sec)
slave@localhost[test]> stop slave;
Query OK, 0 rows affected (0.28 sec)
master@localhost[test]> insert into tb1 values(3,'jack');
Query OK, 1 row affected (0.00 sec)
slave@localhost[test]> change master to
-> MASTER_USER='repl2',
-> MASTER_PASSWORD='Repl2';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
slave@localhost[test]> system more /data/inst3506/data3506/master.info
23
inst3406bin.000001
3294834
192.168.1.177
repl2
Repl2
3406
..........
slave@localhost[test]> start slave;
Query OK, 0 rows affected (0.01 sec)
slave@localhost[test]> select * from tb1 where id=3;
+------+------+
| id | name |
+------+------+
| 3 | jack |
+------+------+
1 row in set (0.00 sec)
slave@localhost[(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.177
Master_User: repl2
Master_Port: 3406
Connect_Retry: 60
Master_Log_File: inst3406bin.000001 --Author :Leshami
Read_Master_Log_Pos: 3296871 --Blog : http://blog.csdn.net/leshami
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 501
Relay_Master_Log_File: inst3406bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test,sakila

 

3、关于change master
CHANGE MASTER TO changes the parameters that the slave server uses for connecting to the master
server, for reading the master binary log, and reading the slave relay log. It also updates the contents
of the master info and relay log info repositories (see Section 16.2.2, “Replication Relay and Status
Logs”). To use CHANGE MASTER TO, the slave replication threads must be stopped (use STOP SLAVE
if necessary). In MySQL 5.6.11 and later, gtid_next [2060] must also be set to AUTOMATIC (Bug
#16062608).

Options not specified retain their value, except as indicated in the following discussion. Thus, in most
cases, there is no need to specify options that do not change. For example, if the password to connect
to your MySQL master has changed, you just need to issue these statements to tell the slave about the
new password:

STOP SLAVE; — if replication was running
CHANGE MASTER TO MASTER_PASSWORD=’new3cret’;
START SLAVE; — if you want to restart replication

MASTER_HOST, MASTER_USER, MASTER_PASSWORD, and MASTER_PORT provide information to the
slave about how to connect to its master:

Note: Replication cannot use Unix socket files. You must be able to connect to the
master MySQL server using TCP/IP.

If you specify the MASTER_HOST or MASTER_PORT option, the slave assumes that the master
server is different from before (even if the option value is the same as its current value.) In this
case, the old values for the master binary log file name and position are considered no longer
applicable, so if you do not specify MASTER_LOG_FILE and MASTER_LOG_POS in the statement,
MASTER_LOG_FILE=’’ and MASTER_LOG_POS=4 are silently appended to it.

Setting MASTER_HOST=’’ (that is, setting its value explicitly to an empty string) is not the same as
not setting MASTER_HOST at all. Beginning with MySQL 5.5, trying to set MASTER_HOST to an empty
string fails with an error. Previously, setting MASTER_HOST to an empty string caused START SLAVE
subsequently to fail. (Bug #28796)

转自

MySQL修改复制用户及密码-蒲公英云
https://www.dandelioncloud.cn/article/details/1556290845425225730

标签:主从复制,slave,Master,复制,master,MySQL,test,MASTER,localhost
From: https://www.cnblogs.com/paul8339/p/17620942.html

相关文章

  • 解决mysqladmin flush-hosts
    1、提高允许的max_connect_errors数量(治标不治本)a.命令行修改 修改max_connection_errors的数量为1000 mysql-h123.57.78.101-P3306-uroot-p123456 setglobalmax_connect_errors=1000; showvariableslike‘%max_connect_errors%’;b.配置文件修改 登陆进入M......
  • mysql双主机热备
    1,主库配置cp-f~/files/my.cnf.master/opt/my.cnfservicemysqlrestartmysql-uroot-pcreateuser'repl'@'%'identifiedwithmysql_native_passwordby'repl';GRANTREPLICATIONSLAVEON*.*TO'repl'@'%';FLUSHPR......
  • mysql与pg的主键索引说明
    mysql与pg的主键说明mysql插入顺序与插入随机测试案例B+Tree原理id顺序的方式插入id随机的方式插入Postgresql堆组织表CTIDPg的元组及索引原理Pg与mysql与oracel问题mysql插入顺序与插入随机测试案例  创建了两张表一张表是按顺序来插入的一张表......
  • Linux的MySQL数据库安装部署
    简介MySQL数据库,是知名的数据库系统,其特点是:轻量,简单,功能丰富。MySQL常用版本有MySQL5.7版本安装MySQL8.x版本安装MySQL在CentOS系统安装(5.7版本与8.0版本)注意:安装操作需要root权限安装配置yum仓库#导入更新密钥rpm--importhttps://repo.mysql.com/RPM-GPG......
  • 2023年十款开源测试开发工具推荐(自动化、性能、造数据、流量复制)
    ​1、AutoMeter-API自动化测试平台AutoMeter是一款针对分布式服务,微服务API做功能和性能一体化的自动化测试平台,一站式提供发布单元,API,环境,用例,前置条件,场景,计划,报告等管理在项目开发,迭代交付过程中开发人员,测试人员需要针对系统提供的API做调试,回归测试,性能测试。自动......
  • MySQL统计各种数据库对象大小
    MySQL统计各种数据库对象大小;包含:数据库、表、索引等脚本使用示例统计实例中各数据库大小SELECTTABLE_SCHEMA,round(SUM(data_length+index_length)/1024/1024,2)ASTOTAL_MB,round(SUM(data_length)/1024/1024,2)ASDATA_MB,round(SUM(index_length)/1024/1024,2)ASINDEX......
  • 全方位对比 Postgres 和 MySQL(2023 版)
    根据2023年的StackOverflow调研(https://survey.stackoverflow.co/2023/),Postgres已经取代MySQL成为最受敬仰和渴望(themostadmired,desired)的数据库。  随着Postgres的发展势头愈发强劲,在Postgres和MySQL之间做选择变得更难了。 如果看安装数量......
  • 想要实现高效数据复制?Paxos并不总是最佳选择!
    数据复制典型的算法就是Paxo和Raft。1分片元数据的存储分布式存储系统中,收到客户端请求后,承担路由功能的节点:先访问分片元数据(简称元数据),确定分片对应节点然后才访问真正数据元数据,一般包括分片的数据范围、数据量、读写流量和分片副本处于哪些物理节点及副本状态等信息。存储角度......
  • Docker下搭建MySql主从复制
    在Docker环境下搭建MySql主从复制,阅读此文章默认读者具备基础的Docker命令操作。 一、环境1、Docker版本:Dockerversion24.0.5,buildced09962、MySql:Serverversion:5.7.36MySQLCommunityServer(GPL)3、Centos:CentOSLinuxrelease7.9.2009(Core)4、数据库Master......
  • mysql Statement接口
    Statement接口是Java执行数据库操作的一个重要接口,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。java.sql.Statement接口用于执行静态的SQL语句并返回执行结果。在默认情况下,同一时间每个Statement接口只能打开一个ResultSet对象。因此,如果读取一个ResultSet......