Chinaunix首页 | 论坛 | 博客
  • 博客访问: 134346
  • 博文数量: 25
  • 博客积分: 389
  • 博客等级: 一等列兵
  • 技术积分: 838
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-23 14:18
文章存档

2013年(20)

2012年(5)

分类: Mysql/postgreSQL

2013-03-09 13:21:57

MySQL主从复制是由三个进程完成的,分别为主服务器上的dump thread和从服务器上的IO thread和SQL thraed进程,IO thread向dump thread发起请求,然后dump thread将主MySQL服务器上的二进制日志发送给IO thread,然后IO thread将二进制日志保存至relay log文件中,然后有SQL thread去逐条(SQL thread是单线程的)执行relay log文件中的日志并保存在本地数据目录中,下面来演示一下简单实现过程:
拓补图:

配置过程:

主服务器和从服务器安装mysql
  1. #tar xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local/
  2. #useradd -r mysql
  3. 创建一个逻辑分区/dev/sda5
  4. #pvcreate /dev/sda5
  5. #vgcreate myvg /dev/sda5
  6. #lvcreate -L 10G -n mydata myvg
  7. #mke2fs -j -L MYDATA /dev/myvg/mydata
  8. #vim /etc/fstab
  9. 添加如下行:
  10. LABEL=MYDATA /data/mydata ext3 defaults 0 0
  11. #mkdir -pv /data/mydata
  12. #mount -a
  13. #chown -R mysql:mysql /data/
  14. #cd /usr/local/
  15. #ln -sv mysql-5.5.24-linux2.6-i686 mysql
  16. #cd mysql
  17. #chown -R mysql:mysql .
  18. #scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql
  19. 提供配置文件:
  20. #cp support-files/my-large.cnf /etc/my.cnf
  21. #vim /etc/my.cnf
  22. 添加如下行:
  23. datadir = /data/mydata
  24. 修改如下行:
  25. thread_concurrency = 2
  26. 为mysql提供sysv服务脚本:
  27. #cp support-files/mysql.server /etc/rc.d/init.d/mysqld
  28. #chkconfig --add mysqld
  29. #chkconfig mysqld on
  30. #chown -R root .
  31. #vim /etc/profile
  32. 添加如下行
  33. PATH=/usr/local/mysql/bin:$PATH
  34. #export PATH=/usr/local/mysql/bin:$PATH
  35. #service mysqld start
 配置主mysql服务器:
  1. 创建具有复制权限的用户:
  2. # mysql
  3. mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO repluser@'172.16.11.%' IDENTIFIED BY 'redhat';
  4. mysql> FLUSH PRIVILEGES;
  5. 查看二进制日志文件所在位置
  6. mysql> SHOW MASTER STATUS;
  7. +------------------+----------+--------------+------------------+
  8. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  9. +------------------+----------+--------------+------------------+
  10. | mysql-bin.000001 | 355 | | |
  11. +------------------+----------+--------------+------------------+
 配置从mysql服务器:
  1. 修改配置文件
  2. # vim /etc/my.cnf
  3. server-id = 21 //修改与主服务器不同就型//
  4. 关闭二进制日志
  5. #log-bin=mysql-bin
  6. 添加中继日志
  7. relay-log=mysql-relay //日志名称可以随便取//
  8. 重启生效
  9. # service mysqld restart
  10. 验证一下配置生效情况
  11. # mysql
  12. mysql> SHOW GLOBAL VARIABLES LIKE '%log%';
  13. log_bin | OFF //确保二进制文件以关闭//
  14. relay_log | mysql-relay //确保中继日志已启动//
  15. mysql> SHOW GLOBAL VARIABLES LIKE 'server_id';
  16. +---------------+-------+
  17. | Variable_name | Value |
  18. +---------------+-------+
  19. | server_id | 21 |
  20. +---------------+-------+
  21. 启动从服务,并指定主服务器参数
  22. mysql> CHANGE MASTER TO MASTER_HOST='172.16.11.11',MASTER_USER='repluser',MASTER_PASSWORD='redhat',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=355;
  23. 此时在从服务器的数据目录下会生成master.info文件,查看内容
  24. # cat /data/mydata/master.info
  25. 18
  26. mysql-bin.000001
  27. 355
  28. 172.16.11.11
  29. repluser
  30. redhat
  31. 3306
  32. 60
  33. 0
  34.  
  35.  
  36.  
  37.  
  38.  
  39. 0
  40. 1800.000
  41.  
  42. 0
  43. 因此有了master.info文件之后mysql重启之后就会知道从什么位置开始复制主服务器的二进制文件了;
  44. 阻止其它用户对从服务器写功能,但是管理员除外;
  45. mysql> SET GLOBAL read_only=1;
  46. 下面启动从服务器
  47. #mysql
  48. mysql> START SLAVE;
  49. 查看从服务器运行状况
  50. mysql> SHOW SLAVE STATUSG
  51. *************************** 1. row ***************************
  52.                Slave_IO_State: Waiting for master to send event
  53.                   Master_Host: 172.16.11.11
  54.                   Master_User: repluser
  55.                   Master_Port: 3306
  56.                 Connect_Retry: 60
  57.               Master_Log_File: mysql-bin.000001
  58.           Read_Master_Log_Pos: 355
  59.                Relay_Log_File: mysql-relay.000002
  60.                 Relay_Log_Pos: 253
  61.         Relay_Master_Log_File: mysql-bin.000001
  62.              Slave_IO_Running: Yes //IO线程已正常启动//
  63.             Slave_SQL_Running: Yes //SQL线程已正常启动//
  64.               Replicate_Do_DB:
  65.           Replicate_Ignore_DB:
  66.            Replicate_Do_Table:
  67.        Replicate_Ignore_Table:
  68.       Replicate_Wild_Do_Table:
  69.   Replicate_Wild_Ignore_Table:
  70.                    Last_Errno: 0
  71.                    Last_Error:
  72.                  Skip_Counter: 0
  73.           Exec_Master_Log_Pos: 355
  74.               Relay_Log_Space: 405
  75.               Until_Condition: None
  76.                Until_Log_File:
  77.                 Until_Log_Pos: 0
  78.            Master_SSL_Allowed: No
  79.            Master_SSL_CA_File:
  80.            Master_SSL_CA_Path:
  81.               Master_SSL_Cert:
  82.             Master_SSL_Cipher:
  83.                Master_SSL_Key:
  84.         Seconds_Behind_Master: 0
  85. Master_SSL_Verify_Server_Cert: No
  86.                 Last_IO_Errno: 0
  87.                 Last_IO_Error:
  88.                Last_SQL_Errno: 0
  89.                Last_SQL_Error:
  90.   Replicate_Ignore_Server_Ids:
  91.              Master_Server_Id: 1
  92. 上面信息说明从服务器已正常启动;
 以上主从服务器已经配置完成,下面验证一下
  1. 在主服务器上创建数据库testdb
  2. #mysql
  3. mysql> CREATE DATABASE testdb;
  4. mysql> SHOW DATABASES;
  5. +---------------------+
  6. | Database |
  7. +---------------------+
  8. | information_schema |
  9. | #mysql50#lost+found |
  10. | mysql |
  11. | performance_schema |
  12. | test |
  13. | testdb
  14. 在testdb上创建表t1
  15. mysql> use testdb;
  16. mysql> CREATE TABLE t1 (NAME VARCHAR(20));
  17. mysql> FLUSH PRIVILEGES
  18. mysql> SHOW TABLES;
  19. +------------------+
  20. | Tables_in_testdb |
  21. +------------------+
  22. | t1 |
  23. +------------------+
  24. 到从服务器上验证一下复制的效果
  25. #mysql
  26. mysql> SHOW DATABASES;
  27. +---------------------+
  28. | Database |
  29. +---------------------+
  30. | information_schema |
  31. | #mysql50#lost+found |
  32. | mysql |
  33. | performance_schema |
  34. | test |
  35. | testdb | //testdb已经复制成功
  36. +---------------------+
  37. mysql> use testdb;
  38. mysql> SHOW TABLES;
  39. +------------------+
  40. | Tables_in_testdb |
  41. +------------------+
  42. | t1 | //t1表已经存在
  43. +------------------+
  44. mysql> DESC t1;
  45. +-------+-------------+------+-----+---------+-------+
  46. | Field | Type | Null | Key | Default | Extra |
  47. +-------+-------------+------+-----+---------+-------+
  48. | NAME | varchar(20) | YES | | NULL | |
  49. +-------+-------------+------+-----+---------+-------+
阅读(1703) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~