mysql master-slave (存档)

OS:centos6.8

Package:  mysql-5.6.40-linux-glibc2.12-x86_64

节点准备:node1.test.com      ipaddress: 192.168.1.1

                node2.test.com      ipaddress: 192.168.1.2

1.    下载并安装mysql通用二进制格式软件

# groupadd -r -g 306 mysql  && useradd -r -u 306 -g 306 mysql

# mkdir -pv /mydata/{data,binlog}

# tar -xf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

# ln -s mysql-5.6.40-linux-glibc2.12-x86_64 mysql

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# chkconfig --add mysqld

# chkconfig mysqld off

# echo 'export PATH=$PATH:/usr/local/mysql/bin/' >> /etc/profile

# chown -R mysql.mysql /mydata/data

# ln -sv /usr/local/mysql/include /usr/include/mysql

# echo "/usr/local/mysql/lib/" > /etc/ld.so.conf.d/mysql.conf

# ldconfig -v

2.    初始化mysql到所设置的datadir路径中

# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mydata/data   ##mysql5.6

# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/mydata/data        ## mysql5.7更改了方式

# cp -a mysql/my.cnf /etc/my.cnf

修改my.cnf (设置datadir的路径)

# vim /etc/my.cnf 

[html] view plain copy

  1. # For advice on how to change settings please see  
  2. # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html  
  3.   
  4. [mysqld]  
  5.   
  6. # Remove leading # and set to the amount of RAM for the most important data  
  7. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.  
  8. innodb_buffer_pool_size = 128M  
  9.   
  10. # Remove leading # to turn on a very important data integrity option: logging  
  11. # changes to the binary log between backups.  
  12. # log_bin 
  13.   
  14. # These are commonly set, remove the # and set as required.  
  15. basedir = .....  
  16. datadir = .....  
  17. datadir = /mydata/data  
  18. port = .....  
  19. server_id = .....  
  20. socket = .....  
  21.   
  22. # Remove leading # to set options mainly useful for reporting servers.  
  23. # The server defaults are faster for transactions and fast SELECTs.  
  24. # Adjust sizes as needed, experiment to find the optimal values.  
  25. join_buffer_size = 128M  
  26. sort_buffer_size = 2M  
  27. read_rnd_buffer_size = 2M  
  28.   
  29. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES  

启动mysqld服务并初始化数据库(设置密码,删除匿名登录,test数据库)

# service mysqld start

# mysql_secure_installation

登录mysql测试

# mysql -uroot -p

3.    mysql master-slave (异步)

# vim /etc/my.cnf (master)

[html] view plain copy

  1. # For advice on how to change settings please see  
  2. # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html  
  3.   
  4. [mysqld]  
  5.   
  6. # Remove leading # and set to the amount of RAM for the most important data  
  7. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.  
  8. innodb_buffer_pool_size = 128M  
  9.   
  10. # Remove leading # to turn on a very important data integrity option: logging  
  11. # changes to the binary log between backups.  
  12. log_bin = /mydata/binlog/master-bin
  13.   
  14. # These are commonly set, remove the # and set as required.  
  15. basedir = .....  
  16. datadir = .....  
  17. datadir = /mydata/data  
  18. port = .....  
  19. server_id = 10  
  20. socket = .....  
  21.   
  22. # Remove leading # to set options mainly useful for reporting servers.  
  23. # The server defaults are faster for transactions and fast SELECTs.  
  24. # Adjust sizes as needed, experiment to find the optimal values.  
  25. join_buffer_size = 128M  
  26. sort_buffer_size = 2M  
  27. read_rnd_buffer_size = 2M  
  28.   
  29. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES  
  30. binlog_format=row
  31. innodb_file_per_table = 1
  32. sync-binlog = 1

mysql> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+-------------------+
| File                         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 |         516 |                      |                             |                              |
+-------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.1.2' IDENTIFIED BY 'replpass';

mysql> FLUSH PRIVILEGES;

# vim /etc/my.cnf (slave)

