Linux install mysql 5.7 to achieve master-slave replication

1. MySQL (5.7) installation

tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  -C /usr/src/
ln -s /usr/src/mysql-5.7.22-linux-glibc2.12-x86_64/ /usr/local/mysql
echo "export PATH=$PATH:/usr/local/mysql/bin/">> /etc/profile
source /etc/profile
echo $PATH

Insert picture description here

useradd -M -s /sbin/nologin mysql
mysqld --user=mysql --initialize --datadir=/usr/local/mysql/data

Insert picture description here
Remember the red place, that is the password after initial encryption. Will be used later.

cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
 /etc/init.d/mysqld start

Can not start up, modify the configuration file

vim /etc/my.cnf

Delete and re-add the following content:
######################################### ###

[client]
port = 3306
socket = /tmp/mysql.sock
 
[mysqld]
server-id = 2
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data

#################################################

 /etc/init.d/mysqld start
mysql -uroot -p"waC-5a<pyftY"
mysql> set password=password('123123');    // 修改密码
exit
mysql -uroot -p123123

2. The same goes for the slave, configure master-slave replication after installation

Note that the server-id in the configuration file cannot be the same

Master host:

`Vim /etc/my.cnf insert a piece of code here
Add a piece
################################# ####################################################################################################################################################################################################################################################### ##########

[mysqld]
server-id = 2
port = 3306
log-bin=/usr/local/mysql/data/bin-log
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data

##############################################

mysql -uroot -p123123
mysql> grant replication slave on *.* to 'repl'@'192.168.9.10' 
-> identified by '123123';
show master status\G;

Insert picture description here

Slave slave:

mysql -uroot -p123123
mysql> change master to master_host='192.168.9.9',
    -> master_user='repl',
    -> master_password='123123',
    -> master_log_file='bin-log.000001',
-> master_log_pos=448;

The content of the last two lines is the corresponding content in the master picture.
show slave status\G;View

Insert picture description here
It must be yes, if not, please turn off the firewall.

2. Based on GTID master-slave replication
Master host:

mysql -uroot -p123123
mysql> show variables like "gtid_mode";

Insert picture description here

set global gtid_mode=1;
mysql> show variables like "gtid_mode";

Insert picture description here

set global gtid_mode=2;
mysql> show variables like "gtid_mode";

Insert picture description here

set global gtid_mode=3;

Insert picture description here

mysql> show variables like "enforce_gtid_consistency";

Insert picture description here

mysql> set global enforce_gtid_consistency=1;
mysql> show variables like "enforce_gtid_consistency";

Insert picture description here

set global gtid_mode=3;
show variables like"gtid_mode";

Insert picture description here
Slave from the library:

Open gtid, same as above

mysql> set  global gtid_mode=1;
Query OK, 0 rows affected (0.01 sec)

mysql> set  global gtid_mode=2;
Query OK, 0 rows affected (0.03 sec)

mysql> set  global enforce_gtid_consistency=on;
Query OK, 0 rows affected (0.00 sec)

mysql> set  global gtid_mode=3;
Query OK, 0 rows affected (0.02 sec)

mysql> show variables like "gtid_mode";

Insert picture description here

mysql> change master to master_host='192.168.9.9',
    -> master_user='repl',
    -> master_password='123123',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.04 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;

Insert picture description here
Insert picture description here
Finally, test it.
You can build or delete a library.

show slave status\G;

Insert picture description here
Note: gtid_mode will become off after restarting the database, so you can modify the configuration file /etc/my.cnf.
Insert picture description here

 vim /etc/my.cnf

1 [client]
  2 port = 3306
  3 socket = /tmp/mysql.sock
  4 
  5 [mysqld]
  6 server-id = 2
  7 port = 3306
  8 gtid_mode=ON
  9 enforce_gtid_consistency=ON                                         
 10 log-bin=/usr/local/mysql/data/bin-log
 11 basedir = /usr/local/mysql
 12 datadir = /usr/local/mysql/data

Insert picture description here

[root@mastre ~]# /etc/init.d/mysqld restart
Shutting down MySQL............ SUCCESS! 
Starting MySQL. SUCCESS! 

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39109226/article/details/111185277