mysql主从复制 之 一主一从

Linux下配置Mysql主从复制
系统环境:CentOS 5.6
Mysql版本:5.1.58
拓扑图:mysql

这里是一主一从,要配置一主多从,其余从服务器把servier -id 修改成不一样的数字,其余的按照从服务器的配置就 OK。
1、安装mysqllinux

在主从服务器上安装mysql,最好是一样版本,若是主服务器版本高,从服务器版本低可能会出问题,若是主服务器版本低,从服务器版本高那却是没有问题,这里用的是5.1.58redis

看Mysql主从复制安装篇sql

http://linux5588.blog.51cto.com/65280/800139数据库

若是主从服务器型号同样,配置同样,主服务器安装完mysql以后,直接打包mysql安装目录,而后传到从服务器上,而后添加mysql用户,修改目录的权限,就能够启动从服务器的mysql服务。vim

2、配置主从复制服务器

1.设置主库(在主服务器上操做)
如下操做,若是没有指定在从服务器上操做的,都是在主服务器上操做ide

1)修改主库my.cnf,vim /usr/local/mysql5.1.58/my.cnf
在[mysqld]部分,添加以下语句ui

  
  
           
  
  
  1. server-id = 1      //主从库id不能重复  
  2. log-bin=binlog   //开启二进制日志文件  
  3. binlog-do-db=bookfm   //要同步的数据库名字 若是不指定这条那么是同步全部新建的数据库  
  4. character-set-server = utf8     //数据库字符集  
  5. replicate-ignore-db = mysql  //不进行同步的数据库  
  6. replicate-ignore-db = test     //不进行同步的数据库  
  7. replicate-ignore-db = information_schema     //不进行同步的数据库 
  
  
           
  
  
  1. 在[mysql]部分,找到 #no-auto-rehash,去掉no,这个功能就是按table键自动补全功能,只能补齐表,列名  
  2. [mysql]  
  3. #no-auto-rehash  
  4. auto-rehash  

2).赋予从库权限账号,容许在主库上读取日志this

  
  
           
  
  
  1. mysql>grant replication slave on *.* to 'admin'@'192.168.100.247' identified by '123456'; 

(在从服务器上操做)当即到从库的机器上登陆试试,看可否登陆上:

  
  
           
  
  
  1. [root@server2 ~]# mysql -uadmin -p -h 192.168.100.248      
  2. Enter password:  
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  4. Your MySQL connection id is 2  
  5. Server version: 5.1.58-log Source distribution  
  6.  
  7. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  8. This software comes with ABSOLUTELY NO WARRANTY. This is free software,  
  9. and you are welcome to modify and redistribute it under the GPL v2 license  
  10.  
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  12.  
  13. mysql>   
  14. 成功显示mysql>界面表示设置成功。  

3).检查用户是否建立成功

  
  
           
  
  
  1. mysql> use mysql;  
  2. Reading table information for completion of table and column names  
  3. You can turn off this feature to get a quicker startup with -A  
  4.  
  5. Database changed  
  6. mysql> select user,host,password from user;  
  7. +-------+-----------------+-------------------------------------------+  
  8. | user  | host            | password                                  |  
  9. +-------+-----------------+-------------------------------------------+  
  10. | root  | localhost       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |  
  11. | root  | server3.com     |                                           |  
  12. | root  | 127.0.0.1       |                                           |  
  13. |       | localhost       |                                           |  
  14. |       | server3.com     |                                           |  
  15. | admin | 192.168.100.247 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |  
  16. +-------+-----------------+-------------------------------------------+  
  17. 6 rows in set (0.00 sec)  
  18. 能够看到已经建立成功。 

4)建立bookfm数据库

  
  
           
  
  
  1. mysql> create database bookfm;  
  2. Query OK, 1 row affected (0.00 sec)  
  3.  
  4. mysql> show databases;  
  5. +--------------------+  
  6. | Database           |  
  7. +--------------------+  
  8. | information_schema |  
  9. | bookfm             |  
  10. | mysql              |  
  11. | test               |  
  12. +--------------------+  
  13. 4 rows in set (0.00 sec) 

5)锁主库表

  
  
           
  
  
  1. mysql> flush tables with read lock;  
  2. Query OK, 0 rows affected (0.01 sec) 

6)显示主库信息,记录File和Position,从库设置将会用到

