Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1801707
  • 博文数量: 286
  • 博客积分: 3713
  • 博客等级: 少校
  • 技术积分: 2275
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-11 09:47
个人简介

http://blog.chinaunix.net/uid/16979052.html

文章分类

全部博文(286)

文章存档

2018年(1)

2017年(16)

2016年(9)

2015年(17)

2014年(15)

2013年(112)

2012年(116)

分类:

2012-10-17 15:11:40

最近做哈尔滨的项目,用到 MySQL 主主复制。
网络配置:
  Mysql1  10.0.6.19
  Mysql2  10.0.6.20
大部分配置都和主从一样,不再复述。
1、Mysql 安装
  目前使用版本  MySQL-server-community-5.1.56-1.rhel5.x86_64
2、修改配置文件
    Mysql1  配置文件 /etc/my.cnf
   

点击(此处)折叠或打开

  1. [client]
  2. port = 3306
  3. socket = /e01/mysql/mysql.sock
  4. [mysqld]
  5. port = 3306
  6. socket = /e01/mysql/mysql.sock
  7. back_log = 128
  8. max_connections = 2048
  9. max_connect_errors = 10
  10. table_open_cache = 2048
  11. max_allowed_packet = 2048M
  12. binlog_cache_size = 1M
  13. expire-logs-days=30
  14. max_heap_table_size = 64M
  15. read_buffer_size = 32M
  16. read_rnd_buffer_size = 32M
  17. sort_buffer_size = 32M
  18. join_buffer_size = 32M
  19. thread_cache_size = 64
  20. thread_concurrency = 32
  21. query_cache_size = 512M
  22. query_cache_type = 1
  23. query_cache_limit = 2M
  24. ft_min_word_len = 2
  25. default-storage-engine = INNODB
  26. thread_stack = 192K
  27. transaction_isolation = REPEATABLE-READ
  28. tmp_table_size = 512M
  29. log-bin=mysql-bin
  30. binlog_format=mixed
  31. sync_binlog=1
  32. log-slave-updates
  33. slow_query_log
  34. long_query_time = 10
  35. key_buffer_size = 256M
  36. bulk_insert_buffer_size = 64M
  37. myisam_sort_buffer_size = 128M
  38. myisam_max_sort_file_size = 10G
  39. myisam_repair_threads = 1
  40. myisam_recover
  41. innodb_additional_mem_pool_size = 32M
  42. innodb_buffer_pool_size = 1G
  43. innodb_data_file_path = ibdata1:10M:autoextend
  44. innodb_file_io_threads = 4
  45. innodb_thread_concurrency = 16
  46. innodb_flush_log_at_trx_commit = 1
  47. innodb_log_buffer_size = 8M
  48. innodb_log_file_size = 256M
  49. innodb_log_files_in_group = 3
  50. innodb_max_dirty_pages_pct = 90
  51. innodb_lock_wait_timeout = 120
  52. datadir=/e01/mysql
  53. user=mysql
  54. character-set-server=gbk
  55. lower_case_table_names=1
  56. skip-name-resolve
  57. old_passwords=1
  58. skip-slave-start
  59. max_user_connections=1024
  60. auto_increment_offset = 1   #自增序列从1开始,默认auto 字段为奇数
  61. auto_increment_increment = 2
  62. innodb_file_per_table=1
  63. log-error=/var/log/mysqld.log
  64. tmpdir=/tmp/mysqldata
  65. server-id=1
  66. replicate-wild-do-table=mq4test.%
  67. replicate-wild-ignore-table=mysql.%
  68. relay-log=mysql-relay-bin
  69. #skip-grant-tables
  70. [mysqldump]
  71. quick
  72. max_allowed_packet = 2048M
  73. [mysql]
  74. no-auto-rehash
  75. [myisamchk]
  76. key_buffer_size = 512M
  77. sort_buffer_size = 512M
  78. read_buffer = 8M
  79. write_buffer = 8M
  Mysql2 配置文件与  Mysql1的大部分一致,唯一的区别如下:

点击(此处)折叠或打开

  1. auto_increment_offset = 2  #自增序列从2开始,默认auto 字段为偶数
  2. auto_increment_increment = 2

2、主从同步授权
 

