Kubesphere installs mysql cluster

Table of contents

foreword

1. Preparation

2. Create a service

1. Create a storage volume

 2. Create a key

3. Create configuration (main mysql)

4. Create configuration (from mysql)

5. Create a service

3. Master-slave synchronization

1. Master to slave authorized account

 2. Set up the main library connection

 4. Verify master-slave synchronization



foreword

Use kubesphere to deploy k8s mysql cluster.


1. Preparation

        You need to create enterprise spaces, projects, accounts, and roles. For details, refer to Create enterprise spaces, projects, accounts, and roles to learn how to use the multi-tenancy function in KubeSphere to perform fine-grained access control at different levels. icon-default.png?t=L892https://v3-1.docs.kubesphere.io/zh/docs/quick-start/create-workspace-and-project/         is very simple and just follow the steps.

 

2. Create a service

1. Create a storage volume

  •  First create a storage to store mysql data, select storage management on the left-storage volume-create

  •  Fill in the name (optional) Click Next

  •  Just follow the default, select the capacity (I choose 10G here) and click Next

  •  Click directly to create, after the creation is complete, as shown in the figure:

 2. Create a key

  • Select Configuration Center-Key-Create in the left navigation bar, fill in the basic information, and click Next

  •  key setting

 Note: The name of the key is the environment variable name of the default password that needs to be set when mysql starts, which is the same as the -e parameter when docker starts. The value is the password you want to set, then click ✔ in the lower right corner and click Create. 

  • Created successfully, click on the key and see that the password will be encrypted.

 

3. Create configuration (main mysql)

  • In the left navigation bar, select Configuration Center-Configuration-Create, fill in the basic information, and click Next

  •  Fill out configuration settings

 

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
secure_file_priv=/var/lib/mysql

server_id=1
log-bin=mysql-bin
read-only=0
binlog-do-db=mymall
binlog-do-db=wallpaper


replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

 

Configuration: This configuration is the configuration of the main mysql. In the configuration, server_id=1 and read-only=0 of the main mysql can be read and written.  

binlog-do-db=mymall, binlog-do-db=wallpaper is the library name that needs master-slave synchronization

replicate-ignore-db=. . . That is, the name of the library that does not require master-slave synchronization

Note: If you are using mysql version 8.0 or above, you need to add the configuration secure_file_priv=/var/lib/mysql to the configuration file, otherwise, mysql will report an error and fail to start.

 

  



4. Create configuration (from mysql)

  • Create a configuration file, the steps are the same as before, post the file here
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
secure_file_priv=/var/lib/mysql

server_id=2
log-bin=mysql-bin
read-only=1
binlog-do-db=mymall
binlog-do-db=wallpaper

replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

Note: The main difference is that server_id=2; read-only=1 is read-only from the library.

  • Create a slave data volume

The steps are the same as above to create a data volume.

5. Create a service

The steps to create master mysql and slave mysql are the same, the difference is to select their respective configuration files and data volumes.

  • Select Application Load-Service-Create in the left navigation bar

  •  Fill in the basic information and click Next

  •  Container image settings, number of copies: 1 container image mysql:8.0.26; click to use the default port

 Configure CPU memory usage

 Check the environment variable and select the previously created key

  •  mount storage

Mount configuration file or key

 Select the configuration file, and select the mount mode as read-write, and the mount path as /etc/mysql

Note: The mount mode selected here can only be read-only or not mounted. You can enter through the edit mode in the upper right corner, and modify the readonly of the file to false 

 Then click the upper right corner again to close the edit mode.

Add storage volume

 

 Tip: Because mysql data is stored in the /var/lib/mysql directory, we mount the storage volume to this location. In fact, it is equivalent to the mount in docker.

  •  advanced settings,

Enable session persistence (optional)

  • create

 

Remember the DNS on the left will be used later.

3. Master-slave synchronization

1. Master to slave authorized account

  •   connect to mysql

 

 Login to MySQL

mysql -uroot -ppassword

password is your password, which is the password set when configuring the key 

If you can't log in to mysql8.0, please read this article.  Mysql8.0.13 login reports 1045 (28000) error_fen_fen's column-CSDN blog

  • Set up a user to sync with

mysql 5.x

#mysql 5.x
GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456'


 mysql 8.x

#mysql 8.x
CREATE USER backup IDENTIFIED BY '123456';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'backup'@'%';
GRANT ALL PRIVILEGES ON *.* TO 'backup'@'%' ;
FLUSH PRIVILEGES;

 If it is mysql 8.x, continue to perform the following operations

  select host,user,plugin,authentication_string from mysql.user;

update user password 

#If host is localhost use:
ALTER USER 'backup'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

#If host is % use:
ALTER USER 'backup'@'%' IDENTIFIED WITH mysql_native_password BY '123456';


# Refresh permissions
FLUSH PRIVILEGES;

View the status of the master

show master status;

 

Remember the name of the queried File, mysql-bin.000002 

 2. Set up the main library connection

  • Log in to mysql from the library

  •  Set up master-slave connection
# 设置主从链接
change master to master_host='mysql-master.mall-swarm-project',master_user='backup',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=0,master_port=3306;

master_host: the DNS address of the master mysql

master_user, master_password: the username and password set in the previous step,

master_log_file: File name queried in the previous step

 

  •  Start synchronization from the library
start slave;
  • View slave status 
show slave status\G;

 

2 yeses in the red box means the master-slave synchronization is successful. 

 Tip: If you want to cancel the master-slave synchronization, you need to execute, stop slave; reset slave all;

            Then execute show slave status\G again; if the result is empty set, the release is successful.

 4. Verify master-slave synchronization

Create a database in the main mysql, and check whether the database is synchronized from the mysql

Guess you like

Origin blog.csdn.net/qq_31277409/article/details/120405338