The primary and secondary functions of mysql are functions provided by mysql itself. We only need to configure the database. The principle of mysql's master and backup is not very complicated, that is, the master database (master) will record every change of itself in the binary log Binarylog. From the database (Slave), the account on the master will be used to log in to the master, read the Binarylog of the master, write to its own relay log Relaylog, and then its own sql thread will be responsible for reading the relay log and execute it again . Then the changes on the master database (master) are synchronized to the slave database (slave).
The following records the configuration process (ubuntu14.04 mysql5.5):
1. Create a user backup account on the master library (master), and the slave library (slave) will use this account to read the binlog.
grant replication slave on *.* to 'repl'@'%' identified by 'password';
2. Open the binlog of the main library (master),
edit /etc/mysql/my.cnf, and add the following under the [mysqld] node:
server-id = 1 binlog_format=mixed log_bin = /var/log/mysql/mysql-bin binlog_do_db = sample auto-increment-increment= 2 auto-increment-offset = 1
The meaning of several items here:
server-id: number, as long as it is unique
log_bin: path and file name of binlog
binlog_do_db: which database to record binlog for, if there are multiple databases, this one can be configured multiple times
binlog-ignore- db : Which databases need to be ignored. I have not configured this item here. You can also configure this item to ignore individual databases, and the rest are recorded. Generally, it is enough to match the above binlog_do_db.
Auto-increment-increment: the increment value of the primary key
auto-increment- offset: The starting value of the primary key.
The function of the last two items is to prevent the primary key from being duplicated when there are multiple databases as the main database. For example, in the above configuration, the starting value of the primary key is 1 and the growth amount is 2. Then on this database, the primary keys are all odd numbers 1, 3, 5, 7, and 9. The other database will configure the starting value of the primary key. The value is 2, and the increment is 2, so that its primary keys are all even numbers: 2, 4, 6, 8. At this time, the primary key will not conflict when synchronizing data between them.
The main library is basically configured here, and then restart
service mysql restart
3. In order to keep the initial data consistent, you need
to lock the database before exporting the data of the main database.
FLUSH TABLES WITH READ LOCK;
then export
mysqldump --master-data -u root -p sample > sample.sql
unlock
unlock tables;
Configure the slave library below
4. Create a database and import data
create database sample default charset utf8; mysql -u root -p sample < sample.sql
5. Edit /etc/mysql/my.conf and add the following under the [mysqld] node:
server-id = 2 replicate-do-db = sample relay_log = /var/log/mysql/mysqld-relay-bin log-slave-updates = ON
If it is the same as the active-active standby configuration, the contents of relay_log and log_bin are required to be similar to the above
server-id: number
replicat-do-db: the library that needs to be synchronized
replicate-ignore-db: Ignored library
relay_log: The full path of relaylog
log-slave-update: After the relay log is executed, whether these changes need to be counted into its own binarylog. It needs to be opened when your B server needs to be the master server of another server. It is a dual-master backup, or a multi-master loop backup.
6. Enable synchronization
to execute on the main library:
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000055 | 1528312 | sample | | +------------------+----------+--------------+------------------+
Execute on the slave library:
CHANGE MASTER TO MASTER_HOST='192.***.***.***', MASTER_USER='repl', MASTER_PASSWORD=****', MASTER_LOG_FILE='mysql-bin.000055', MASTER_LOG_POS=1528312;
master_host: the ip of the main library
master_user: the user created on the main library at the beginning
master_password: the password of that user
master_log_file: the binlog file, which is the file displayed by executing show master status on the main library above
master_log_pos: the file location, which is also above displayed numbers.
7. Finally start the slave
start slave
8. Check the synchronization status
and execute "show slave status \G;" on the slave library. Generally speaking, Slave_IO_Running: Yes, Slave_SQL_Running: Yes, these two display yes is basically normal.
OK, it's over.
Reprint http://blog.csdn.net/redstarofsleep/article/details/53539226
http://www.cnblogs.com/kristain/articles/4142970.html