synchronization data from the master database mysql

1. The main installation mysql configuration from the server

# sudo apt-get install mysql-server
# sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: N
Please set the password for root here.(创建密码)

New password:

Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y(是否移除匿名用户)
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n(是否允许root远程登录)
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y(删除测试库)
 - Dropping test database...
Success.

 -ON the Test Database privileges ... Removing 
Success.

Reloading at The Privilege of Ensure that the Tables by Will All Changes 
Made by Will the Take Effect Immediately is SO FAR. 

Reload the Tables Privilege now (the y-Press | the Y-? For Yes, the any OTHER Key for No): the y-(immediately refresh the database configuration) 
Success. 

All DONE ! 

# sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
the bind-address = 0.0.0.0 (ip monitor modifications specified for their ip)

  # sudo mysql -u root -p

  . Mysql> grant all on * * to root @ '%' identified by 'your password' with grant option;. (* * Means that all database, you can also specify the database; root represents a user-created;% means that all hosts can access can also be specific ip; localhost indicates that only allow local access; password is to create a user's password)

  mysql> flush privileges; # refresh permission

  mysql> exit

  Then execute the exit command mysql service, execute the following command and then restart mysql:

  # sudo systemctl restart mysql

 

2. The master-slave configuration database

   If necessary a good idea to ensure that the main agreement from the database server time, you can use NTP service, slave database servers automatically synchronize master database server time

(1) Configure the master database 
login database
MySQL> Grant Slave Replication to ON * *. ' Sync_user ' @ ' 10.100.0%. ' IDENTIFIED by ' 123123 ' ; (sync_user establish an account on the host and empowering the slave, for connecting master database)
MySQL> flush privileges;
MySQL> flush the Tables with the Read lock; # lock the library to prevent anyone modifying the configuration database when the master database from
mysql> exit;
Configure MySQL 
# sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
  
  
  server- id =1                        # 数据库唯一ID,主从的标识号绝对不能重复。
  log-bin=/var/log/mysql/mysql-bin.log      # 开启bin-log,并指定文件目录和文件名前缀
  binlog- do -db= xxx                 # 需要同步xxx数据库。如果是多个同步库,就以此格式另写几行即可。如果不指明对某个具体库同步,就去掉此行,表示同步所有库(除了ignore忽略的库)。
  binlog-ignore-db=mysql             # 不同步mysql系统数据库。如果是多个不同步库,就以此格式另写几行;也可以在一行,中间逗号隔开。   replicate-do-db和replicate-ignore-db为互斥选项,只需要一个即可
  sync_binlog = 1                   # 确保binlog日志写入后与硬盘同步
  binlog_checksum = none            # 跳过现有的采用checksum的事件,mysql5.6.5以后的版本中binlog_checksum=crc32,而低版本都是binlog_checksum=none
  binlog_format = mixed             # bin-log日志文件格式,设置为MIXED可以防止主键重复。
 

  •   温馨提示:在主服务器上最重要的二进制日志设置是sync_binlog,这使得mysql在每次提交事务的时候把二进制日志的内容同步到磁盘上,即使服务器崩溃也会把事件写入日志中。
    sync_binlog 这个参数是对于MySQL系统来说是至关重要的,他不仅影响到Binlog对MySQL所带来的性能损耗,而且还影响到MySQL中数据的完整性。对于 "sync_binlog" 参数的各种设置的说明如下:
    sync_binlog=0,当事务提交之后,MySQL不做fsync之类的磁盘同步指令刷新binlog_cache中的信息到磁盘,而让Filesystem自行决定什么时候来做同步,或者cache满了之后才同步到磁盘。
    sync_binlog=n,当每进行n次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。
  •  In MySQL, the default setting is sync_binlog = 0, that is, without any mandatory disks refresh command, this time the performance is the best, but the risk is greatest. Because once the system Crash, all binlog information will be lost in the binlog_cache. When set to "1" when, but the performance loss is the safest maximum setting. Because when set to 1, even if the system Crash, also lost up to a transaction binlog_cache unfinished, without any substantial effect on the actual data.
  • For a system of concurrent transactions is high, "sync_binlog" is set to 0 and set to 1, the system write performance gap may be as high as 5 times or more


Restart the database service 
# systemctl restart mysql.service

MySQL> Show Master Status \ G
  
  • After performing this step not to operate the main server MySQL, to prevent the primary server state value change condition occurs
Back up master 
# sudo mysqldump -uroot -p -A --events> backup.sql
copied to the slave database waits introduced
# sudo scp backup.sql root # 10.100.0.147: / tmp /
(2) Configuration Database slave 
# sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
need to modify at least a server-id = xx (xx is a unique value)

温馨提示:
slave-skip-errors = all     
# 跳过所有的错误,继续执行复制操作
replicate-do-db=liting     # 需要同步的数据库名。如果不指明同步哪些库,就去掉这行,表示所有库的同步(除了ignore忽略的库)
replicate-ignore-db=mysql   # 不同步test数据库
  当只针对某些库的某张表进行同步时,如下,只同步liting库的haha表和test库的heihei表:
  replicate- do -db = liting
  replicate-wild- do -table = liting.haha        // 当只同步几个或少数表时,可以这样设置。注意这要跟上面的库指定配合使用;
  replicate- do -db = test
  replicate-wild- do -table = test.heihei       // 如果同步的库的表比较多时,就不能这样一一指定了,就把这个选项配置去掉,直接根据指定的库进行同步。


Restarting the database service
# sudo systemctl restart mysql.service
introduced master database backup
# sudo mysql </tmp/backup.sql

arranged DB master
MySQL> STOP Slave;
MySQL> Change master to MASTER_HOST .100.0.249 = '10 ', MASTER_PORT = 3306, MASTER_USER = 'sync_user with', master_password = '123123', MASTER_LOG_FILE = 'MySQL-bin.000001', MASTER_LOG_POS = 154;
MySQL> Slave Start;
MySQL> Show Slave Status \ G
  
  • Slave_IO_Running and Slave_SQL_Running are yes, it means that the synchronization is successful
(3) landing master database, unlock the table 
mysql> unlock tables;

 3. Test and synchronization effect

Log master database: 
the Create Database the Test; 
MySQL > the Create the Table people (
     -> user_id int unsigned AUTO_INCREMENT,
     -> user_name VARCHAR ( 25 ) the NOT NULL,
     -> Age int ,
     -> Primary Key (user_id)
     ->); 
MySQL> INSERT into people (user_name, age) values ( 'john', 20);

whether the landing inspection data from the database to be synchronized:
  

  

  Data has been written, synchronized properly.

 


  If in the process of synchronization, which occurs in the main synchronization failure being given a statement, the following statements can not synchronize a success . For example, there is a master data library, from the library and this one is no data, however, in the main library to perform the update operation, there is no such data from a database, thus the error. At this point the data from the database synchronization has failed, so the back of the synchronized statement can not proceed.

  # 在从数据库中,使用SET全局sql_slave_skip_counter来跳过事件,跳过这一个错误,然后执行从下一个事件组开始

  mysql> stop slave;

  mysql> set global sql_slave_skip_counter=1;

  mysql> start slave;

 

Guess you like

Origin www.cnblogs.com/alamisu/p/11840749.html