mysql 8.0.28 主从配置

该博客详细记录了MySQL8.0.28的主从复制配置过程。首先在主服务器上设置配置文件,初始化、安装并启动服务,创建并授权复制用户。然后,在两个从服务器上同样配置并连接到主服务器,完成主从同步。在从服务器上,通过`change master`和`start slave`命令设置并启动复制。

mysql 8.0.28 主从配置

1. master

配置文件

#c:/dbm/m3300/my.ini
[mysqld]
port=3300
log-bin=mysql-bin
server-id=1

初始化、安装、启动

C:\dbm\m3300\bin>mysqld --initialize-insecure
C:\dbm\m3300\bin>mysqld --install m3300
Service successfully installed.

C:\dbm\m3300\bin>net start m3300
m3300 服务正在启动 ......
m3300 服务已经启动成功。

C:\dbm\m3300\bin>mysql -P3300 -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> select version();
+-----------+
| version() |
+-----------+
| 8.0.28    |
+-----------+
1 row in set (0.00 sec)

建立主从账号授权

alter user rep identified with mysql_native_password by '123';

必须使用mysql_natvie_password插件加密机制

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      157 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.04 sec)

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 157
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

mysql> create user rep identified by '123';
Query OK, 0 rows affected (0.26 sec)

mysql> grant replication slave on *.* to rep;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to rep;
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     1071 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2. slave1

my.ini

[mysqld]
port=3301
log-bin=mysql-bin
server-id=2

初始化、安装、启动、查看信息

C:\dbm\m3301\bin>mysqld --initialize-insecure

C:\dbm\m3301\bin>mysqld --install m3301
Service successfully installed.

C:\dbm\m3301\bin>net start m3301
m3301 服务正在启动 .
m3301 服务已经启动成功。


C:\dbm\m3301\bin>mysql -uroot -P3301
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> select version();
+-----------+
| version() |
+-----------+
| 8.0.28    |
+-----------+
1 row in set (0.00 sec)

mysql> change master to master_host='127.0.0.1',
    -> master_user='rep',
    -> master_password='123',
    -> master_log_file=' mysql-bin.000002',
    -> master_log_pos=1071;
