MySQL修改复制用户及密码

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

1、更改复制账户密码

--演示环境,同一主机上的2个实例,主3406,从3506
--当前版本,注:master账户表明是对主库进行相关操作,slave则是对从库进行相关操作
[email protected][(none)]> show variables like ‘version‘;
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.6.12-log |
+---------------+------------+

--主库上的记录
[email protected][test]> select * from tb1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | robin |
+------+-------+

--从库上的记录
[email protected][test]> select * from tb1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | robin |
+------+-------+

--当前从库上的状态信息
[email protected][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         

--主库上复制账户的信息
[email protected][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‘ |
+----------------------------------------------------------------------------------------------------------------+

--修改复制账户密码
[email protected][test]> GRANT REPLICATION SLAVE ON *.* TO ‘repl‘@‘192.168.1.177‘ IDENTIFIED BY ‘replpwd‘;

--如下查询密码已更改
[email protected][test]> select user,host,password from mysql.user where user=‘repl‘;
+------+---------------+-------------------------------------------+
| user | host          | password                                  |
+------+---------------+-------------------------------------------+
| repl | 192.168.1.177 | *4A04E4FD524292A79E3DCFEBBD46094478F178EF |
+------+---------------+-------------------------------------------+

--更新记录
[email protected][test]> insert into tb1 values(2,‘fred‘);

--重库上可以查询到刚刚被更新的记录
[email protected][test]> select * from tb1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | robin |
|    2 | fred  |
+------+-------+

[email protected][test]> stop slave;
Query OK, 0 rows affected (0.02 sec)

[email protected][test]> start slave;
Query OK, 0 rows affected (0.01 sec)

--再次查看状态出现了错误提示
[email protected][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文件中
[email protected][test]> stop slave;

[email protected][test]> change master to
    -> master_user=‘repl‘,
    -> master_password=‘replpwd‘;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

--修改密码后,从库状态正常,以下检查结果不再列出
[email protected][test]> start slave;

--查看master.info,密码已更改且为名文
[email protected][(none)]> system grep repl /data/inst3506/data3506/master.info
repl
replpwd

2、更换复制账户及密码

[email protected][test]> GRANT REPLICATION SLAVE ON *.* TO ‘repl2‘@‘192.168.1.177‘ IDENTIFIED BY ‘Repl2‘;
Query OK, 0 rows affected (0.00 sec)  

[email protected][test]> stop slave;
Query OK, 0 rows affected (0.28 sec)

[email protected][test]> insert into tb1 values(3,‘jack‘);
Query OK, 1 row affected (0.00 sec)

[email protected][test]> change master to
    -> MASTER_USER=‘repl2‘,
    -> MASTER_PASSWORD=‘Repl2‘;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

[email protected][test]> system more /data/inst3506/data3506/master.info
23
inst3406bin.000001
3294834
192.168.1.177
repl2
Repl2
3406
  ..........

[email protected][test]> start slave;
Query OK, 0 rows affected (0.01 sec)

[email protected][test]> select * from tb1 where id=3;
+------+------+
| id   | name |
+------+------+
|    3 | jack |
+------+------+
1 row in set (0.00 sec)

[email protected][(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)

时间: 2024-10-24 16:33:08

MySQL修改复制用户及密码的相关文章

设置、修改及找回 MySQL 数据库 root 用户的密码

1. MySQL 数据库用户安全策略介绍 安装 MySQL 数据库后,默认的管理员 root 密码为空,很不安全,需要设置密码.针对 MySQL数据库的用户处理,还有更严格的做法: ① 增加 system 并提升权限为超级管理员,即和 root 等价的用户,只是名字不同.     mysql> grant all privileges on *.* to [email protected]'localhost' identified by 'alinuxer123' with grant opt

Mysql修改和破解登录密码(详)

Mysql修改或破解登录密码 一.重置MySQL管理密码 跳过授权表启动MySQL服务程序 这一步主要利用mysqld的 --skip-grant-tables选项,具体操作时可选择不同方式,但本质上是一样的.以下提供三种方式,任选其中一种都可以. 方式1(推荐),执行mysql脚本起服务,末尾加 --skip-grant-tables 参数: [[email protected] ~]# service mysql stop  服务关闭 [[email protected] ~]# servi

如何通过OWA登录界面修改域用户的密码

Exchange邮箱用户可以登录OWA修改密码,当AD用户密码过期或者管理员给用户重置密码时勾选了"用户下次登录时须更改密码"时,用户登录Exchange 2013 OWA时会自动跳到修改密码的页面,但是对于只有AD用户没有邮箱的用户,则无法使用OWA修改密码. 前段时间做项目时,客户提出1个需求:在OWA首页增加一个更改密码的链接,以方便AD用户随意自主修改密码. 通过研究发现是可以实现的,实现方法如下: 1. 更改密码的页面使用的是Exchange 2013前端自带的Expired

重设MySQL数据库root用户的密码

在已知MYSQL数据库的ROOT用户密码的情况下,修改密码的方法: 1.  在SHELL环境下,使用mysqladmin命令设置:mysqladmin –u root –p password "新密码"   回车后要求输入旧密码 2.  在mysql>环境中,使用update命令,直接更新mysql库user表的数据: Update mysql.user  set  password=password('新密码')  where  user='root'; flush   pri

Linux如何修改root用户的密码

Linux系统的root账号是非常重要的一个账号,也是权限最大的一个账号,但是有时候忘了root密码怎么办?总不能重装系统吧,这个是下下策,其实Linux系统中,如果忘记了root账号密码,是可以通过进入单用户模式或其它方法修改密码的.当然,具体方式跟Boot Loader.操作系统有关系,GRUB与LILO的实现方式有所不同.Boot Loader 是在操作系统内核运行之前运行的一段小程序.通过这段小程序,我们可以初始化硬件设备.建立内存空间的映射图,从而将系统的软硬件环境带到一个合适的状态,

Mysql修改root用户密码 For Mac

环境 Mysql版本:5.7.12 操作系统:OSX 10.11 安装文件:.dmg文件 MySQL:mysql-5.7.12-osx10.11-x86_64.dmg(注意5.7跟之前的字段有些不同,下面会说) Step 1 关闭mysql服务.无论你有没有开启mysql服务,保险起见都要运行一下以下命令. sudo /usr/local/mysql/support-files/mysql.server stop (注:我这里报错了:ERROR! MySQL server PID file co

mysql如何修改root用户的密码

方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:mysql> set password for [email protected] = password('123'); 方法2:用mysqladmin 格式:mysqladmin -u用户名 -p旧密码 password 新密码 例子:mysqladmin -uroot -p123456 password

Centos系统mysql 忘记root用户的密码

第一步:(停掉正在运行的mysql)[root@maomao ~]# service mysqld stop  Stopping MySQL:                                            [  OK  ] 第二步:使用 "--skip-grant-tables"参数重新启动mysql[[email protected] ~]# mysqld_safe --skip-grant-tables [[email protected] ~]# Star

CentOS中对MySql的root用户重置密码

由于一般的修改ini文件来完成无密码登录数据库容易造成较大风险,所以使用修改启动服务参数的方式来更新user表,达到无密码登陆的目的. 1.关闭系统下运行的mysql服务 (1)杀掉进程号达到关闭目的 先使用ps -ef|grep mysql查找进程号,然后使用kill+ PID的方式杀掉进程. (2)使用systemctl工具来关闭mysql服务 systemctl stop mysql 2.修改mysql的启动选项更新user表,达到无密码登陆的效果 systemctl set-enviro