Chinaunix首页 | 论坛 | 博客
  • 博客访问: 318190
  • 博文数量: 84
  • 博客积分: 886
  • 博客等级: 准尉
  • 技术积分: 769
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-30 19:32
文章分类

全部博文(84)

文章存档

2018年(1)

2017年(2)

2014年(1)

2013年(50)

2012年(30)

我的朋友

分类: LINUX

2013-08-20 14:41:20

第一次配置mysql主从,遇到了许多问题,现在把正确的配置记录下来,请多指教。(回头买几本书恶补一下)

两台服务器分别为:192.168.1.11(Slave),192.168.1.13(Master)

虚拟机软件情况为:CentOS 6.4 ,mysql-5.5
mysql安装过程详见:http://blog.chinaunix.net/uid-26267891-id-3846967.html

(1)首先确认两台机器上的mysql版本,版本最好一致,mysql升级后,bin-log会有更改。
即使两台机器上的mysql版本不一致也需要Slave的版本高于Master,尽量做到一致吧!
(2)在Master上首先建立同步所用的帐号:


  1. mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.157.11' IDENTIFIED BY '111111';
  2. Query OK, 0 rows affected (0.13 sec)

  3. mysql> flush privileges


(3)修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启Mysql服务。

  1. [mysqld]
  2. log-bin=mysql-bin
  3. server-id=1
  4. binlog-do-db = test
  5. binlog-ignore-db=mysql

(4)之后可以得到主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复

  1. mysql> show master status\G;
  2. *************************** 1. row ***************************
  3. File: mysql-bin.000008
  4. Position: 338
  5. Binlog_Do_DB: test
  6. Binlog_Ignore_DB: mysql
  7. 1 row in set (0.00 sec)

(5)停止对主库的更新操作,并将主库的数据库文件导出一份,并且导入到从库中

  1. #首先添加一个读锁保证数据库的一致性
  2. mysql> flush tables with read lock;
  3. mysql> quit;
  4. mysqldump -uroot -p -P 3306 test > /usr/local/mysql/test.sql

  5. #然后将test.sql传到Slave机器上,并且导入。这里要求从库必须是启动的。
  6. mysql> -uroot -p --default-character-set=utf8 test < test.sql

  7. #最后恢复Master机器的读锁
  8. mysql> unlock tables

(6)修改从库的/etc/my.cnf,要求与主库的id不同,不然后面执行slave start会报错。

  1. server-id = 2

(7)配置从服务器

  1. mysql>change master to master_host='192.168.157.13',master_user='slave',master_password='111111',master_log_file='mysql-bin.000008',master_log_pos=338; //注意不要断开,“338”无单引号

  1. start slave;#启动slave进程

(8)执行show salve status验证主从配置是否生效

  1. mysql> show slave status\G;
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 192.168.157.13
  5.                   Master_User: slave
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 60
  8.               Master_Log_File: mysql-bin.000008
  9.           Read_Master_Log_Pos: 834
  10.                Relay_Log_File: lvstest-relay-bin.000004
  11.                 Relay_Log_Pos: 980
  12.         Relay_Master_Log_File: mysql-bin.000008
  13.              Slave_IO_Running: Yes
  14.             Slave_SQL_Running: Yes
  15.               Replicate_Do_DB:
  16.           Replicate_Ignore_DB:
  17.            Replicate_Do_Table:
  18.        Replicate_Ignore_Table:
  19.       Replicate_Wild_Do_Table:
  20.   Replicate_Wild_Ignore_Table:
  21.                    Last_Errno: 0
  22.                    Last_Error:
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 834
  25.               Relay_Log_Space: 1284
  26.               Until_Condition: None
  27.                Until_Log_File:
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File:
  31.            Master_SSL_CA_Path:
  32.               Master_SSL_Cert:
  33.             Master_SSL_Cipher:
  34.                Master_SSL_Key:
  35.         Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error:
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error:
  41.   Replicate_Ignore_Server_Ids:
  42.              Master_Server_Id: 1
  43. 1 row in set (0.00 sec)
主要看这两项:

  1. Slave_IO_Running: Yes
  2. Slave_SQL_Running: Yes
说明主从配置成功,然后在主库中建表插数据,测试成功!



阅读(1806) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~