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
- # For advice on how to change settings please see
- # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
- [mysqld]
- # Remove leading # and set to the amount of RAM for the most important data
- # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
- # innodb_buffer_pool_size = 128M
- # Remove leading # to turn on a very important data integrity option: logging
- # changes to the binary log between backups.
- # log_bin
- # These are commonly set, remove the # and set as required.
- # basedir = .....
- # datadir = .....
- datadir = /mydata/data
- # port = .....
- # server_id = .....
- # socket = .....
- # Remove leading # to set options mainly useful for reporting servers.
- # The server defaults are faster for transactions and fast SELECTs.
- # Adjust sizes as needed, experiment to find the optimal values.
- # join_buffer_size = 128M
- # sort_buffer_size = 2M
- # read_rnd_buffer_size = 2M
- 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
- # For advice on how to change settings please see
- # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
- [mysqld]
- # Remove leading # and set to the amount of RAM for the most important data
- # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
- # innodb_buffer_pool_size = 128M
- # Remove leading # to turn on a very important data integrity option: logging
- # changes to the binary log between backups.
- log_bin = /mydata/binlog/master-bin
- # These are commonly set, remove the # and set as required.
- # basedir = .....
- # datadir = .....
- datadir = /mydata/data
- # port = .....
- server_id = 10
- # socket = .....
- # Remove leading # to set options mainly useful for reporting servers.
- # The server defaults are faster for transactions and fast SELECTs.
- # Adjust sizes as needed, experiment to find the optimal values.
- # join_buffer_size = 128M
- # sort_buffer_size = 2M
- # read_rnd_buffer_size = 2M
- sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
- binlog_format=row
- innodb_file_per_table = 1
- 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
- # For advice on how to change settings please see
- # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
- [mysqld]
- # Remove leading # and set to the amount of RAM for the most important data
- # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
- # innodb_buffer_pool_size = 128M
- # Remove leading # to turn on a very important data integrity option: logging
- # changes to the binary log between backups.
- # log_bin = /mydata/binlog/master-bin
- # These are commonly set, remove the # and set as required.
- # basedir = .....
- # datadir = .....
- datadir = /mydata/data
- # port = .....
- server_id = 11
- # socket = .....
- # Remove leading # to set options mainly useful for reporting servers.
- # The server defaults are faster for transactions and fast SELECTs.
- # Adjust sizes as needed, experiment to find the optimal values.
- # join_buffer_size = 128M
- # sort_buffer_size = 2M
- # read_rnd_buffer_size = 2M
- sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
- relay-log = relay-log
- relay-log-index = relay-log.index
- read-only = ON
- 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
- # For advice on how to change settings please see
- # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
- [mysqld]
- # Remove leading # and set to the amount of RAM for the most important data
- # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
- # innodb_buffer_pool_size = 128M
- # Remove leading # to turn on a very important data integrity option: logging
- # changes to the binary log between backups.
- log_bin = /mydata/binlog/master-bin
- # These are commonly set, remove the # and set as required.
- # basedir = .....
- # datadir = .....
- datadir = /mydata/data
- # port = .....
- server_id = 10
- # socket = .....
- # Remove leading # to set options mainly useful for reporting servers.
- # The server defaults are faster for transactions and fast SELECTs.
- # Adjust sizes as needed, experiment to find the optimal values.
- # join_buffer_size = 128M
- # sort_buffer_size = 2M
- # read_rnd_buffer_size = 2M
- sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
- binlog_format=row
- innodb_file_per_table = 1
- sync-binlog = 1
- rpl_semi_sync_master_enabled=1
- rpl_semi_sync_master_timeout=3000
# vim /etc/my.cnf (slave)
[html] view plain copy
- # For advice on how to change settings please see
- # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
- [mysqld]
- # Remove leading # and set to the amount of RAM for the most important data
- # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
- # innodb_buffer_pool_size = 128M
- # Remove leading # to turn on a very important data integrity option: logging
- # changes to the binary log between backups.
- # log_bin = /mydata/binlog/master-bin
- # These are commonly set, remove the # and set as required.
- # basedir = .....
- # datadir = .....
- datadir = /mydata/data
- # port = .....
- server_id = 11
- # socket = .....
- # Remove leading # to set options mainly useful for reporting servers.
- # The server defaults are faster for transactions and fast SELECTs.
- # Adjust sizes as needed, experiment to find the optimal values.
- # join_buffer_size = 128M
- # sort_buffer_size = 2M
- # read_rnd_buffer_size = 2M
- sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
- binlog_format=row
- innodb_file_per_table = 1
- read-only = ON
- rpl_semi_sync_slave_enabled=1