MySQL replication introduction and construction

http://blog.jobbole.com/100081/

1. First, create two mysql services locally (refer to here) and specify different ports. I have one master (3306) and one slave (3307).
2. Then modify the main configuration file:
[mysqld]
server-id = 1  
binlog-do-db=test #Database to be synchronized
#binlog-ignore-db=mysql #Unsynchronized database, if binlog-do-db is specified There should be no need to specify
log-bin=mysql-bin #The name of the binary log file to be generated

Modify the slave configuration file:
[mysqld]
server-id = 2
log-bin = mysql-bin
replicate-do-db=test
3, Add a user repl to the main library and specify replication permissions
create user 'repl'@'127.0.0.1' identified by 'asdf';

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'127.0.0.1'; -- -- Here I get an error when I specify the database (test.*), but it succeeds when I specify the full library (*.*).

4. Keep the initial state of the master-slave mysql test database consistent.

Generally, read locks are added to all tables first, and then copy the database folder on the disk. I stop the service directly here, and then copy the data file over.

5. Run show master status in the main database; note the parameters corresponding to the file and position fields.

mysql> show master status;
+------------------+----------+------------- -+-----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+ ----------+--------------+------------------+
| mysql-bin. 000001 | 107 | test | |
+-----------------+----------+------------ --+-----------------+
1 row in set (0.00 sec)

6. Set its master in the slave library:

mysql> change master to master_host='127.0. 0.1',master_port=3306,master_user='repl',master_password='asdf',master_log_file='mysql-bin.000001',master_log_pos=107;
Query OK, 0 rows affected (0.19 sec)

The master_log_file and master_log_pos here correspond to just now The parameters noted by show master status.

7. Enable the copy function from the database in the slave library.

slave start;

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

In the slave library, you can view some parameters by show slave status.

8. At this point, create a table or insert data in the main library, and it will soon be visible in the slave library.

-- master database

mysql> create table tianyc_02(b int);
Query OK, 0 rows affected (0.16 sec)

mysql> insert into tianyc_02 values(2013);
Query OK, 1 row affected (0.13 sec)

-- slave database

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tianyc_01 |
| tianyc_02 |
+- ---------------+
2 rows in set (0.00 sec)

mysql> select * from tianyc_02;
+------+
| b |
+------+
| 2013 |
+------+
1 row in set (0.00 sec)

Similarly, the second and third slave nodes can be built.



Reference: http://blog.sina.com.cn/s/blog_4d06da1f01010m55.html

Note: The serve_id of the two services must be different, otherwise the error

mysql> slave start will be prompted when the replication function is enabled;
ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO

I set the master node id here to 1 and the slave node id to 2. It can also be set according to ip or port, which is easy to distinguish. Refer here.



Note:

     * The database between the master and slave is not synchronized in real time. Even if the network connection is normal, there is a momentary inconsistency between master and slave data.
    * If the master-slave network is disconnected, the slaves will synchronize in batches after the network is normal.
    * If the data is modified on the slave, it is very likely that the slave will stop synchronization due to an error when executing the master's bin-log, which is a very dangerous operation. So in general, be very careful to modify the data from the above.

Reference: http://bbs.chinaunix.net/thread-2297959-1-1.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326653894&siteId=291194637