1 つのマスターと 1 つのスレーブで mysql8 クラスターを構築する

ホスト(hadoop102)の設定ファイルを変更する

#配置mysql8的主从复制-----修改主机配置文件
#主服务器唯一ID 
server-id=1 
#启用二进制日志 
log-bin=mysql-bin
#设置需要复制的数据库 
binlog-do-db=mydb1                  
#设置logbin格式 
binlog_format=STATEMENT

#0表示大小写,1表示不区分大小写
lower_case_table_names=0 
#添加密码验证插件
plugin-load-add=validate_password.so

#服务器在启动时加载插件,并防止在服务器运行时删除插件
validate-password=FORCE_PLUS_PERMANENT

スレーブ設定ファイルを変更する

#配置mysql8的主从复制------从机配置文件
##主服务器唯一ID 
server-id=2
#启用中继日志
relay-log=mysql-relay

マスター/スレーブの mysql サービスを再起動し、ファイアウォールを閉じます。

ザ・ホスト

[root@mysql ~]# 
[root@mysql ~]# systemctl restart mysqld

[root@mysql ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-07-11 12:32:17 CST; 26s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 17068 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 17100 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 23500)
   Memory: 394.0M
   CGroup: /system.slice/mysqld.service
           └─17100 /usr/sbin/mysqld

Jul 11 12:31:13 mysql systemd[1]: Starting MySQL Server...
Jul 11 12:32:17 mysql systemd[1]: Started MySQL Server.
[root@mysql ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2023-07-10 09:27:37 CST; 1 day 3h ago
     Docs: man:firewalld(1)
 Main PID: 1093 (firewalld)
    Tasks: 2 (limit: 23500)
   Memory: 30.9M
   CGroup: /system.slice/firewalld.service
           └─1093 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

Jul 10 09:27:35 mysql systemd[1]: Starting firewalld - dynamic firewall daemon...
Jul 10 09:27:37 mysql systemd[1]: Started firewalld - dynamic firewall daemon.
Jul 10 09:27:37 mysql firewalld[1093]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure>

[root@mysql ~]# systemctl stop firewalld

スレーブ


[root@mysql2 yum.repos.d]# systemctl restart mysqld
[root@mysql2 yum.repos.d]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-07-11 12:31:29 CST; 39s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 42459 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 42487 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 23372)
   Memory: 398.6M
   CGroup: /system.slice/mysqld.service
           └─42487 /usr/sbin/mysqld

Jul 11 12:31:27 mysql2 systemd[1]: Starting MySQL Server...
Jul 11 12:31:29 mysql2 systemd[1]: Started MySQL Server.
[root@mysql2 yum.repos.d]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2023-07-11 11:32:06 CST; 1h 0min ago
     Docs: man:firewalld(1)
 Main PID: 1214 (firewalld)
    Tasks: 2 (limit: 23372)
   Memory: 31.0M
   CGroup: /system.slice/firewalld.service
           └─1214 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

Jul 11 11:32:06 mysql2 systemd[1]: Starting firewalld - dynamic firewall daemon...
Jul 11 11:32:06 mysql2 systemd[1]: Started firewalld - dynamic firewall daemon.
Jul 11 11:32:06 mysql2 firewalld[1214]: WARNING: AllowZoneDrifting is enabled. This is considered an insecur>
...skipping...
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2023-07-11 11:32:06 CST; 1h 0min ago
     Docs: man:firewalld(1)
 Main PID: 1214 (firewalld)
    Tasks: 2 (limit: 23372)
   Memory: 31.0M
   CGroup: /system.slice/firewalld.service
           └─1214 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

Jul 11 11:32:06 mysql2 systemd[1]: Starting firewalld - dynamic firewall daemon...
Jul 11 11:32:06 mysql2 systemd[1]: Started firewalld - dynamic firewall daemon.
Jul 11 11:32:06 mysql2 firewalld[1214]: WARNING: AllowZoneDrifting is enabled. This is considered an insecur>

[root@mysql2 yum.repos.d]# systemctl stop firewalld.service 

ホスト上でアカウントを作成し、スレーブを認証します

[root@mysql ~]# mysql -uroot -pmaster
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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> create user 'slave2'@'%' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password.policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> create user 'slave2'@'%' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password.length=6;
Query OK, 0 rows affected (0.00 sec)

mysql> create user 'slave2'@'%' identified by '123456';
Query OK, 0 rows affected (0.02 sec)

mysql> 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave2'@'%';  //在主机MySQL里执行授权命令
Query OK, 0 rows affected (0.03 sec)

mysql> ALTER USER 'slave2'@'%' IDENTIFIED WITH mysql_native_password BY '123456';//此语句必须执行。否则见下面。
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status; //查询master的状态
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1144 | mydb1        |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)



スレーブマシンにコピーする必要があるホスト情報を設定します。

[root@mysql2 yum.repos.d]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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='192.168.182.152', //主机IP
    -> MASTER_USER='slave2', 
    -> MASTER_PASSWORD='123456',//主机设置的登录密码
    -> MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=1144;//主机上查到的Position码
Query OK, 0 rows affected, 8 warnings (0.01 sec)

mysql> start slave; //#启动从服务器复制功能
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show slave status\G; //查看从服务器状态 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.182.152
                  Master_User: slave2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1144
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000001
             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: 1144
              Relay_Log_Space: 532
              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: 85d65c74-1eef-11ee-9285-000c2989e779
             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.00 sec)

ERROR: 
No query specified


mysql8 1 マスター 1 スレーブ クラスターが正常に構築されていることを確認します。

ホストは新しいライブラリを作成し、新しいテーブルを作成し、データを挿入し、スレーブからコピーします。ホストのデータベースに入り、コマンドを実行します。

mysql> create database mydb1;
Query OK, 1 row affected (0.00 sec)

mysql> use mydb1;
Database changed
mysql> create table mydb1(id int,name VARCHAR(60));
Query OK, 0 rows affected (0.10 sec)

mysql> insert into mydb1 values(1,"wqb");
Query OK, 1 row affected (0.64 sec)
mysql> exit;

作成したテーブルがスレーブマシン上で同期されているかどうかを確認します


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

mysql> use mydb1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from mydb1;
+------+------+
| id   | name |
+------+------+
|    1 | wqb  |
+------+------+
1 row in set (0.00 sec)

mysql> exit;
Bye

スレーブコピー機能を停止する

stop slave;

マスター/スレーブを再構成する

stop slave; 
reset master;

おすすめ

転載: blog.csdn.net/weixin_44048054/article/details/131657673