7)将主库数据目录打包,发送给从库机器,这种方式适合于数据库刚安装时,数据库比较单一,若是数据库比较大可使用mysqldump的方式把数据导出为.sql文件,而后在从库上建立同名数据库,把数据导入

  
  
           
  
  
  1. [root@server3 mysql5.1.58]# tar zcf data.tar.gz data/  
  2. [root@server3 mysql5.1.58]# scp data.tar.gz root@192.168.100.247:/usr/local/mysql5.1.58/ 

2.设置从库(在从服务器上操做)
如下操做,若是没有指定在主服务器上操做的,都是在从服务器上操做

1)备份从库data目录,把从主库复制过来的data.tar.gz直接解压出来

  
  
           
  
  
  1. [root@server2 mysql5.1.58]# mv data data_bak  
  2. [root@server2 mysql5.1.58# tar zxf data.tar.gz   

2)解锁主库表(在主服务器上操做)

  
  
           
  
  
  1. mysql>unlock tables; 

3)启动从库mysql服务

  
  
           
  
  
  1. 编辑从库/usr/local/mysql5.1.58/my.cnf ,找到server-id把值修改成2   
  2. server-id = 2   
  3.  
  4. [root@server2 mysql5.1.58]# /usr/local/mysql5.1.58/bin/mysqld_safe --defaults-file=/usr/local/mysql5.1.58/my.cnf --user=mysql &  

4)在从库上设置同步

  
  
           
  
  
  1. 设置链接MASTER MASTER_LOG为主库的File,MASTER_LOG_POS为主库的Position  
  2. mysql> slave stop;  
  3. mysql> change master to master_host='192.168.100.248',master_user='admin',master_password='123456',master_log_file='binlog.000006',master_log_pos=278;  
  4. mysql> slave start;  
  5. mysql> show slave status\G;  
  6. *************************** 1. row ***************************  
  7.                Slave_IO_State: Waiting for master to send event  
  8.                   Master_Host: 192.168.100.248  
  9.                   Master_User: admin  
  10.                   Master_Port: 3306  
  11.                 Connect_Retry: 60  
  12.               Master_Log_File: binlog.000006  
  13.           Read_Master_Log_Pos: 278  
  14.                Relay_Log_File: server2-relay-bin.000002  
  15.                 Relay_Log_Pos: 248  
  16.         Relay_Master_Log_File: binlog.000006  
  17.              Slave_IO_Running: Yes  
  18.             Slave_SQL_Running: Yes        //这2项要Yes才行  
  19.               Replicate_Do_DB:  
  20.           Replicate_Ignore_DB:  
  21.            Replicate_Do_Table:  
  22.        Replicate_Ignore_Table:  
  23.       Replicate_Wild_Do_Table:  
  24.   Replicate_Wild_Ignore_Table:  
  25.                    Last_Errno: 0  
  26.                    Last_Error:  
  27.                  Skip_Counter: 0  
  28.           Exec_Master_Log_Pos: 278  
  29.               Relay_Log_Space: 405  
  30.               Until_Condition: None  
  31.                Until_Log_File:  
  32.                 Until_Log_Pos: 0  
  33.            Master_SSL_Allowed: No  
  34.            Master_SSL_CA_File:  
  35.            Master_SSL_CA_Path:  
  36.               Master_SSL_Cert:  
  37.             Master_SSL_Cipher:  
  38.                Master_SSL_Key:  
  39.         Seconds_Behind_Master: 0  
  40. Master_SSL_Verify_Server_Cert: No  
  41.                 Last_IO_Errno: 0  
  42.                 Last_IO_Error:  
  43.                Last_SQL_Errno: 0  
  44.                Last_SQL_Error:  
  45. 1 row in set (0.00 sec)  
  46.  
  47. ERROR:  
  48. No query specified 

5)在主库上的bookfm数据库上创建book表

  
  
           
  
  
  1. mysql> create table book (id int,name char(10)) engine=MYISAM;  
  2. mysql> insert into book values(1,'a');  

6)在从库上查询

  
  
           
  
  
  1. mysql> select * from book;  
  2. +------+------+  
  3. | id   | name |  
  4. +------+------+  
  5. |    1 | a    |  
  6. +------+------+  
  7. 1 row in set (0.00 sec)  
  8. 能够查询到,说明配置成功  
相关文章
相关标签/搜索