[Windows] Mysql master-slave system library configuration to achieve synchronization from the primary data table

[Windows] Mysql master-slave system library configuration to achieve synchronization from the primary data table

  • In order to alleviate the large number of concurrent access, in addition to achieving load balancing in distributed sites. At the database level, a single server mysql certainly can not meet the reading and writing large amounts of data. So we need to build mysql cluster. A primary (master) library from the plurality of mounted libraries (Slave), the pressure data to a plurality of shunt connection from the library, to share the main library.

  • Mysql using master-slave configuration, separate read and write, data synchronization. Main library is responsible for writing data, is responsible for querying data from the database.

  • Preparation (Keep reading, I think you have the windows system and install mysql sql some basic reading and writing skills):

    1. Set up a master master database server (win7 system, Ip: 192.168.1.150), install mysql on the server (my version is mysql-5.5.56).

    2. Set up a slave server from the library (win10 system, Ip: 192.168.1.168), install mysql on the server (my version is mysql-5.5.56).

  • Main principles of synchronous data from the library:

    Mysql there is a log called a bin log (binary log). Under sql statements modify the database of all the log records. In fact, the main principle is to be copied from the copy bin logs on the master server from the server to execute once, so that the data from the server on the primary server and the data synchronized.

    Continued gossip less, cut to the chase. Began to configure the master from the library.

1. Main Library Configuration

  • In the win7 system, Ip: mysql-based configuration database (master) server on 192.168.1.150.

    1) the user name of my main library: the root, password: 12345, the following code is provided to the main library remotely connect main library from the library, and can be read by the binary log account data synchronization. 192.168.1.% Is remote access to the main library from the library IP address. % Is a wildcard meaning 192.168.1.0-192.168.1.255 server can log in as root user to the main server. Of course, you can also specify a fixed IP. However, in order to facilitate increased later from the library, I now use wildcards, you can according to the actual situation, to be configured.

    	mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY '12345';
    

    (PS: If you do not want to use the command line, you can use graphical tools such as Navicat assign permissions)

    2) modify the configuration files in the installation path my.ini mysql. (The default path is: C: \ Program Files \ MySQL \ MySQL Server 5.5).

    Add the following lines of code in the my.ini file:

    #给数据库服务的唯一标识,一般为大家设置服务器Ip的末尾号
    server-id = 150
    log-bin = master-bin
    log-bin-index = master-bin.index
    

    3) Check the main library log name, you can get the following sql.

    mysql> show master status;
    

    Query results are as shown below, master-bin.000004 for my main library bin log file, 2580 is log_position. Configuration connecting the main bank from the bank requires the above two values.
    Here Insert Picture Description

    So far, the main library master the basic configuration has been completed, restart the mysql service, configuration is complete. Next, we continue to configure from the library.

2. From the Library Configuration

  • Mysql 192.168.1.168 arranged on the server from the library (slave): in win10 system, Ip.

    1) modify the configuration file in the installation path my.ini mysql. (The default path is: C: \ Program Files \ MySQL \ MySQL Server 5.5).

    Add the following lines of code in the my.ini file:

    #给数据库服务的唯一标识,一般为大家设置服务器Ip的末尾号
    server-id=168
    relay-log-index=slave-relay-bin.index
    relay-log=slave-relay-bin
    

    Mysql restart the service, to validate the configuration. (Not just restart the server)

    2) Run the following sql in the code from the library, the connecting main library from the library, and may be real-time synchronization data.

    change master to master_host='192.168.1.150',
    	master_port=3306,
    	master_user='root',
    	master_password='12345',
    	master_log_file='master-bin.000004',
    	master_log_pos=2580;
    

3) start the slave, the following code:

	start slave;

So far, the library is also configured. If you want to configure multiple from the library, configured with the same configuration and appeal.

now. Owners have been configured from the library. Nazan Next, you can verify real-time synchronization of data.

3. Create a database in the main library and build a table. Specific code as follows:

create table if not exists `table1` (
    `t_id` int auto_increment,
     `t_username` varchar(64) not null,
     primary key(`t_id`)
)engine=innodb default charset=utf8;

Inserted into the data in a table, as follows:

insert into `table1` (`t_username`) values('fubo'); 

4. Verify whether the synchronization data from the library.

1) Check whether to create the main library from the library's database specific code as follows:

show databases;

2) switching demo1 database to see if the sync master database tables and data. code show as below:

use demo1;
select * from `table1`;

Stop service from a database backup, and reset

stop slave;
reset slave all;

Reprinted link

Author: fubo1990
Source: CSDN
Original: https://blog.csdn.net/fubo1990/article/details/82788149
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/cai454692590/article/details/90694471