centos安装主从mysql集群

发布时间:2024年01月20日

在 CentOS 系统上安装和配置 MySQL 主从复制环境的步骤与 Debian/Ubuntu 系统有所不同。以下是在 CentOS 系统上进行配置的详细步骤:

步骤 1:在主服务器上安装和配置 MySQL(10.206.0.13)

  1. 安装 MySQL 服务器:

    • 首先,添加 MySQL Yum 仓库:
      sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
      
    • 接着,安装 MySQL 服务器:
      sudo yum install mysql-server
      
  2. 启动 MySQL 服务并设置开机自启:

    sudo systemctl start mysqld
    sudo systemctl enable mysqld
    
  3. 安全配置 MySQL:

    • 初始安装完成后,MySQL 生成一个临时密码。使用以下命令查看密码:
      sudo grep 'temporary password' /var/log/mysqld.log
      
    • 运行安全安装脚本来设置 root 密码并完成其他安全设置:
      sudo mysql_secure_installation
      
  4. 配置 MySQL 以支持复制:

    • 编辑 MySQL 配置文件:
      sudo vi /etc/my.cnf
      
    • [mysqld] 部分添加以下配置:
      server-id=1
      log_bin=mysql-bin
      binlog_do_db=mydb  # 替换为你的数据库名
      
    • 保存并关闭文件。
  5. 重启 MySQL 服务:

    sudo systemctl restart mysqld
    
  6. 创建复制用户:

    • 登录 MySQL:
      mysql -u root -p
      
    • 创建复制用户并授予权限:
      CREATE USER 'replica'@'10.206.0.8' IDENTIFIED BY 'password';  -- 设置一个安全的密码
      GRANT REPLICATION SLAVE ON *.* TO 'replica'@'10.206.0.8';
      FLUSH PRIVILEGES;
      SHOW MASTER STATUS;
      
    • 记录 FilePosition 的值,稍后在从服务器上配置时将需要这些信息。

