mysql master-slave configuration under linux

Keywords: MySQL Database socket SQL

CentOS 6.5 192.168.1.84 host
CentOS 6.5 192.168.1.83 slave
1, Primary server configuration:
Create synchronization user and specify server address

[root@node04 ~]# mysql -u root -p111111
mysql>use mysql;
mysql>grant replication slave on *.* to 'tongbu'@'192.168.1.84' identified by '123456';
mysql>flush privileges #Refresh authority

The authorized user tongbu can only access the database on 192.168.1.84 from 192.168.1.83
Modify the / etc/my.cnf configuration file

[root@node04 ~]# vim /etc/my.cnf
//Content:
[mysqld]
   default-character-set=utf8
   datadir=/var/lib/mysql
   socket=/var/lib/mysql/mysql.sock
   user=mysql
   # Disabling symbolic-links is recommended to prevent assorted security risks
   symbolic-links=0
   lower_case_table_names = 1
   # Content added by master db config
   log-bin=MySQL-bin #Binary log file
   server-id=1 #Server ID 
   binlog-do-db=test01 #Databases to synchronize
   binlog-ignore_db=mysql #Asynchronous mysql system database

   [mysqld_safe]
   log-error=/var/log/mysqld.log
   pid-file=/var/run/mysqld/mysqld.pid
   [client]
   default-character-set=utf8
   socket=/var/lib/mysql/mysql.sock
[root@node04 ~]# /etc/init.d/mysqld restart

To view the master status of the master server:

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000002 |      106 | test01       | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

Create a test database for testing:

mysql> create database test;
mysql> use test;
mysql> create table test(id INT NOT NULL AUTO_INCREMENT, name varchar(15) NOT NULL, PRIMARY KEY (id))AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql>insert into test values(1, "aaa"),(2, "bbb"),(3,"ccc");
mysql> select *from test;
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | bbb  |
|  3 | ccc  |
+----+------+
3 rows in set (0.00 sec)

To back up the data of this test database, you need to lock it first

mysql> flush tables with read lock; #Database read only lock command unlock table
[root@node04 ~]# mysqldump -uroot -p111111 test > /root/test.sql #Export database structure and data
[root@node04 ~]# mysqldump -uroot -p111111 -ntd -R test > test_f.sql #

Send test.sql to the slave machine

2, Configure from server

[root@node03 ~]#vim /etc/my.cnf
[mysqld]
   default-character-set=utf8
   datadir=/var/lib/mysql
   socket=/var/lib/mysql/mysql.sock
   user=mysql
   # Disabling symbolic-links is recommended to prevent assorted security risks
   symbolic-links=0
   lower_case_table_names = 1
   #slave db config added content
   server-id=2 #Server ID, which must be different from the primary server
   log-bin=MySQL-bin #Binary log file
   binlog-do-db=test01 #Synchronized database
   binlog-ignore-db=mysql #Asynchronous mysql system database

   [mysqld_safe]
   log-error=/var/log/mysqld.log
   pid-file=/var/run/mysqld/mysqld.pid
   [client]
   default-character-set=utf8
   socket=/var/lib/mysql/mysql.sock
[root@node03 ~]# /etc/init.d/mysqld restart 

Import database:

[root@node04 ~]# mysqldump -uroot -p111111 test < test.sql

Configure master-slave synchronization:

[root@node03 ~]# mysql -u root -p111111
mysql> use mysql
mysql> stop slave;
mysql> change master to master_host='192.168.1.84',
master_user='tongbu',
master_password='123456',
master_log_file='MySQL-bin.000001',#Log file name from show master status
master_log_pos=106;
mysql> start slave;
mysql> show slave statue\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.84
                  Master_User: tongbu
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: MySQL-bin.000002
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysqld-relay-bin.000006
                Relay_Log_Pos: 251
        Relay_Master_Log_File: MySQL-bin.000002
             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: 106
              Relay_Log_Space: 407
              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)

Synchronization successful, test:
From server:

mysql> use test
mysql> select *from test;
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | bbb  |
|  3 | ccc  |
+----+------+
3 rows in set (0.00 sec)

Primary server:

mysql> insert into test values(11, "xxx");
mysql> select * from test;
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | bbb  |
|  3 | ccc  |
| 11 | xxx  |
+----+------+
4 rows in set (0.01 sec) 

View from server:

mysql> select *from test;
mysql> select * from test;
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | bbb  |
|  3 | ccc  |
| 11 | xxx  |
+----+------+
4 rows in set (0.01 sec) 

Success!!

Posted by smilinmonki666 on Thu, 02 Apr 2020 14:59:59 -0700