MySQL database master-slave synchronization installation and configuration summary

  MySQL master-slave synchronization is a very mature infrastructure, the advantages are:

  ① the server can execute the query from work (that is, we often say that the reading function), reducing the main server stress;

  ② In the backups from the primary server to avoid affecting the primary server during backup services;

  ③ When the primary server is a problem, can be switched from the server.

  So I often use this program in project deployment and implementation.

  +? Database catalogs and other

  my.cnf configuration file?? /etc/my.cnf

  mysql database location?? datadir = / var / lib / mysql

  Master database: 192.168.2.119

  From the database: 192.168.2.220

   OS : RHEL5.x 32 bit

  Server type: Virtual Machine

   + Mysql5.0.77 installation:

  After ① configured linux service of yum, yum -y install mysql can directly

  Attachment: mounting php \ mysql a command to install: yum -y install httpd php mysql mysql-server php-mysql

  ② start MySQL

  service mysqld start(restart|stop)

   First, set the main library

  1, the my.cnf modify the primary database, mainly to set a different id and logbin (# this to follow on the specific environment, the pressure of the employed huge.cnf)

  [root@localhost etc]#vi /etc/my.cnf

  # Remember this part must be configured in the [mysqld] behind, or can not be found from the node, the meaning of each configuration item can be found in the document himself

[mysqld]

log-bin=mysql-bin

server-id=1

binlog-ignore-db=information_schema

binlog-ignore-db=cluster

binlog-ignore-db=mysql

  2、启动主库生效

  [root@localhost etc]service mysqld restart

  3、登陆主库

  [root@localhost etc]mysql -u root -p

  4、赋予从库权限帐号,允许用户在主库上读取日志

  mysql> grant all privileges on *.* to '用户名'@'%' identified by '密码';

  5、检查创建是否成功

  select user,host from mysql.user;

  6、锁主库表

  mysql> flush tables with read lock; 7、显示主库信息

  记录File和Position,从库设置将会用到

mysql> show master status;

+------------------+----------+--------------+------------------+

| File ? ? ? ? ? ? | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 98 ? | ? ? ? ? ? ? ?| ? ? ? ? ? ? ? ? ?|

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

# 说明,如果执行这个步骤始终为Empty set(0.00 sec),那说明前面的my.cnf没配置对。

  8、另开一个终端登陆220,打包主库迁移数据(如果你使用的yum安装,有默认数据库并未做任何改动,则不需要进行拷贝)

  目的是为了保证两台服务器的mysql数据库一致,这里可以自行tar打包或者使用mysqldump命令备份恢复的方式进行。

   二、设置从库

  1、传输拿到主库包、解包

  # 登陆从库

  从上一步中备份的数据库恢复到220服务器节点上。

  2、在119节点上解锁主库表(对应第一点设置主库中第6步锁主库表的操作)

  mysql> unlock tables;

  3、在220节点上修改从库my.cnf(位置一样)

  # 记住这部分一定要配置在[mysqld]后面,否则无法找到从节点,各个配置项的含义可自己查阅文档

[mysqld]

log-bin=mysql-bin

server-id=2

binlog-ignore-db=information_schema

binlog-ignore-db=cluster

binlog-ignore-db=mysql

replicate-do-db=test

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

slave-net-timeout=60

master-host=192.168.2.119

master-user=root

master-password=pfingo

  4、在220节点上验证连接主库

  [root@localhost etc]mysql -h 192.168.2.119 -u 用户名 -p

  5、在220节点从库上设置同步

#设置连接MASTER MASTER_LOG_FILE为主库的File,MASTER_LOG_POS为主库的Position

#注意下面第二条命令语句中的master_log_file='mysql-bin.000001', master_log_pos=98;对应为前面在主库中执行的show master status;结果

mysql> slave stop;

mysql> change master to master_host='192.168.2.119',master_user='root',master_password='pfingo',master_log_file='mysql-bin.000001', master_log_pos=98;

mysql> slave start;

  6、启动从库服务

  mysql> slave start;

  7、进行测试

  在主库上的test库上建立名为myTest的表

mysql> CREATE TABLE `myTest` (

`id` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT ,

`username` VARCHAR( 20 ) NOT NULL ,

`password` CHAR( 32 ) NOT NULL ,

`last_update` DATETIME NOT NULL ,

`number` FLOAT( 10 ) NOT NULL ,

`content` TEXT NOT NULL ,

PRIMARY KEY ( `id` )

) ENGINE=MYISAM ;

  在从表中马上看到了效果,主从同步成功了;

  为了更进一步验证在从库上输入show slave status\G;

  mysql> show slave status\G;

  Slave_IO_Running: Yes(网络正常);

  Slave_SQL_Running: Yes(表结构正常)

  进一步验证了以上过程的正确性。



最新内容请见作者的GitHub页:http://qaseven.github.io/


Guess you like

Origin www.cnblogs.com/mynale/p/11018681.html