步骤 2:在从服务器上安装和配置 MySQL(10.206.0.8)

  1. 安装 MySQL 服务器:

    • 添加 MySQL Yum 仓库:
      sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
      
    • 安装 MySQL 服务器:
      sudo yum install mysql-server
      
  2. 启动 MySQL 服务并设置开机自启:

    sudo systemctl start mysqld
    sudo systemctl enable mysqld
    
  3. 安全配置 MySQL:

    • 查看初始临时密码:
      sudo grep 'temporary password' /var/log/mysqld.log
      
    • 运行安全安装脚本:
      sudo mysql_secure_installation
      
  4. 配置 MySQL 以支持复制:

    • 编辑 MySQL 配置文件:
      sudo vi /etc/my.cnf
      
    • [mysqld] 部分添加以下配置:
      server-id=2
      relay_log=mysql-relay-bin
      
    • 保存并关闭文件。
  5. 重启 MySQL 服务:

    sudo systemctl restart mysqld
    
  6. 配置从服务器以连接到主服务器:

    • 登录 MySQL:
      mysql -u root -p
      
    • 设置复制并开始复制过程:
      CHANGE MASTER TO
      MASTER_HOST='10.206.0.13',
      MASTER_USER='replica',
      MASTER_PASSWORD='password',  -- 主服务器上设置的密码
      MASTER_LOG_FILE='之前记录的文件名',
      MASTER_LOG_POS=之前记录的位置;
      
      START SLAVE;
      
      SHOW SLAVE STATUS\G
      
    • 确保 `Slave_IO_R

主服务器的安装记录

Welcome to TencentOS 3 64bit
Version 3.1 20230621
tlinux3.1-64bit-5.4.119-19.0009.28-20230621
Last failed login: Sat Jan 20 14:01:05 CST 2024 from 85.209.11.254 on ssh:notty
There were 66324 failed login attempts since the last successful login.
Last login: Sun Jan  7 16:08:16 2024 from 114.84.30.36
[root@node1 ~]# sudo apt update
sudo: apt: command not found
[root@node1 ~]# ls
Changelog  tmp.log
[root@node1 ~]# cd /home
[root@node1 home]# ls
postgresql-master
[root@node1 home]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS                                       NAMES
3e97b56c1293   postgres   "docker-entrypoint.s…"   12 days ago   Up 12 days   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres-master
[root@node1 home]# mkdir mysql-master
[root@node1 home]# ls
mysql-master  postgresql-master
[root@node1 home]# cd mysql-master/
[root@node1 mysql-master]# ls
[root@node1 mysql-master]# cd sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
-bash: cd: too many arguments
[root@node1 mysql-master]# sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
TencentOS Server 3.1 - TencentOS                                                                                          46 kB/s | 3.8 kB     00:00    
TencentOS Server 3.1 - Updates                                                                                            44 kB/s | 3.8 kB     00:00    
TencentOS Server 3.1 - Updates                                                                                            11 MB/s |  30 MB     00:02    
TencentOS Server 3.1 - TencentOS-AppStream                                                                                55 kB/s | 4.2 kB     00:00    
TencentOS Server 3.1 - TencentOS-AppStream                                                                                15 MB/s |  20 MB     00:01    
TencentOS Server 3.1 - Base                                                                                               41 kB/s | 3.0 kB     00:00    
TencentOS Server 3.1 - AppStream                                                                                          32 kB/s | 2.9 kB     00:00    
TencentOS Server 3.1 - Extras                                                                                             35 kB/s | 3.0 kB     00:00    
TencentOS Server 3.1 - Extras                                                                                            127 kB/s |  25 kB     00:00    
TencentOS Server 3.1 - PowerTools                                                                                         31 kB/s | 3.0 kB     00:00    
Docker CE Stable - x86_64                                                                                                 47 kB/s | 3.5 kB     00:00    
Extra Packages for TencentOS Server 3.1 - x86_64                                                                         257 kB/s | 4.7 kB     00:00    
Extra Packages for TencentOS Server 3.1 - x86_64                                                                          27 MB/s |  16 MB     00:00    
Extra Packages for TencentOS Server 3.1 Modular - x86_64                                                                 186 kB/s | 3.0 kB     00:00    
Kubernetes                                                                                                                20 kB/s | 1.4 kB     00:00    
mysql80-community-release-el7-3.noarch.rpm                                                                               5.7 kB/s |  25 kB     00:04    
Dependencies resolved.
=========================================================================================================================================================
 Package                                          Architecture                  Version                        Repository                           Size
=========================================================================================================================================================
Installing:
 mysql80-community-release                        noarch                        el7-3                          @commandline                         25 k

Transaction Summary
=========================================================================================================================================================
Install  1 Package

Total size: 25 k
Installed size: 31 k
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                 1/1 
  Installing       : mysql80-community-release-el7-3.noarch                                                                                          1/1 
  Verifying        : mysql80-community-release-el7-3.noarch                                                                                          1/1 

Installed:
  mysql80-community-release-el7-3.noarch                                                                                                                 

Complete!
[root@node1 mysql-master]# sudo yum install mysql-server
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
MySQL 8.0 Community Server                                                                                               1.1 MB/s | 3.3 MB     00:03    
MySQL Connectors Community                                                                                                68 kB/s |  68 kB     00:00    
MySQL Tools Community                                                                                                    472 kB/s | 1.1 MB     00:02    
Dependencies resolved.
=========================================================================================================================================================
 Package                        Architecture            Version                                               Repository                            Size
=========================================================================================================================================================
Installing:
 mysql-server                   x86_64                  8.0.32-1.module+el8.8.0+484+537fff7e                  TencentOS-AppStream                   32 M
Installing dependencies:
 mecab                          x86_64                  0.996-2.module+el8.8.0+484+537fff7e                   TencentOS-AppStream                  392 k
 mysql-errmsg                   x86_64                  8.0.32-1.module+el8.8.0+484+537fff7e                  TencentOS-AppStream                  628 k
 protobuf-lite                  x86_64                  3.5.0-15.tl3                                          Updates                              148 k

Transaction Summary
=========================================================================================================================================================
Install  4 Packages

Total download size: 33 M
Installed size: 171 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): protobuf-lite-3.5.0-15.tl3.x86_64.rpm                                                                             635 kB/s | 148 kB     00:00    
(2/4): mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64.rpm                                                              1.2 MB/s | 392 kB     00:00    
(3/4): mysql-errmsg-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64.rpm                                                      1.6 MB/s | 628 kB     00:00    
(4/4): mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64.rpm                                                       17 MB/s |  32 MB     00:01    
---------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                     16 MB/s |  33 MB     00:02     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                 1/1 
  Installing       : mysql-errmsg-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        1/4 
  Installing       : mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64                                                                                2/4 
  Running scriptlet: mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64                                                                                2/4 
  Installing       : protobuf-lite-3.5.0-15.tl3.x86_64                                                                                               3/4 
  Running scriptlet: mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        4/4 
  Installing       : mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        4/4 
  Running scriptlet: mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        4/4 
ValueError: File context for /var/log/mysql(/.*)? already defined

  Verifying        : protobuf-lite-3.5.0-15.tl3.x86_64                                                                                               1/4 
  Verifying        : mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64                                                                                2/4 
  Verifying        : mysql-errmsg-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        3/4 
  Verifying        : mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        4/4 

Installed:
  mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64                            mysql-errmsg-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                   
  mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                    protobuf-lite-3.5.0-15.tl3.x86_64                                          

Complete!
[root@node1 mysql-master]# sudo systemctl start mysqld
[root@node1 mysql-master]# sudo systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@node1 mysql-master]# sudo systemctl enable mysqld
[root@node1 mysql-master]# sudo grep 'temporary password' /var/log/mysqld.log
grep: /var/log/mysqld.log: No such file or directory
[root@node1 mysql-master]# sudo grep 'shtc@2024' /var/log/mysqld.log
grep: /var/log/mysqld.log: No such file or directory
[root@node1 mysql-master]# cd /var
[root@node1 var]# ls
account  adm  cache  crash  db  empty  ftp  games  gopher  kerberos  lib  local  lock  log  mail  nis  opt  preserve  run  spool  tmp  yp
[root@node1 var]# cd lo
local/ lock/  log/   
[root@node1 var]# cd lo
local/ lock/  log/   
[root@node1 var]# cd log
[root@node1 log]# ls
anaconda           cloud-init.log         dnf.librepo.log      lastlog            messages-20231231  secure            spooler-20240107
audit              cloud-init-output.log  dnf.log              maillog            messages-20240107  secure-20231224   spooler-20240114
boot.log           containers             dnf.rpm.log          maillog-20231224   messages-20240114  secure-20231231   sssd
boot.log-20231214  cron                   hawkey.log           maillog-20231231   mysql              secure-20240107   wtmp
boot.log-20231220  cron-20231224          hawkey.log-20231217  maillog-20240107   pods               secure-20240114
btmp               cron-20231231          hawkey.log-20231224  maillog-20240114   private            spooler
btmp-20240101      cron-20240107          iptraf-ng            messages           qcloud_action.log  spooler-20231224
chrony             cron-20240114          kdump.log            messages-20231224  sa                 spooler-20231231
[root@node1 log]# cd mysql
[root@node1 mysql]# ls
mysqld.log
[root@node1 mysql]# sudo grep 'shtc@2024' /var/log/mysql/mysqld.log
[root@node1 mysql]# sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password: 

Re-enter new password: 
Sorry, passwords do not match.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[root@node1 mysql]# 
[root@node1 mysql]# sudo vi /etc/my.cnf
[root@node1 mysql]# sudo vi /etc/my.cnf
[root@node1 mysql]# cd  /etc/my.cnf.d/
[root@node1 my.cnf.d]# ls
client.cnf  mysql-default-authentication-plugin.cnf  mysql-server.cnf
[root@node1 my.cnf.d]# vi mysql-server.cnf 
[root@node1 my.cnf.d]# sudo systemctl restart mysqld
[root@node1 my.cnf.d]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32 Source distribution

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 'replica'@'10.206.0.8' IDENTIFIED BY 'Shtc@2024';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'replica'@'10.206.0.8';
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.000001 |      851 | mysqldb      |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

从服务器的安装记录

Welcome to TencentOS 3 64bit
Version 3.1 20230621
tlinux3.1-64bit-5.4.119-19.0009.28-20230621
Last failed login: Sat Jan 20 14:19:36 CST 2024 from 157.245.199.76 on ssh:notty
There were 52883 failed login attempts since the last successful login.
Last login: Sun Jan  7 16:09:23 2024 from 114.84.30.36
[root@node2 ~]# sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
TencentOS Server 3.1 - TencentOS                                                                                          48 kB/s | 3.8 kB     00:00    
TencentOS Server 3.1 - Updates                                                                                            46 kB/s | 3.8 kB     00:00    
TencentOS Server 3.1 - Updates                                                                                            26 MB/s |  30 MB     00:01    
TencentOS Server 3.1 - TencentOS-AppStream                                                                                51 kB/s | 4.2 kB     00:00    
TencentOS Server 3.1 - TencentOS-AppStream                                                                                20 MB/s |  20 MB     00:01    
TencentOS Server 3.1 - Base                                                                                               38 kB/s | 3.0 kB     00:00    
TencentOS Server 3.1 - AppStream                                                                                          35 kB/s | 2.9 kB     00:00    
TencentOS Server 3.1 - Extras                                                                                             37 kB/s | 3.0 kB     00:00    
TencentOS Server 3.1 - Extras                                                                                            123 kB/s |  25 kB     00:00    
TencentOS Server 3.1 - PowerTools                                                                                         37 kB/s | 3.0 kB     00:00    
Docker CE Stable - x86_64                                                                                                 43 kB/s | 3.5 kB     00:00    
Extra Packages for TencentOS Server 3.1 - x86_64                                                                         241 kB/s | 4.7 kB     00:00    
Extra Packages for TencentOS Server 3.1 - x86_64                                                                          26 MB/s |  16 MB     00:00    
Extra Packages for TencentOS Server 3.1 Modular - x86_64                                                                 136 kB/s | 3.0 kB     00:00    
Kubernetes                                                                                                                47 kB/s | 1.4 kB     00:00    
mysql80-community-release-el7-3.noarch.rpm                                                                               7.6 kB/s |  25 kB     00:03    
Dependencies resolved.
=========================================================================================================================================================
 Package                                          Architecture                  Version                        Repository                           Size
=========================================================================================================================================================
Installing:
 mysql80-community-release                        noarch                        el7-3                          @commandline                         25 k

Transaction Summary
=========================================================================================================================================================
Install  1 Package

Total size: 25 k
Installed size: 31 k
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                 1/1 
  Installing       : mysql80-community-release-el7-3.noarch                                                                                          1/1 
  Verifying        : mysql80-community-release-el7-3.noarch                                                                                          1/1 

Installed:
  mysql80-community-release-el7-3.noarch                                                                                                                 

Complete!
[root@node2 ~]# sudo yum install mysql-server
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
MySQL 8.0 Community Server                                                                                                95 kB/s | 3.3 MB     00:35    
MySQL Connectors Community                                                                                                16 kB/s |  68 kB     00:04    
MySQL Tools Community                                                                                                     80 kB/s | 1.1 MB     00:14    
Dependencies resolved.
=========================================================================================================================================================
 Package                        Architecture            Version                                               Repository                            Size
=========================================================================================================================================================
Installing:
 mysql-server                   x86_64                  8.0.32-1.module+el8.8.0+484+537fff7e                  TencentOS-AppStream                   32 M
Installing dependencies:
 mecab                          x86_64                  0.996-2.module+el8.8.0+484+537fff7e                   TencentOS-AppStream                  392 k
 mysql-errmsg                   x86_64                  8.0.32-1.module+el8.8.0+484+537fff7e                  TencentOS-AppStream                  628 k
 protobuf-lite                  x86_64                  3.5.0-15.tl3                                          Updates                              148 k

Transaction Summary
=========================================================================================================================================================
Install  4 Packages

Total download size: 33 M
Installed size: 171 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): protobuf-lite-3.5.0-15.tl3.x86_64.rpm                                                                             670 kB/s | 148 kB     00:00    
(2/4): mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64.rpm                                                              1.2 MB/s | 392 kB     00:00    
(3/4): mysql-errmsg-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64.rpm                                                      1.4 MB/s | 628 kB     00:00    
(4/4): mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64.rpm                                                       10 MB/s |  32 MB     00:03    
---------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                    9.8 MB/s |  33 MB     00:03     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                 1/1 
  Installing       : mysql-errmsg-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        1/4 
  Installing       : mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64                                                                                2/4 
  Running scriptlet: mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64                                                                                2/4 
  Installing       : protobuf-lite-3.5.0-15.tl3.x86_64                                                                                               3/4 
  Running scriptlet: mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        4/4 
  Installing       : mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        4/4 
  Running scriptlet: mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        4/4 
ValueError: File context for /var/log/mysql(/.*)? already defined

  Verifying        : protobuf-lite-3.5.0-15.tl3.x86_64                                                                                               1/4 
  Verifying        : mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64                                                                                2/4 
  Verifying        : mysql-errmsg-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        3/4 
  Verifying        : mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                                                                        4/4 

Installed:
  mecab-0.996-2.module+el8.8.0+484+537fff7e.x86_64                            mysql-errmsg-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                   
  mysql-server-8.0.32-1.module+el8.8.0+484+537fff7e.x86_64                    protobuf-lite-3.5.0-15.tl3.x86_64                                          

Complete!
[root@node2 ~]# sudo systemctl start mysqld
[root@node2 ~]# sudo systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@node2 ~]# sduo grep 'shtc@2024' /var/log/mysql/mysqld.log 
bash: sduo: command not found...
Similar command is: 'sudo'
[root@node2 ~]# grep 'shtc@2024' /var/log/mysql/mysqld.log 
[root@node2 ~]# sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[root@node2 ~]# sudo vi /etc/my.cnf
[root@node2 ~]# cd  /etc/my.cnf/
-bash: cd: /etc/my.cnf/: Not a directory
[root@node2 ~]# cd /etc/my.cnf.d/
[root@node2 my.cnf.d]# ls
client.cnf  mysql-default-authentication-plugin.cnf  mysql-server.cnf
[root@node2 my.cnf.d]# vi mysql-server.cnf 
[root@node2 my.cnf.d]# sudo systemctl restart mysqld
[root@node2 my.cnf.d]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32 Source distribution

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='10.206.0.13',
    -> MASTER_USER='replica',
    -> MASTER_PASSWORD='password',  -- 主服务器上设置的密码
    -> MASTER_LOG_FILE='之前记录的文件名',
    -> MASTER_LOG_POS=之前记录的位置;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '之前记录的位置' at line 6
mysql> 
mysql> START SLAVE;
ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO
mysql> 
mysql> SHOW SLAVE STATUS\G
Empty set, 1 warning (0.00 sec)

mysql> CHANGE MASTER TO
    -> MASTER_HOST='10.206.0.13',
    -> MASTER_USER='replica',
    -> MASTER_PASSWORD='Shtc@2024',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=851;
Query OK, 0 rows affected, 8 warnings (0.02 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: 10.206.0.13
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 851
               Relay_Log_File: mysql-relay-bin.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: 851
              Relay_Log_Space: 536
              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: 65496a81-b75a-11ee-8979-5254005b393e
             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

mysql> show SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 10.206.0.13
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 851
               Relay_Log_File: mysql-relay-bin.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: 851
              Relay_Log_Space: 536
              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: 65496a81-b75a-11ee-8979-5254005b393e
             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)

mysql> 

在这里插入图片描述

文章来源:https://blog.csdn.net/sunyuhua_keyboard/article/details/135715613
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。