Mysql builds master-slave synchronization

1. 1. Experimental environment: two servers build master-slave services to achieve synchronization
2. Experimental topology:
master server HK63 (IP: 192.168.2.104) --------- slave server HK64 (IP: 192.168.2.105)
3. Experimental ideas:
(1), prepare two servers
(2), build a database on two servers (database version 5.1)
(3), create a database to be synchronized (MA)
(4), configure the main server main configuration file Open binary log, the slave server does not need to open, restart the service
(5), the master server authorizes the access rights of the slave server
(6), the slave server specifies the master server IP address, user name, password, port number
(7), and the master and slave servers are closed. selinux and iptables
(8), test

2. Setting up the environment
1. Related configuration of the main server HK63 database
(1), enter the server Hk63 database to create the database MA to be synchronized

mysql> show databases;
+--------------------+
| Database |
+--------------------+|
information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.10 sec)
mysql> create database MA;Query OK, 1 row affected (0.10 sec)mysql> show databases;
+--------------------+
| Database |
+--------------------+|
information_schema |
| MA |
| mysql |
| test |
+--------------------+4 rows in set (0.00 sec)
mysql> use MA;
Database changed
mysql> create table shuiguo(price int);
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+--------------+|
Tables_in_MA
|+--------------+|
shuiguo
|+--------------+
1 row in set (0.00 sec)

(2) Edit the server HK63 configuration file
[root@HK63 ~]# vim /etc/my.cnf and
add the following bold content

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
**log-bin=mysqllog  #启用二进制日志,默认存在/var/lib/mysql  下面
server-id=1       #本机数据库ID 标示。其中master_id必须为1到232之间的一个正整数值
binlog-do-db=MA   #可以被从服务器复制的库。二进制需要同步的数据库名
**
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

(3), restart the HK63 database

[root@HK63 ~]# service mysqld restart

(4), enter the database to authorize the access rights from the server

[root@HK63 ~]# mysql -uroot -p123456
mysql> grant replication slave on . to [email protected] identified by "123456";Query OK, 0 rows affected (0.00 sec)
View master data status information
mysql> show master status;
+-------+----------+--------------+--- ---------------+
|File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+------ ----+--------------+------------------+
|mysqllog.000001 | 258 | MA | |
+ -----------------+------------+----------------------------+------ ------------+
1 row in set (0.00 sec)
View binary file storage location:
[root@HK63 ~]# ls /var/lib/mysql/
ibdata1 ib_logfile1 mysql mysqllog.index testib_logfile0
MA mysqllog.000001 mysql.sock

2. Ensure the consistency of the relevant database before configuring the slave server database service:

[root@HK63 ~]# mysqldump -u root -p123456 -A >all.sql

[root@HK63 ~]# lsall.sql anaconda-ks.cfg install.log install.log.syslog
[root@HK63 ~]# scp all.sql 192.168.2.105:/root/
all.sql 100% 516KB 515.7KB/s 00:00
[root@HK64 ~]# mysql -u root -p < all.sql
[root@HK64 ~]# mysql -uroot -p123456

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| MA |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)

3. HK64 slave server related configuration
(1), test whether the connection to the main service database is successful

[root@HK64 ~]# mysql -u backup -h 192.168.2.104 -p123456

View the current login user
mysql> select user();
+----------------------+
| user() |
+--------- -------------+
| [email protected] |
+----------------------+
1 row in set ( 0.00 sec)
to view the database, can not see the MA, because the authorized user only runs the copy read MA permission does not have the permission to view
mysql> show databases;
+-------------------- +
| Database |
+--------------------+
| information_schema |
| test |
+----------------- ---+
2 rows in set (0.00 sec)

(2) Set the slave server mysql configuration
[root@HK64 ~]# vim /etc/my.cnf to
add the following parameters marked in red

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

#Write the following content in the configuration file
server-id=2 #The ID number of the slave server should not be the same as the master ID. If multiple slave servers are set, each slave server must have a unique server-id value, which must be the same as the master ID. The server and other slave servers are not the same. Server-id values ​​can be thought of as similar to IP addresses: these ID values ​​uniquely identify each server instance in a replicated server cluster.
master-host=192.168.2.104 #Specify the IP address of the master server
master-user=backup #Make a user name that can be synchronized on the master server
master-password=123456 #Password #####The following can not be written
#master-port = 3306 #Port used for synchronization
#master-connect-retry=60 #Breakpoint reconnection time
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
(3), restart the database service
[root@HK64 ~]# service mysqld restart
(4), check the status of the slave server

[root@HK64 ~]# mysql -uroot -p123456

mysql> show slave status\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.104
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqllog.000001
Read_Master_Log_Pos: 258
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 402
Relay_Master_Log_File: mysqllog.000001
Slave_IO_Running: Yes 可以看到这两个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: 258
Relay_Log_Space: 558
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: 1 row in set (0.00 sec)

Slave_IO_Running: A responsible for io communication with the host
Slave_SQL_Running: responsible for its own slave mysql process

4. Test:
(1) Log in to the main server HK63 to test insert data
[root@HK63 ~]# mysql -uroot -p123456

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| MA |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)

mysql> use MA

Database changed
mysql> show tables;
+--------------+
| Tables_in_MA |
+--------------+
| shuiguo |
+--------------+
1 row in set (0.00 sec)

mysql> insert into shuiguo values(10);
Query OK, 1 row affected (0.00 sec)

Log in to the slave server HK64 to see if the synchronization is successful

[root@HK64 ~]# mysql -uroot -p123456

mysql> use MA;

Database changed
mysql> select * from shuiguo;
+-------+
| price |
+-------+
| 10 |
+-------+
1 row in set (0.00 sec)

(2) Whether the data inserted from the server will be synchronized with the main server data, and test:

mysql> insert into shuiguo values(20);
Query OK, 1 row affected (0.00 sec)
mysql> select * from shuiguo;
+-------+
| price |
+-------+
| 10 |
| 20 |
+-------+

2 rows in set (0.00 sec)
[root@HK63 ~]# mysql -uroot -p123456
mysql> use MA
Database changed
mysql> show tables;+--------------+
| Tables_in_MA |
+--------------+
| shuiguo |
+--------------+
1 row in set (0.00 sec)
mysql> select * from shuiguo ;
+-------+
| price |
+-------+
| 10 |
+-------+
1 row in set (0.00 sec)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324943333&siteId=291194637