Query OK, 0 rows affected, 7 warnings (0.05 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to source
                  Master_Host: 127.0.0.1
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File:  mysql-bin.000002
          Read_Master_Log_Pos: 1071
               Relay_Log_File: MS-UXUYPZYCFGQB-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File:  mysql-bin.000002
             Slave_IO_Running: Connecting
            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: 1071
              Relay_Log_Space: 157
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error connecting to master 'rep@127.0.0.1:3306' - retry-time: 60 retries: 1 message: Can't connect to MySQL server on '127.0.0.1:3306' (10061)
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
                  Master_UUID:
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp: 220219 11:41:59
     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:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set, 1 warning (0.01 sec)

image-20220219125844751

3. slave2

[mysqld]
port=3302
log-bin=mysql-bin
server-id=3
C:\dbm\m3302\bin>mysqld --initialize-insecure

C:\dbm\m3302\bin>mysqld --install m3302
Service successfully installed.

C:\dbm\m3302\bin>net start m3302
m3302 服务正在启动 .
m3302 服务已经启动成功。


C:\dbm\m3302\bin>mysql -uroot -P3302
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> change master to master_host='127.0.0.1',
    -> master_user='rep',
    -> master_password='123',
    -> master_port=3300,
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=5264;
Query OK, 0 rows affected, 8 warnings (0.12 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 127.0.0.1
                  Master_User: rep
                  Master_Port: 3300
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 5443
               Relay_Log_File: MS-UXUYPZYCFGQB-relay-bin.000002
                Relay_Log_Pos: 505
        Relay_Master_Log_File: mysql-bin.000002
             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: 5443
              Relay_Log_Space: 725
              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: c8414953-9133-11ec-a320-1c3947e43531
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica 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:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set, 1 warning (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db                 |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> use db;
Database changed
mysql> show tables;
Empty set (0.01 sec)

mysql> show tables;
+--------------+
| Tables_in_db |
+--------------+
| t            |
+--------------+
1 row in set (0.00 sec)

从机上执行

change

start slave

stop slave

change master to master_host='127.0.0.1',
master_user='rep',
master_password='123',
master_port=3300,
master_log_file='mysql-bin.000002',
master_log_pos=5264;

image-20220219130913201

image-20220219131002163

image-20220219131312161

<think>好的,我现在需要帮用户解决如何在MySQL 8.0.28中设置root用户支持远程访问的问题。用户已经提供了一些引用资料,我需要仔细看看这些引用内容,然后结合自己的知识来整理出正确的步骤。 首先看引用1,里面提到使用GRANT命令赋予root所有权限,并允许任意IP登录。但这里要注意,MySQL 8.0之后可能不再推荐直接用GRANT来创建用户和权限,而是需要先创建用户再授权。不过引用中的命令可能还是有效的,但可能需要先更新root用户的host字段。 然后引用2,用户提到用update语句将root的host从localhost改为%,然后刷新权限。这应该是关键步骤,因为默认情况下root可能只允许本地连接。另外,引用2还提到了查看用户状态的语句,这有助于确认当前root的host设置。 引用3主要涉及主从配置和权限刷新,这里可能不太相关,但刷新权限是必要的步骤,执行FLUSH PRIVILEGES;这点需要注意。 现在结合这些引用,我需要整理出一个正确的流程。首先,可能需要登录MySQL,然后修改root的host为%,赋予权限,然后刷新权限。不过MySQL 8.0可能对权限管理有更严格的要求,比如可能需要先创建用户或者使用ALTER USER命令。 还要注意,MySQL 8.0默认使用的认证插件是caching_sha2_password,而有些客户端可能不支持,可能需要更改为mysql_native_password,但用户的问题中没有提到连接问题,可能不需要这一步,但可能需要检查插件类型,如果遇到问题可以调整。 所以正确的步骤可能是: 1. 使用root用户登录MySQL。 2. 切换到mysql数据库。 3. 更新user表中root用户的host为%。 4. 授予root用户所有权限。 5. 刷新权限。 6. 检查防火墙是否放行3306端口。 7. 重启MySQL服务使更改生效。 但需要注意,直接使用UPDATE修改user表可能在MySQL 8.0中不是推荐的做法,更安全的方式是使用CREATE USER和GRANT语句,但root用户可能特殊,需要直接修改。 或者,使用ALTER USER语句来更改root的主机。例如: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; 但不确定是否需要。或者,如果root已经存在,可能需要先删除原来的root@localhost,再创建root@%?不过这样可能有风险,需要谨慎。 根据引用2中的步骤,用户是直接更新user表中的host字段,然后刷新权限。这可能适用于MySQL 8.0.28,但需要确认。 此外,可能需要检查MySQL的配置文件(如my.cnf)中的bind-address是否为0.0.0.0,以允许外部连接,否则即使权限正确,MySQL也可能不监听外部请求。 综合这些信息,正确的步骤应包括: - 修改root的host为% - 授予权限 - 刷新权限 - 调整配置文件 - 处理防火墙 - 重启服务 现在需要将这些步骤有条理地呈现给用户,并确保每一步都正确无误,适用于MySQL 8.0.28版本。</think>### 分步配置MySQL 8.0.28的root用户远程访问 #### 1. 登录MySQL数据库 通过管理员权限连接MySQL(注意替换实际密码): ```bash mysql -u root -p ``` 输入密码后进入MySQL命令行。 #### 2. 切换到系统数据库 ```sql USE mysql; ``` #### 3. 修改root用户访问权限 **方法一:直接更新用户表(推荐)** ```sql UPDATE user SET host='%' WHERE user='root'; ``` **方法二:创建新root用户(可选)** 若需保留本地root@localhost账户,可新建root@%账户: ```sql CREATE USER 'root'@'%' IDENTIFIED BY '你的密码'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; ``` #### 4. 刷新权限 ```sql FLUSH PRIVILEGES; ``` #### 5. 验证用户状态 执行以下命令确认root的host已更新为`%`: ```sql SELECT user, host FROM user WHERE user='root'; ``` #### 6. 修改MySQL配置文件 编辑`/etc/mysql/my.cnf`或`/etc/mysql/mysql.conf.d/mysqld.cnf`,确保包含: ```ini bind-address = 0.0.0.0 ``` #### 7. 配置防火墙 开放3306端口(根据系统选择命令): ```bash # Ubuntu ufw allow 3306/tcp # CentOS firewall-cmd --permanent --add-port=3306/tcp ``` #### 8. 重启MySQL服务 ```bash systemctl restart mysql # 或 service mysql restart ``` #### 9. 远程连接测试 通过其他设备使用客户端工具连接: ```bash mysql -u root -h MySQL服务器IP -p ``` ### 常见问题排查 1. **认证插件冲突** MySQL 8.0默认使用`caching_sha2_password`插件,若客户端不支持可修改为传统插件: ```sql ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码'; ``` 2. **权限未生效** 确保执行`FLUSH PRIVILEGES`并重启服务[^2][^3]。 3. **连接超时** 检查防火墙规则、云服务器安全组策略及网络路由。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值