点击(此处)折叠或打开

  1. grant replication slave on *.* to 'repluser'@'%' identified by 'repluser';
3、 在Mysql1 上启动从

点击(此处)折叠或打开

  1. change master to master_host='10.0.6.20', master_user='repluser', master_password='repluser', master_log_file='mysql-bin.000006', master_log_pos=339 ;
  2. start slave;
4、 在Mysql2 上启动从

点击(此处)折叠或打开

  1. change master to master_host='10.0.6.19', master_user='repluser', master_password='repluser', master_log_file='mysql-bin.000007', master_log_pos=339 ;
  2. start slave;

6、做好MySQL 的主主后,开始安装 keepalvied 
  

点击(此处)折叠或打开

  1. #安装IPVSADM
  2. yum install kernel-devel
  3. ln -s /usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux
  4. make
  5. make install
  6. #验证
  7. ipvsadm
  8. IP Virtual Server version 1.2.1 (size=4096)
  9. Prot LocalAddress:Port Scheduler Flags
  10. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  11. lsmod | grep ip_vs
  12. ip_vs 122113 0
  13. # keepalived 安装
  14. #keepalived 1.22 编译错误的解决方法
  15. ./configure -prefix=/usr/local/keepalive
  16. make
  17. make install
  18. mkdir /etc/keepalived/
  19. ln -s /usr/local/keepalive/etc/keepalived/keepalived.conf /etc/keepalived/
  20. ln -s /usr/local/keepalive/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
  21. ln -s /usr/local/keepalive/etc/sysconfig/keepalived /etc/sysconfig/
  22. ln -s /usr/local/keepalive/sbin/keepalived /usr/sbin/
7、配置 keepalived 
vim /etc/keepalived/keepalived.conf

点击(此处)折叠或打开

  1. #Configuration File for keepalived
  2. global_defs {
  3. router_id MySQL-ha
  4. }
  5. vrrp_instance VI_1 {
  6. state BACKUP   #Mysql1,Mysql2上面的配置都是 BACKUP
  7. interface eth0
  8. lvs_sync_daemon_inteface eth0
  9. virtual_router_id 78   #  Mysql1,Mysql2 必须配置一致
  10. priority 100   #优先级  Mysql1 设置100 ,Mysql2 设置 90
  11. advert_int 1
  12. nopreempt   # 不抢占VIP,优先级高的配置该参数
  13. authentication {
  14. auth_type PASS
  15. auth_pass 1111
  16. }
  17. virtual_ipaddress {
  18. 10.0.6.21 dev eth0 label eth0:1   #虚拟IP VIP
  19. }
  20. }
  21. virtual_server 10.0.6.21 3306 {
  22. delay_loop 2
  23. lb_algo wrr
  24. # lb_kind DR 
  25. lb_kind NAT     #lvs 模式,使用 NAT 
  26. persistence_timeout 60
  27. protocol TCP
  28. real_server 10.0.6.19 3306 {
  29. weight 1
  30. notify_down /etc/keepalived/scripts/when_db_down.sh   #检测到服务down 后执行脚本
  31. notify_up /etc/keepalived/scripts/when_db_up.sh
  32. TCP_CHECK {   #检测服务的方式
  33. connect_timeout 10
  34. nb_get_retry 3
  35. delay_before_retry 3
  36. connect_port 3306
  37. }
  38. # MISC_CHECK {       #检测服务的方式
  39. # misc_path "/etc/keepalived/scripts/pingM.sh"
  40. # misc_timeout 5
  41. # }
  42. }
  43. }
8、脚本
    when_db_down.sh 

点击(此处)折叠或打开

  1. #!/bin/sh
  2. if [ -f /var/run/vrrp.pid ]; then
  3. pkill keepalived

  4. #only run health check when mysql goes down
  5. keepalived -C -D -d -S 0 -f /etc/keepalived/keepalived.conf
  6. fi
when_db_up.sh 

点击(此处)折叠或打开

  1. #!/bin/sh
  2. pkill keepalived
  3. #/etc/keepalived/scripts/startMySQL.sh
  4. keepalived -D -d -S 0 -f /etc/keepalived/keepalived.conf --vrrp_pid /var/run/vrrp.pid



9,启动keepalived 测试。略。
阅读(858) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~