[html] view plain copy

  1. # For advice on how to change settings please see  
  2. # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html  
  3.   
  4. [mysqld]  
  5.   
  6. # Remove leading # and set to the amount of RAM for the most important data  
  7. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.  
  8. innodb_buffer_pool_size = 128M  
  9.   
  10. # Remove leading # to turn on a very important data integrity option: logging  
  11. # changes to the binary log between backups.  
  12. # log_bin = /mydata/binlog/master-bin
  13.   
  14. # These are commonly set, remove the # and set as required.  
  15. basedir = .....  
  16. datadir = .....  
  17. datadir = /mydata/data  
  18. port = .....  
  19. server_id = 11  
  20. socket = .....  
  21.   
  22. # Remove leading # to set options mainly useful for reporting servers.  
  23. # The server defaults are faster for transactions and fast SELECTs.  
  24. # Adjust sizes as needed, experiment to find the optimal values.  
  25. join_buffer_size = 128M  
  26. sort_buffer_size = 2M  
  27. read_rnd_buffer_size = 2M  
  28.   
  29. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES  
  30. relay-log = relay-log
  31. relay-log-index = relay-log.index
  32. read-only = ON
  33. innodb_file_per_table = 1

mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.1',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=516;

mysql> SHOW SLAVE STATUS\G      # 检查mysql的slave状态

mysql> START SLAVE;

 

4.    mysql master-slave (半同步)

# vim /etc/my.cnf (master)

[html] view plain copy

  1. # For advice on how to change settings please see  
  2. # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html  
  3.   
  4. [mysqld]  
  5.   
  6. # Remove leading # and set to the amount of RAM for the most important data  
  7. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.  
  8. innodb_buffer_pool_size = 128M  
  9.   
  10. # Remove leading # to turn on a very important data integrity option: logging  
  11. # changes to the binary log between backups.  
  12. log_bin = /mydata/binlog/master-bin
  13.   
  14. # These are commonly set, remove the # and set as required.  
  15. basedir = .....  
  16. datadir = .....  
  17. datadir = /mydata/data  
  18. port = .....  
  19. server_id = 10  
  20. socket = .....  
  21.   
  22. # Remove leading # to set options mainly useful for reporting servers.  
  23. # The server defaults are faster for transactions and fast SELECTs.  
  24. # Adjust sizes as needed, experiment to find the optimal values.  
  25. join_buffer_size = 128M  
  26. sort_buffer_size = 2M  
  27. read_rnd_buffer_size = 2M  
  28.   
  29. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES  
  30. binlog_format=row
  31. innodb_file_per_table = 1
  32. sync-binlog = 1
  33. rpl_semi_sync_master_enabled=1
  34. rpl_semi_sync_master_timeout=3000

# vim /etc/my.cnf (slave)

[html] view plain copy

  1. # For advice on how to change settings please see  
  2. # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html  
  3.   
  4. [mysqld]  
  5.   
  6. # Remove leading # and set to the amount of RAM for the most important data  
  7. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.  
  8. innodb_buffer_pool_size = 128M  
  9.   
  10. # Remove leading # to turn on a very important data integrity option: logging  
  11. # changes to the binary log between backups.  
  12. # log_bin = /mydata/binlog/master-bin
  13.   
  14. # These are commonly set, remove the # and set as required.  
  15. basedir = .....  
  16. datadir = .....  
  17. datadir = /mydata/data  
  18. port = .....  
  19. server_id = 11  
  20. socket = .....  
  21.   
  22. # Remove leading # to set options mainly useful for reporting servers.  
  23. # The server defaults are faster for transactions and fast SELECTs.  
  24. # Adjust sizes as needed, experiment to find the optimal values.  
  25. join_buffer_size = 128M  
  26. sort_buffer_size = 2M  
  27. read_rnd_buffer_size = 2M  
  28.   
  29. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES  
  30. binlog_format=row
  31. innodb_file_per_table = 1
  32. read-only = ON
  33. rpl_semi_sync_slave_enabled=1

猜你喜欢

转载自blog.csdn.net/Gordon_luo/article/details/80663496
今日推荐