MySQL installation

Delete the mysql that was previously installed

Check whether mysql is installed

rpm -qa | grep mysql
Copy the code



The mysql server has been installed and needs to be deleted

Mysql > alter database

#The suffix file name depends on the local situationRPM -e --nodeps --allmatches mysql-community-common-5.7.32-1.el7.x86_64 RPM -e --nodeps --allmatches Mysql -community-server-5.7.32-1.el7.x86_64 RPM -e --nodeps --allmatches mysql-community-libs-5.7.32-1.el7.x86_64 RPM -e --nodeps --allmatches mysql-community-libs-compat-5.7.32-1.el7.x86_64 RPM -e --nodeps --allmatches Mysql -mmm-agent-2.2.1-15.el7.noarch RPM -e --nodeps --allmatches mysql-mmm-agent-2.2.1-15.el7.noarch RPM -e --nodeps --allmatches mysql57-community-release-el7-8.noarch rpm -e --nodeps --allmatches Mysql - community - the client - 5.7.32-1. El7. X86_64Copy the code
RPM command -e to delete the specified package, uninstall the RPM package --nodeps does not verify the correlation of the package file, forcibly delete --allmatches deletes the files that match the specified package, and deletes all packages with the same nameCopy the code

Verify again if uninstall hurry



To completely delete mysql, run the rm -rf command to delete all files or folders related to mysql

Query all mysql related folders

find / -name mysql
Copy the code



Delete related directories and folders

#The suffix file name depends on the local situation
rm -rf /usr/share/mysql/ /var/lib/mysql/ /var/lib/mysql/mysql/ /usr/lib64/perl5/vendor_perl/DBD/mysql /usr/lib64/perl5/vendor_perl/auto/DBD/mysql/ /etc/selinux/targeted/active/modules/100/mysql/
Copy the code

Verify that the deletion is complete

Check whether the mysql user group and user exist. If not, create the mysql user group

#Query whether the mysql user group exists
cat /etc/group | grep mysql
#Query whether the mysql user exists
cat /etc/passwd |grep mysql
Copy the code

#Add a mysql user group
groupadd mysql
#Add mysql user
useradd -r -g mysql mysql
Copy the code

Create a user useradd [options] User name note: -g Specifies the group to which the user belongs. Delete the user userdel [options] LOGIN Note: -f,--force Forcibly deletes the user, regardless of whether the user is in use. -h,--help Help information Note: -r,--remove Deletes users and groups and deletes the user's home directory and mail notification directory note: Delete a user without parameters userdel Username Delete only the user, password, and user group, but not the user's home and mail directory. Create a user group groupadd Group name Delete a user group groupdel Group name Change a user group name change a user group name: Groupmod -h New group name Old group name Change user group ID: groupmod -g New group ID Old group IDCopy the code

Install mysql(version 5.7)

Download mysql yum source

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
Copy the code



Install the mysql yum source locally

yum localinstall mysql57-community-release-el7-8.noarch.rpm
Copy the code
<! -- yum localinstall -- yum localinstall -- yum localinstallCopy the code

Check whether the mysql yum source is available

yum repolist enabled | grep "mysql.*-community.*"
Copy the code
# yum repolist all # yum repolist enabled # yum repolist disabled # Yum repolist disabled #Copy the code

Installing the mysql service

yum install -y mysql-community-server
Copy the code
Mysql currently comes in two versions, commercial and community. Mysql-community-server is the community version, which is available for free, while the commercial version is available for a feeCopy the code

Start the mysql

systemctl start mysqld
Copy the code

If any of the following problems occur, perform the following operations (If yes, ignore them) : Step 1: Run the journalctl -xe command

journalctl -xe
Copy the code

Could not open file ‘/var/log/mysqld.log’ for error logging: Permission denied Step 2: Modify the owner and group of the file

chown -R mysql:mysql /var/log/mysqld.log
Copy the code

Step 3: View the mysql configuration file my. CNF and obtain the data directory

tail /etc/my.cnf
Copy the code

Check whether data exists in the data directory in the mysql database. If yes, clear the directory

cd /var/lib/mysql
Copy the code

Step 3: Clear the mysql Data directory

rm -rf *
Copy the code



Step 4: Start the mysql database again

systemctl start mysqld
Copy the code

Set the mysql service to start automatically upon startup

systemctl enable mysqld
Copy the code

Reload the configuration file for the service

systemctl daemon-reload
Copy the code

If a new service is installed and belongs to systemctl, load the service program configuration file of the new service to check whether mysql is successfully started

ps -ef|grep mysql
Copy the code

The startup is successful. Obtain the default password of mysql

grep password /var/log/mysqld.log
Copy the code

Use the default password to log in to mysql

Mysql -uroot -pCopy the code

Login successful Changing the MySQL password

 ALTER USER 'root'@'localhost' IDENTIFIED BY 'kaikeba100';
Copy the code

After you log out, use the new password to log in again

The dual-master replication architecture is set up

Creating a replication user

For Master01 and Master02, create user replication

create user replication identified by "kaikeba100";

grant all privileges on *.* to replication@'%' identified by 'kaikeba100';

flush privileges;
Copy the code

Enable mysql bin_log

Master01 Host: Enable mysql bin_log

#Edit the mysql configuration file
vim /etc/my.cnf
#Open the log_bin annotation and identify the server
Copy the code

systemctl restart mysqld
Copy the code

Master02: Enable mysql bin_log The log_bin comment is opened, the server-id is set to 2, and the MySQL service is restarted

Mysql backup

Master01 machine: mysql backup

mysqldump --single-transaction --master-data=2 --all-databases -uroot -pkaikeba100 > dump1.sql;
Copy the code
Mysqldump command --all-databases: exports all databases. --master-data: writes the binlog file in the backup file. Incremental data is restored from the log after the backup file. If the value is 1, the binlog file name and location are uncommented. If the value is 2, the binlog file name and location are commented in the backup file. -- Single-transaction: suitable for backing up innoDB transaction databases. To ensure the consistency of backup, set the isolation level of this session to Repeatable read to ensure that the data submitted by other sessions will not be seen during this session (dump).Copy the code

For Master01: view the generated backup file and obtain MASTER_LOG_FILE and MASTER_LOG_POS

more dump1.sql
Copy the code

Master02 machine: Replicates the data of machine 1

change master to Master_host = '10.0.10.240, master_user =' replication ', master_password = 'kaikeba100', MASTER_LOG_FILE = 'rabbitmq001 - bin. 000001 ',MASTER_LOG_POS=154;Copy the code

Master02 Machine: Executes the master/slave replication command

start slave;
Copy the code
Note to stop slave, run the stop slave commandCopy the code

Master02 Machine: View the slave status

show slave status \G;
Copy the code

Master02 machine: mysql backup

mysqldump --single-transaction --master-data=2 --all-databases -uroot -pkaikeba100 > dump2.sql
Copy the code

For Master02: view the generated backup file and obtain MASTER_LOG_FILE and MASTER_LOG_POS

more dump2.sql
Copy the code

Master01 Machine: Replicates the data on machine 2

change master to Master_host = '10.0.10.241, master_user =' replication ', master_password = 'kaikeba100', MASTER_LOG_FILE = 'rabbitmq002 - bin. 000003 ',MASTER_LOG_POS=154;Copy the code

Master01 Machine: Run the master/slave replication command

start slave;
Copy the code

Master01 Machine: View the slave status

show slave status \G;
Copy the code

Testing capabilities

Master01 machine: Create a test library test The Test library automatically appears on the Master02 machine