MySQL双主+keeplived安装部署说明
MySQL双主+keeplived安装部署说明
一、环境介绍
1.1、规划
序号 |
类别 |
版本 |
主机名 |
IP |
端口 |
备注 |
1 |
OS |
CentOS release 6.9 (Final) (minimal) |
my1 |
172.16.210.180 |
8306 |
172.16.210.183 |
2 |
mysql |
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz |
my2 |
172.16.210.181 |
8306 |
|
3 |
keeplived |
keepalived-1.2.7.tar.gz |
参考资料:
http://www.cnblogs.com/276815076/p/5649539.html
mysql下载地址:
https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
keeplived下载地址:
http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
1.2、MySQL双主互备+keepalived高可用架构介绍
MySQL主从复制架构可以在很大程度保证MySQL的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力。但是在这种架构中,主库出现故障时需要手动将一台从库提升为主库。在对写操作要求较高的环境中,主库故障在主从架构中会成为单点故障。因此需要主主互备架构,避免主节点故障造成写操作失效。
在双主互备的架构中,每台MySQL都充当主服务器,同时充当对方的从服务器。在任意一台服务器上的写操作都会被复制到另一台服务器上,从而保证了数据的可靠性。
在双主互备的基础上加上keepalived,在其中一台机器上绑定虚拟ip(VIP)。利用vip统一对外服务,可以避免在两个节点同时写数据造成冲突。同时当keepalived主节点发生故障时,keeplived会自动将VIP切换到备节点上,从而实现主服务器的高可用。
二、安装mysql5.7
2.1、主机M1上的操作
2.1.1、安装依赖包
- yum clean all
- yum -y update
- yum -y install gcc gcc-c++ make autoconf automake ncurses-devel bison ncurses cmake libaio libaio-devel boost
- yum -y install gcc-c++ gd libxml2-devel libjpeg-devel libpng-devel net-snmp-devel wget telnet vim zip unzip
- yum -y install curl-devel libxslt-devel pcre-devel libjpeg libpng libcurl4-openssl-dev
- yum -y install libcurl-devel libcurl freetype-config freetype freetype-devel unixODBC libxslt
- yum -y install gcc automake autoconf libtool openssl-devel
- yum -y install perl-devel perl-ExtUtils-Embed
- yum -y install cmake ncurses-devel.x86_64 openldap-devel.x86_64 lrzsz openssh-clients gcc-g77 bison
- yum -y install libmcrypt libmcrypt-devel mhash mhash-devel bzip2 bzip2-devel
- yum -y install ntpdate rsync svn patch iptables iptables-services
- yum -y install libevent libevent-devel cyrus-sasl cyrus-sasl-devel
- yum -y install gd-devel libmemcached-devel memcached git libssl-devel libyaml-devel auto make
- yum -y groupinstall "Server Platform Development" "Development tools"
- yum -y groupinstall "Development tools"
2.1.2、下载解压
- rm -rf /etc/my.cnf
- mkdir -p /opt/mysql
- cd /opt/mysql/
- wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
- tar -zxf mysql-5.7.-linux-glibc2.-x86_64.tar.gz
- cd /usr/local/
- ln -s /opt/mysql/mysql-5.7.-linux-glibc2.-x86_64 mysql
2.1.3、创建所需要的目录
- mkdir -p /data/mysql/mysql_8306/{data,logs,tmp}
2.1.4、更改权限
- groupadd mysql
- useradd -g mysql mysql -d /home/mysql -s /sbin/nologin
2.1.5、创建my.cnf
- cat >/data/mysql/mysql_8306/my_8306.cnf <<EOF
- #my.cnf
- [client]
- port =
- socket = /data/mysql/mysql_8306/tmp/mysql_8306.sock
- [mysql]
- #prompt="\u@\h:\p \R:\m:\s [\d]>"
- #tee=/data/mysql/mysql_8306/data/query.log
- #prompt="\u@\h:\p \R:\m:\s [\d]>"
- prompt = "[\u@\h][\d]>\_"
- connect_timeout =
- no-auto-rehash
- [mysqld]
- #misc
- user = mysql
- basedir = /usr/local/mysql
- datadir = /data/mysql/mysql_8306/data
- port =
- socket = /data/mysql/mysql_8306/tmp/mysql_8306.sock
- #timeout
- interactive_timeout =
- wait_timeout =
- #character set
- character-set-server = utf8
- open_files_limit =
- max_connections =
- max_connect_errors =
- skip-name-resolve =
- #logs
- log-output=file
- slow_query_log =
- slow_query_log_file = /data/mysql/mysql_8306/logs/slow.log
- log-error = /data/mysql/mysql_8306/logs/error.log
- log_error_verbosity =
- pid-file = mysql.pid
- long_query_time =
- #log-slow-admin-statements =
- #log-queries-not-using-indexes =
- log-slow-slave-statements =
- #tmp
- tmpdir=/data/mysql/mysql_8306/tmp
- event_scheduler =
- performance_schema = on
- max_allowed_packet = 32M
- character_set_server = utf8mb4
- #character_set_server = utf8
- default-time-zone = system
- default-storage-engine = InnoDB
- #bind_address = 172.16.151.248
- explicit_defaults_for_timestamp =
- #binlog
- binlog_format = row
- server-id =
- log-bin = /data/mysql/mysql_8306/logs/mysql-bin
- log-bin-index = /data/mysql/mysql_8306/logs/mysql-bin.index
- binlog_cache_size = 4M
- max_binlog_size = 1G
- max_binlog_cache_size = 2G
- sync_binlog =
- expire_logs_days =
- #replicate-wild-ignore-table=mysql.%
- replicate-wild-ignore-table=test.%
- replicate-wild-ignore-table=information_schema.%
- #relay log
- skip_slave_start =
- max_relay_log_size = 1G
- relay_log_purge =
- relay_log_recovery =
- log_slave_updates
- #slave-skip-errors=,,
- explicit_defaults_for_timestamp=
- #buffers & cache
- table_open_cache =
- table_definition_cache =
- table_open_cache =
- max_heap_table_size = 96M
- sort_buffer_size = 2M
- join_buffer_size = 2M
- thread_cache_size =
- query_cache_size =
- query_cache_type =
- query_cache_limit = 256K
- query_cache_min_res_unit =
- thread_stack = 192K
- tmp_table_size = 96M
- key_buffer_size = 8M
- read_buffer_size = 2M
- read_rnd_buffer_size = 16M
- bulk_insert_buffer_size = 32M
- #myisam
- myisam_sort_buffer_size = 128M
- myisam_max_sort_file_size = 10G
- myisam_repair_threads =
- #innodb
- innodb_buffer_pool_size = 10G
- innodb_buffer_pool_instances =
- innodb_data_file_path = ibdata1:1G:autoextend
- innodb_flush_log_at_trx_commit =
- innodb_log_buffer_size = 64M
- innodb_log_file_size = 500M
- innodb_log_files_in_group =
- innodb_max_dirty_pages_pct =
- innodb_file_per_table =
- innodb_rollback_on_timeout
- innodb_status_file =
- innodb_io_capacity =
- transaction_isolation = READ-COMMITTED
- innodb_flush_method = O_DIRECT
- gtid_mode = ON
- enforce_gtid_consistency = ON
- master_info_repository = TABLE
- relay-log-info-repository = TABLE
- binlog_checksum = NONE
- log_slave_updates = ON
- # Two-Master configure
- #server-
- auto-increment-offset =
- auto-increment-increment =
- #server-
- #auto-increment-offset =
- #auto-increment-increment =
- # semi sync replication settings #
- plugin_dir = /usr/local/mysql/lib/plugin #官方版本的路径
- #plugin_dir = /usr/local/mysql/lib/mysql/plugin
- plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" #官方版本的路径
- #plugin_load = "validate_password.so;rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
- loose_rpl_semi_sync_master_enabled = on
- loose_rpl_semi_sync_master_timeout =
- loose_rpl_semi_sync_master_trace_level =
- loose_rpl_semi_sync_master_wait_no_slave = on
- loose_rpl_semi_sync_slave_enabled = on
- loose_rpl_semi_sync_slave_trace_level =
- loose_rpl_semi_sync_master_enabled =
- loose_rpl_semi_sync_slave_enabled =
- loose_rpl_semi_sync_master_timeout =
- loose_rpl_semi_sync_master_wait_for_slave_count=
- loose_rpl_semi_sync_master_wait_point=AFTER_SYNC
- slave_preserve_commit_order =
- slave_transaction_retries =
- log_timestamps = system
- show_compatibility_56 = on
- slave_parallel_workers =
- slave_parallel_type = LOGICAL_CLOCK
- loose_innodb_numa_interleave =
- innodb_buffer_pool_dump_pct =
- innodb_page_cleaners =
- innodb_undo_log_truncate =
- innodb_max_undo_log_size = 2G
- innodb_purge_rseg_truncate_frequency =
- #transaction_write_set_extraction = MURMUR32
- # group replication
- ##log-bin = mysql
- ##server-id =
- ##gtid_mode = ON
- ##enforce_gtid_consistency = ON
- ##master_info_repository = TABLE
- ##relay-log-info-repository = TABLE
- ##binlog_checksum = NONE
- ##log_slave_updates = ON
- ##binlog_format = row
- ##transaction_write_set_extraction=XXHASH64
- ##loose-group_replication_group_name = '3db33b36-0e51-409f-a61d-c99756e90154'
- ##loose-group_replication_start_on_boot = off
- ##loose-group_replication_local_address= "10.125.141.62:28306" # 不能超过5位数字
- ##loose-group_replication_group_seeds= "10.125.141.62:28306,10.125.141.62:23307,10.125.141.62:23308" # 不能超过5位数字
- ##loose-group_replication_bootstrap_group= off
- # loose-group_replication_single_primary_mode=FALSE ###本次搭建的是mutil_mode
- # loose-group_replication_enforce_update_everywhere_checks= TRUE
- [mysqld_safe]
- #malloc-lib=/usr/local/mysql/lib/jmalloc.so
- nice=-
- open-files-limit=
- EOF
2.1.6、修改目录权限
- chown -R mysql.mysql /data/mysql/
2.1.7、初始化
- /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_8306/my_8306.cnf --initialize-insecure &
2.1.8、启动
- /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_8306/my_8306.cnf &
# 推荐的启动方式
- /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_8306/my_8306.cnf &
2.1.9、登陆方式
- /usr/local/mysql/bin/mysql -uroot -p -P8306 -S /data/mysql/mysql_8306/tmp/mysql_8306.sock
或者
- /usr/local/mysql/bin/mysql -P8306 -S /data/mysql/mysql_8306/tmp/mysql_8306.sock
2.1.10、创建授权修改密码
- set sql_log_bin = 0;
- create user 'rpl_user'@'%';
- grant replication slave on *.* to 'rpl_user'@'%' identified by 'rpl_user2017';
- update mysql.user set authentication_string=password('root2017') where user='root';
- flush privileges;
- set sql_log_bin = 1;
- reset master ; reset slave all;
2.1.11、change master
- CHANGE MASTER TO MASTER_HOST='172.16.210.181',MASTER_USER='rpl_user',
- MASTER_PASSWORD='rpl_user2017',MASTER_PORT=8306,
- MASTER_CONNECT_RETRY=10,MASTER_AUTO_POSITION =1;
- start slave;
- show slave status\G;
2.1.12、设置快捷登陆方式
- [root@my1 local]# /usr/local/mysql/bin/mysql_config_editor set --host=localhost --login-path=8306_localhost_login \
- > --user=root --port= --password --socket=/data/mysql/mysql_8306/tmp/mysql_8306.sock
- Enter password:
- [root@my1 local]#
- # 查看
- /usr/local/mysql/bin/mysql_config_editor print --all
- [root@my1 local]# alias mysql..login='/usr/local/mysql/bin/mysql --defaults-file=/data/mysql/mysql_8306/my_8306.cnf --login-path=8306_localhost_login'
2.1.13、快捷关闭数据库
- [root@my1 local]#alias mysql..stop='/usr/local/mysql/bin/mysqladmin --login-path=8306_localhost_login shutdown'
2.1.14、加入备忘录
- [root@my1 ~]# cat >>/root/.bashrc <<EOF
- alias mysql..start='/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_8306/my_8306.cnf &'
- alias mysql..login='/usr/local/mysql/bin/mysql --defaults-file=/data/mysql/mysql_8306/my_8306.cnf --login-path=8306_localhost_login'
- alias mysql..stop='/usr/local/mysql/bin/mysqladmin --login-path=8306_localhost_login shutdown'
- EOF
- [root@my1 ~]# source /root/.bash_profile
2.1.15、备份脚本
- [root@my1 ~]# cat /root/all_database.sh
- #!/bin/bash
- /usr/local/mysql/bin/mysqldump --login-path=8306_localhost_login -R -E --triggers -e --max_allowed_packet= --net_buffer_length= --master-data= --single-transaction --all-databases --quick | gzip >/root/all_database_bak_`date +%Y-%m-%d_%H_%M_%S`.sql.gz
- [root@my1 ~]#
2.2、主机M2上的操作
2.2.1、安装依赖包
- yum clean all
- yum -y update
- yum -y install gcc gcc-c++ make autoconf automake ncurses-devel bison ncurses cmake libaio libaio-devel boost
- yum -y install gcc-c++ gd libxml2-devel libjpeg-devel libpng-devel net-snmp-devel wget telnet vim zip unzip
- yum -y install curl-devel libxslt-devel pcre-devel libjpeg libpng libcurl4-openssl-dev
- yum -y install libcurl-devel libcurl freetype-config freetype freetype-devel unixODBC libxslt
- yum -y install gcc automake autoconf libtool openssl-devel
- yum -y install perl-devel perl-ExtUtils-Embed
- yum -y install cmake ncurses-devel.x86_64 openldap-devel.x86_64 lrzsz openssh-clients gcc-g77 bison
- yum -y install libmcrypt libmcrypt-devel mhash mhash-devel bzip2 bzip2-devel
- yum -y install ntpdate rsync svn patch iptables iptables-services
- yum -y install libevent libevent-devel cyrus-sasl cyrus-sasl-devel
- yum -y install gd-devel libmemcached-devel memcached git libssl-devel libyaml-devel auto make
- yum -y groupinstall "Server Platform Development" "Development tools"
- yum -y groupinstall "Development tools"
2.2.2、下载安装
- rm -rf /etc/my.cnf
- mkdir -p /opt/mysql
- cd /opt/mysql/
- wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
- tar -zxf mysql-5.7.-linux-glibc2.-x86_64.tar.gz
- cd /usr/local/
- ln -s /opt/mysql/mysql-5.7.-linux-glibc2.-x86_64 mysql
2.2.3、创建所需要的目录
- mkdir -p /data/mysql/mysql_8306/{data,logs,tmp}
2.2.4、更改权限
- groupadd mysql
- useradd -g mysql mysql -d /home/mysql -s /sbin/nologin
2.2.5、创建my.cnf
- cat >/data/mysql/mysql_8306/my_8306.cnf <<EOF
- #my.cnf
- [client]
- port =
- socket = /data/mysql/mysql_8306/tmp/mysql_8306.sock
- [mysql]
- #prompt="\u@\h:\p \R:\m:\s [\d]>"
- #tee=/data/mysql/mysql_8306/data/query.log
- #prompt="\u@\h:\p \R:\m:\s [\d]>"
- prompt = "[\u@\h][\d]>\_"
- connect_timeout =
- no-auto-rehash
- [mysqld]
- #misc
- user = mysql
- basedir = /usr/local/mysql
- datadir = /data/mysql/mysql_8306/data
- port =
- socket = /data/mysql/mysql_8306/tmp/mysql_8306.sock
- #timeout
- interactive_timeout =
- wait_timeout =
- #character set
- character-set-server = utf8
- open_files_limit =
- max_connections =
- max_connect_errors =
- skip-name-resolve =
- #logs
- log-output=file
- slow_query_log =
- slow_query_log_file = /data/mysql/mysql_8306/logs/slow.log
- log-error = /data/mysql/mysql_8306/logs/error.log
- log_error_verbosity =
- pid-file = mysql.pid
- long_query_time =
- #log-slow-admin-statements =
- #log-queries-not-using-indexes =
- log-slow-slave-statements =
- #tmp
- tmpdir=/data/mysql/mysql_8306/tmp
- event_scheduler =
- performance_schema = on
- max_allowed_packet = 32M
- character_set_server = utf8mb4
- #character_set_server = utf8
- default-time-zone = system
- default-storage-engine = InnoDB
- #bind_address = 172.16.151.248
- explicit_defaults_for_timestamp =
- #binlog
- binlog_format = row
- server-id =
- log-bin = /data/mysql/mysql_8306/logs/mysql-bin
- log-bin-index = /data/mysql/mysql_8306/logs/mysql-bin.index
- binlog_cache_size = 4M
- max_binlog_size = 1G
- max_binlog_cache_size = 2G
- sync_binlog =
- expire_logs_days =
- #replicate-wild-ignore-table=mysql.%
- replicate-wild-ignore-table=test.%
- replicate-wild-ignore-table=information_schema.%
- #relay log
- skip_slave_start =
- max_relay_log_size = 1G
- relay_log_purge =
- relay_log_recovery =
- log_slave_updates
- #slave-skip-errors=,,
- explicit_defaults_for_timestamp=
- #buffers & cache
- table_open_cache =
- table_definition_cache =
- table_open_cache =
- max_heap_table_size = 96M
- sort_buffer_size = 2M
- join_buffer_size = 2M
- thread_cache_size =
- query_cache_size =
- query_cache_type =
- query_cache_limit = 256K
- query_cache_min_res_unit =
- thread_stack = 192K
- tmp_table_size = 96M
- key_buffer_size = 8M
- read_buffer_size = 2M
- read_rnd_buffer_size = 16M
- bulk_insert_buffer_size = 32M
- #myisam
- myisam_sort_buffer_size = 128M
- myisam_max_sort_file_size = 10G
- myisam_repair_threads =
- #innodb
- innodb_buffer_pool_size = 10G
- innodb_buffer_pool_instances =
- innodb_data_file_path = ibdata1:1G:autoextend
- innodb_flush_log_at_trx_commit =
- innodb_log_buffer_size = 64M
- innodb_log_file_size = 500M
- innodb_log_files_in_group =
- innodb_max_dirty_pages_pct =
- innodb_file_per_table =
- innodb_rollback_on_timeout
- innodb_status_file =
- innodb_io_capacity =
- transaction_isolation = READ-COMMITTED
- innodb_flush_method = O_DIRECT
- gtid_mode = ON
- enforce_gtid_consistency = ON
- master_info_repository = TABLE
- relay-log-info-repository = TABLE
- binlog_checksum = NONE
- log_slave_updates = ON
- # Two-Master configure
- #server-
- #auto-increment-offset =
- #auto-increment-increment =
- #server-
- auto-increment-offset =
- auto-increment-increment =
- # semi sync replication settings #
- plugin_dir = /usr/local/mysql/lib/plugin #官方版本的路径
- #plugin_dir = /usr/local/mysql/lib/mysql/plugin
- plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" #官方版本的路径
- #plugin_load = "validate_password.so;rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
- loose_rpl_semi_sync_master_enabled = on
- loose_rpl_semi_sync_master_timeout =
- loose_rpl_semi_sync_master_trace_level =
- loose_rpl_semi_sync_master_wait_no_slave = on
- loose_rpl_semi_sync_slave_enabled = on
- loose_rpl_semi_sync_slave_trace_level =
- loose_rpl_semi_sync_master_enabled =
- loose_rpl_semi_sync_slave_enabled =
- loose_rpl_semi_sync_master_timeout =
- loose_rpl_semi_sync_master_wait_for_slave_count=
- loose_rpl_semi_sync_master_wait_point=AFTER_SYNC
- slave_preserve_commit_order =
- slave_transaction_retries =
- log_timestamps = system
- show_compatibility_56 = on
- slave_parallel_workers =
- slave_parallel_type = LOGICAL_CLOCK
- loose_innodb_numa_interleave =
- innodb_buffer_pool_dump_pct =
- innodb_page_cleaners =
- innodb_undo_log_truncate =
- innodb_max_undo_log_size = 2G
- innodb_purge_rseg_truncate_frequency =
- #transaction_write_set_extraction = MURMUR32
- # group replication
- ##log-bin = mysql
- ##server-id =
- ##gtid_mode = ON
- ##enforce_gtid_consistency = ON
- ##master_info_repository = TABLE
- ##relay-log-info-repository = TABLE
- ##binlog_checksum = NONE
- ##log_slave_updates = ON
- ##binlog_format = row
- ##transaction_write_set_extraction=XXHASH64
- ##loose-group_replication_group_name = '3db33b36-0e51-409f-a61d-c99756e90154'
- ##loose-group_replication_start_on_boot = off
- ##loose-group_replication_local_address= "10.125.141.62:28306" # 不能超过5位数字
- ##loose-group_replication_group_seeds= "10.125.141.62:28306,10.125.141.62:23307,10.125.141.62:23308" # 不能超过5位数字
- ##loose-group_replication_bootstrap_group= off
- # loose-group_replication_single_primary_mode=FALSE ###本次搭建的是mutil_mode
- # loose-group_replication_enforce_update_everywhere_checks= TRUE
- [mysqld_safe]
- #malloc-lib=/usr/local/mysql/lib/jmalloc.so
- nice=-
- open-files-limit=
- EOF
2.2.6、修改目录权限
- chown -R mysql.mysql /data/mysql/
2.2.7、初始化
- /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_8306/my_8306.cnf --initialize-insecure &
2.2.8、启动
- # /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_8306/my_8306.cnf &
- # 推荐的启动方式
- /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_8306/my_8306.cnf &
2.2.9、登陆方式
- /usr/local/mysql/bin/mysql -uroot -p -P8306 -S /data/mysql/mysql_8306/tmp/mysql_8306.sock
- 或者
- /usr/local/mysql/bin/mysql -P8306 -S /data/mysql/mysql_8306/tmp/mysql_8306.sock
2.2.10、创建授权修改密码
- set sql_log_bin = 0;
- create user 'rpl_user'@'%';
- grant replication slave on *.* to 'rpl_user'@'%' identified by 'rpl_user2017';
- update mysql.user set authentication_string=password('root2017') where user='root';
- flush privileges;
- set sql_log_bin = 1;
- reset master ; reset slave all;
2.2.11、change master
- CHANGE MASTER TO MASTER_HOST='172.16.210.180',MASTER_USER='rpl_user',
- MASTER_PASSWORD='rpl_user2017',MASTER_PORT=8306,
- MASTER_CONNECT_RETRY=10,MASTER_AUTO_POSITION =1;
- start slave;
- show slave status\G;
2.2.12、设置快捷登陆方式
- [root@my2 local]# /usr/local/mysql/bin/mysql_config_editor set --host=localhost --login-path=8306_localhost_login \
- > --user=root --port= --password --socket=/data/mysql/mysql_8306/tmp/mysql_8306.sock
- Enter password:
- [root@my2 local]#
- # 查看
- /usr/local/mysql/bin/mysql_config_editor print --all
- [root@my2 local]# alias mysql..login='/usr/local/mysql/bin/mysql --defaults-file=/data/mysql/mysql_8306/my_8306.cnf --login-path=8306_localhost_login'
2.2.13、快捷关闭数据库
- [root@my2 local]#alias mysql..stop='/usr/local/mysql/bin/mysqladmin --login-path=8306_localhost_login shutdown'
2.2.14、加入备忘录
- [root@my2 ~]# cat >>/root/.bashrc <<EOF
- alias mysql..start='/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_8306/my_8306.cnf &'
- alias mysql..login='/usr/local/mysql/bin/mysql --defaults-file=/data/mysql/mysql_8306/my_8306.cnf --login-path=8306_localhost_login'
- alias mysql..stop='/usr/local/mysql/bin/mysqladmin --login-path=8306_localhost_login shutdown'
- EOF
- [root@my2 ~]# source /root/.bash_profile
2.2.15、备份脚本
- [root@my2 ~]# cat /root/all_database.sh
- #!/bin/bash
- /usr/local/mysql/bin/mysqldump --login-path=8306_localhost_login -R -E --triggers -e --max_allowed_packet= --net_buffer_length= --master-data= --single-transaction --all-databases --quick | gzip >/root/all_database_bak_`date +%Y-%m-%d_%H_%M_%S`.sql.gz
- [root@my2 ~]#
三、验证同步情况
3.1、在my1中操作
- [root@my1 ~]# mysql.8306.login
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 6
- Server version: 5.7.20-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- [root@localhost][(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 4 rows in set (0.00 sec)
- [root@localhost][(none)]> create database db1;
- Query OK, 1 row affected (0.01 sec)
- [root@localhost][(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | db1 |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 5 rows in set (0.00 sec)
- [root@localhost][(none)]>
- [root@localhost][(none)]> show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State:
- Master_Host: 172.16.210.181
- Master_User: rpl_user
- Master_Port: 8306
- Connect_Retry: 10
- Master_Log_File: mysql-bin.000001
- Read_Master_Log_Pos: 150
- Relay_Log_File: my1-relay-bin.000003
- Relay_Log_Pos: 4
- Relay_Master_Log_File: mysql-bin.000001
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno: 0
- Last_Error:
- Skip_Counter: 0
- Exec_Master_Log_Pos: 150
- Relay_Log_Space: 721
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos: 0
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master: NULL
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 0
- Master_UUID: d14b54b4-de49-11e7-96ea-8ae132e2dda2
- Master_Info_File: mysql.slave_master_info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State:
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp:
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set:
- Executed_Gtid_Set: ce20a632-de49-11e7-9587-c2c763ed137c:1
- Auto_Position: 1
- Replicate_Rewrite_DB:
- Channel_Name:
- Master_TLS_Version:
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
- [root@localhost][(none)]>
3.2、在my2中操作
- [root@my2 local]# mysql.8306.login
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 32
- Server version: 5.7.20-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- [root@localhost][(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | db1 |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 5 rows in set (0.00 sec)
- [root@localhost][(none)]> create database db2;
- Query OK, 1 row affected (0.00 sec)
- [root@localhost][(none)]> show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.16.210.180
- Master_User: rpl_user
- Master_Port: 8306
- Connect_Retry: 10
- Master_Log_File: mysql-bin.000003
- Read_Master_Log_Pos: 303
- Relay_Log_File: my2-relay-bin.000003
- Relay_Log_Pos: 508
- Relay_Master_Log_File: mysql-bin.000003
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno: 0
- Last_Error:
- Skip_Counter: 0
- Exec_Master_Log_Pos: 303
- Relay_Log_Space: 910
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos: 0
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master: 0
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 1808306
- Master_UUID: ce20a632-de49-11e7-9587-c2c763ed137c
- Master_Info_File: mysql.slave_master_info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp:
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set: ce20a632-de49-11e7-9587-c2c763ed137c:1
- Executed_Gtid_Set: ce20a632-de49-11e7-9587-c2c763ed137c:1,
- d14b54b4-de49-11e7-96ea-8ae132e2dda2:1
- Auto_Position: 1
- Replicate_Rewrite_DB:
- Channel_Name:
- Master_TLS_Version:
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
- 同步正常
四、同步故障处理举例
- # gtid故障处理
- 模拟在从库删除库,然后再在主库删除该库,报如下错误
- Last_SQL_Error: Error 'Can't drop database 'db1'; database doesn't exist' on query. Default database: 'db1'. Query: 'drop database db1'
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 628306
- Master_UUID: 11526eb0-fcbc-11e6-af7d-005056b937e2
- Master_Info_File: mysql.slave_master_info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State:
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp: 170227 15:44:06
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set: 11526eb0-fcbc-11e6-af7d-005056b937e2:1-2
- Executed_Gtid_Set: 11526eb0-fcbc-11e6-af7d-005056b937e2:1,
- 1760a7a5-fcbc-11e6-8f14-005056b90358:1
- Auto_Position: 1
- Replicate_Rewrite_DB:
- Channel_Name:
- Master_TLS_Version:
- 处理方法:
- stop slave;
- set gtid_next='11526eb0-fcbc-11e6-af7d-005056b937e2:2';
- begin;
- commit;
- set gtid_next='automatic';
- start slave;
- show slave status\G;
五、配置keeplived实现高可用
参考资料:
http://blog.51cto.com/lizhenliang/1362313
5.1、在my1中的操作
- yum install -y pcre-devel openssl-devel popt-devel libnl-* libn*#安装依赖包
- # 将keepalived配置成系统服务
- wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
- tar zxvf keepalived-1.2..tar.gz
- cd keepalived-1.2.
- ./configure --prefix=/usr/local/keepalived
- make && make install
- cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
- cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
- mkdir /etc/keepalived/
- cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
- cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
- chmod +x /etc/init.d/keepalived
- # server1
- cat >/etc/keepalived/keepalived.conf <<EOF
- ! Configuration File forkeepalived
- global_defs {
- # notification_email {
- # test@sina.com
- # }
- # notification_email_from admin@test.com
- # smtp_server 127.0.0.1
- # smtp_connect_timeout
- router_id MYSQL_HA18 #标识,双主相同
- }
- vrrp_instance VI_1 {
- state BACKUP #两台都设置BACKUP
- interface eth0
- virtual_router_id #主备相同
- priority #优先级,backup设置90
- advert_int
- nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
- authentication {
- auth_type PASS
- auth_pass
- }
- virtual_ipaddress {
- 172.16.210.183
- }
- }
- virtual_server 172.16.210.183 {
- delay_loop
- #lb_algo rr #LVS算法,用不到,我们就关闭了
- #lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
- persistence_timeout #同一IP的连接60秒内被分配到同一台真实服务器
- protocol TCP
- real_server 172.16.210.180 { #检测本地mysql,backup也要写检测本地mysql
- weight
- notify_down /usr/local/keepalived/mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
- TCP_CHECK {
- connect_timeout #连接超时
- nb_get_retry #重试次数
- delay_before_retry #重试间隔时间
- }
- }
- EOF
- cat >/usr/local/keepalived/mysql.sh <<EOF
- #!/bin/bash
- pkill keepalived
- EOF
- chmod +x /usr/local/keepalived/mysql.sh
- chmod +x /etc/init.d/keepalived
- /etc/init.d/keepalived start
5.2、在my2中的操作
- yum install -y pcre-devel openssl-devel popt-devel libnl-* libn*#安装依赖包
- # 将keepalived配置成系统服务
- wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
- tar zxvf keepalived-1.2..tar.gz
- cd keepalived-1.2.
- ./configure --prefix=/usr/local/keepalived
- make && make install
- cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
- cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
- mkdir /etc/keepalived/
- cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
- cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
- chmod +x /etc/init.d/keepalived
- # server2
- cat >/etc/keepalived/keepalived.conf <<EOF
- cat /etc/keepalived/keepalived.conf
- ! Configuration File forkeepalived
- global_defs {
- # notification_email {
- # test@sina.com
- # }
- # notification_email_from admin@test.com
- # smtp_server 127.0.0.1
- # smtp_connect_timeout
- router_id MYSQL_HA18 #标识,双主相同
- }
- vrrp_instance VI_1 {
- state BACKUP #两台都设置BACKUP
- interface eth0
- virtual_router_id #主备相同
- priority #优先级,backup设置90
- advert_int
- #nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
- authentication {
- auth_type PASS
- auth_pass
- }
- virtual_ipaddress {
- 172.16.210.183
- }
- }
- virtual_server 172.16.210.183 {
- delay_loop
- #lb_algo rr #LVS算法,用不到,我们就关闭了
- #lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
- persistence_timeout #同一IP的连接60秒内被分配到同一台真实服务器
- protocol TCP
- real_server 172.16.210.181 { #检测本地mysql,backup也要写检测本地mysql
- weight
- notify_down /usr/local/keepalived/mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
- TCP_CHECK {
- connect_timeout #连接超时
- nb_get_retry #重试次数
- delay_before_retry #重试间隔时间
- }
- }
- EOF
- cat >/usr/local/keepalived/mysql.sh <<EOF
- #!/bin/bash
- pkill keepalived
- EOF
- chmod +x /usr/local/keepalived/mysql.sh
- chmod +x /etc/init.d/keepalived
- /etc/init.d/keepalived start
六、测试高可用性
6.1、启动my1的keeplive服务
- [root@my1 ~]# /etc/init.d/keepalived start
- Starting keepalived: [ OK ]
- [root@my1 ~]#
- [root@my1 ~]# tail /var/log/messages
- Dec :: localhost Keepalived_healthcheckers[]: IPVS: Service not defined
- Dec :: localhost Keepalived_healthcheckers[]: Using LinkWatch kernel netlink reflector...
- Dec :: localhost Keepalived_healthcheckers[]: Activating healthchecker for service [172.16.210.180]:
- Dec :: localhost kernel: IPVS: Scheduler module ip_vs_ not found
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Transition to MASTER STATE
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering MASTER STATE
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) setting protocol VIPs.
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.210.183
- Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP 172.16.210.183 added
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.210.183
- [root@my1 ~]#
6.2、启动my2的keeplived服务
- [root@my2 ~]# /etc/init.d/keepalived start
- Starting keepalived: [ OK ]
- [root@my2 ~]#
- [root@my2 ~]# tail /var/log/messages
- Dec :: localhost Keepalived_healthcheckers[]: Opening file '/etc/keepalived/keepalived.conf'.
- Dec :: localhost Keepalived_healthcheckers[]: Configuration is using : Bytes
- Dec :: localhost Keepalived_vrrp[]: Using LinkWatch kernel netlink reflector...
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering BACKUP STATE
- Dec :: localhost Keepalived_vrrp[]: VRRP sockpool: [ifindex(), proto(), fd(,)]
- Dec :: localhost Keepalived_healthcheckers[]: IPVS: Scheduler not found
- Dec :: localhost Keepalived_healthcheckers[]: IPVS: Service not defined
- Dec :: localhost Keepalived_healthcheckers[]: Using LinkWatch kernel netlink reflector...
- Dec :: localhost Keepalived_healthcheckers[]: Activating healthchecker for service [172.16.210.181]:
- Dec :: localhost kernel: IPVS: Scheduler module ip_vs_ not found
- [root@my2 ~]#
6.3、关闭my1的mysql服务
- [root@my1 ~]# ps -ef|grep mysql
- root : pts/ :: grep mysql
- root Dec11 ? :: /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_8306/my_8306.cnf
- mysql Dec11 ? :: /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_8306/my_8306.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/mysql_8306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql_8306/logs/error.log --open-files-limit= --pid-file=mysql.pid --socket=/data/mysql/mysql_8306/tmp/mysql_8306.sock --port=
- [root@my1 ~]# mysql..stop
- [root@my1 ~]# ps -ef|grep mysql
- root : pts/ :: grep mysql
- [root@my1 ~]#
- # 查看my1的日志
- [root@my1 ~]# tail - /var/log/messages
- Dec :: localhost Keepalived_healthcheckers[]: IPVS: Service not defined
- Dec :: localhost Keepalived_healthcheckers[]: Using LinkWatch kernel netlink reflector...
- Dec :: localhost Keepalived_healthcheckers[]: Activating healthchecker for service [172.16.210.180]:
- Dec :: localhost kernel: IPVS: Scheduler module ip_vs_ not found
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Transition to MASTER STATE
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering MASTER STATE
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) setting protocol VIPs.
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.210.183
- Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP 172.16.210.183 added
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.210.183
- Dec :: localhost Keepalived_healthcheckers[]: TCP connection to [172.16.210.180]: failed !!!
- Dec :: localhost Keepalived_healthcheckers[]: Removing service [172.16.210.180]: from VS [172.16.210.183]:
- Dec :: localhost Keepalived_healthcheckers[]: IPVS: Service not defined
- Dec :: localhost Keepalived_healthcheckers[]: Executing [/usr/local/keepalived/mysql.sh] for service [172.16.210.180]: in VS [172.16.210.183]:
- Dec :: localhost Keepalived_healthcheckers[]: Lost quorum -= > for VS [172.16.210.183]:
- Dec :: localhost Keepalived[]: Stopping Keepalived v1.2.7 (/,)
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) sending priority
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) removing protocol VIPs.
- Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP 172.16.210.183 removed
- Dec :: localhost Keepalived_healthcheckers[]: IPVS: No such service
- [root@my1 ~]#
6.4、查看my2的日志及其vip情况
- [root@my2 ~]# ip add
- : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
- link/loopback ::::: brd :::::
- inet 127.0.0.1/ scope host lo
- inet6 ::/ scope host
- valid_lft forever preferred_lft forever
- : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
- link/ether 8a:e1::e2:dd:a2 brd ff:ff:ff:ff:ff:ff
- inet 172.16.210.181/ brd 172.16.210.255 scope global eth0
- inet 172.16.210.183/ scope global eth0
- inet6 fe80::88e1:32ff:fee2:dda2/ scope link
- valid_lft forever preferred_lft forever
- : eth1: <BROADCAST,MULTICAST> mtu qdisc noop state DOWN qlen
- link/ether f6:da:a4:::cc brd ff:ff:ff:ff:ff:ff
- [root@my2 ~]#
- [root@my2 ~]# tail - /var/log/messages
- Dec :: localhost Keepalived_vrrp[]: No such interface, eth1
- Dec :: localhost Keepalived_vrrp[]: Netlink reflector reports IP 172.16.210.181 added
- Dec :: localhost Keepalived_vrrp[]: Netlink reflector reports IP fe80::88e1:32ff:fee2:dda2 added
- Dec :: localhost Keepalived_vrrp[]: Registering Kernel netlink reflector
- Dec :: localhost Keepalived_vrrp[]: Registering Kernel netlink command channel
- Dec :: localhost Keepalived_vrrp[]: Registering gratuitous ARP shared channel
- Dec :: localhost Keepalived_healthcheckers[]: Interface queue is empty
- Dec :: localhost Keepalived_healthcheckers[]: No such interface, eth1
- Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP 172.16.210.181 added
- Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP fe80::88e1:32ff:fee2:dda2 added
- Dec :: localhost Keepalived_healthcheckers[]: Registering Kernel netlink reflector
- Dec :: localhost Keepalived_healthcheckers[]: Registering Kernel netlink command channel
- Dec :: localhost Keepalived_vrrp[]: Opening file '/etc/keepalived/keepalived.conf'.
- Dec :: localhost Keepalived_vrrp[]: Configuration is using : Bytes
- Dec :: localhost Keepalived_healthcheckers[]: Opening file '/etc/keepalived/keepalived.conf'.
- Dec :: localhost Keepalived_healthcheckers[]: Configuration is using : Bytes
- Dec :: localhost Keepalived_vrrp[]: Using LinkWatch kernel netlink reflector...
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering BACKUP STATE
- Dec :: localhost Keepalived_vrrp[]: VRRP sockpool: [ifindex(), proto(), fd(,)]
- Dec :: localhost Keepalived_healthcheckers[]: IPVS: Scheduler not found
- Dec :: localhost Keepalived_healthcheckers[]: IPVS: Service not defined
- Dec :: localhost Keepalived_healthcheckers[]: Using LinkWatch kernel netlink reflector...
- Dec :: localhost Keepalived_healthcheckers[]: Activating healthchecker for service [172.16.210.181]:
- Dec :: localhost kernel: IPVS: Scheduler module ip_vs_ not found
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Transition to MASTER STATE
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering MASTER STATE
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) setting protocol VIPs.
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.210.183
- Dec :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP 172.16.210.183 added
- Dec :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.210.183
- [root@my2 ~]#
- 表明已经切换完成。
七、系统参数优化配置
7.1、系统配置
- #手工的执行如下的动作,使之立刻生效,以下是物理机中会有,根据实际可能需要修改:
- echo never > /sys/kernel/mm/transparent_hugepage/enabled
- echo never > /sys/kernel/mm/transparent_hugepage/defrag
- echo deadline > /sys/block/sda/queue/scheduler
- echo "" > /sys/block/sda/queue/read_ahead_kb
- echo "" > /sys/block/sda/queue/nr_requests
- #减少预读:/sys/block/sda/queue/read_ahead_kb,默认128,调整为16
- #增大队列:/sys/block/sda/queue/nr_requests,默认128,调整为512
- echo "" > /sys/block/sda/queue/read_ahead_kb
- echo "" > /sys/block/sda/queue/nr_requests
- #如果是使用普通SAS盘的话,使用elevator=deadline
- #如果是使用SSD/FLASH卡的话,使用elevator=noop
- echo noop > /sys/block/sda/queue/scheduler
- echo deadline > /sys/block/sda/queue/scheduler
- #对于关闭透明大页的问题,也执行如下的操作:编辑 /etc/rc.local,添加如下内容
- cat >> /etc/rc.local <<EOF
- #echo noop > /sys/block/sda/queue/scheduler
- echo deadline > /sys/block/sda/queue/scheduler
- echo never > /sys/kernel/mm/transparent_hugepage/enabled
- echo never > /sys/kernel/mm/transparent_hugepage/defrag
- echo "" > /sys/block/sda/queue/read_ahead_kb
- echo "" > /sys/block/sda/queue/nr_requests
- if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
- echo never > /sys/kernel/mm/transparent_hugepage/enabled
- fi
- if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
- echo never > /sys/kernel/mm/transparent_hugepage/defrag
- fi
- EOF
- # 修改目录权限
- chown -R mysql.mysql /data/mysql/
7.2、修改系统内核参数
- cat >> /etc/sysctl.conf <<EOF
- fs.file-max=
- fs.aio-max-nr =
- kernel.sem =
- kernel.shmmax =
- kernel.shmall =
- kernel.shmmni =
- net.ipv4.ip_local_port_range =
- net.ipv4.tcp_mem =
- net.core.wmem_default =
- net.core.rmem_default =
- net.core.rmem_max =
- net.core.wmem_max =
- net.ipv4.tcp_rmem =
- net.ipv4.tcp_wmem =
- net.core.netdev_max_backlog =
- net.ipv4.tcp_tw_recycle =
- net.ipv4.tcp_tw_reuse =
- net.ipv4.tcp_fin_timeout =
- net.ipv4.tcp_keepalive_time =
- net.ipv4.tcp_max_syn_backlog =
- net.ipv4.tcp_syncookies =
- net.ipv4.tcp_timestamps =
- net.ipv4.conf.default.accept_source_route =
- vm.swappiness=
- EOF
- sysctl -p
- echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
- echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
- echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
- echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
附录:新安装的虚拟机修改信息
1、修改主机名
- yum -y install vim telnet unzip zip lrzsz
- hostname dev_test
- sed -i 's/HOSTNAME=localhost.localdomain/HOSTNAME=dev_test/g' /etc/sysconfig/network
- sed -n '/HOSTNAME/p' /etc/sysconfig/network
2、关闭selinux
- sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
- setenforce
3、关闭防火墙
- /etc/init.d/iptables stop
- cat >>/etc/rc.local <<EOF
- /etc/init.d/iptables stop
- EOF
4、修改网卡ip信息
- cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
- TYPE=Ethernet
- BOOTPROTO=static
- NAME=eth0
- DEVICE=eth0
- ONBOOT=yes
- IPADDR=172.16.210.111
- NETMASK=255.255.255.0
- GATEWAY=172.16.210.250
- EOF
5、重启网卡服务
- /etc/init.d/network restart
6、修改dns
- cat >/etc/resolv.conf <<EOF
- nameserver 172.16.110.11
- nameserver 8.8.8.8
- EOF
7、系统初始化配置
7.1、关闭SELINUX
- #修改配置文件,重启服务后永久生效。
- sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
- #命令行设置立即生效
- setenforce
7.2、防火墙设置
- cat >>/etc/rc.local <<EOF
- /etc/init.d/iptables stop
- EOF
- cp /etc/sysconfig/iptables /root/iptables.bak
- cat >/etc/sysconfig/iptables <<EOF
- # Firewall configuration written by system-config-firewall
- # Manual customization of this file is not recommended.
- *filter
- :INPUT DROP [:]
- :FORWARD ACCEPT [:]
- :OUTPUT ACCEPT [:]
- -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- -A INPUT -p icmp -j ACCEPT
- -A INPUT -i lo -j ACCEPT
- -A INPUT -p tcp -m tcp --dport -j ACCEPT
- -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
- -A INPUT -j REJECT --reject-with icmp-host-prohibited
- -A FORWARD -j REJECT --reject-with icmp-host-prohibited
- COMMIT
- EOF
- /etc/init.d/iptables restart
7.3、安装基础依赖包
- # yum -y install gcc-c++ gd libxml2-devel libjpeg-devel libpng-devel net-snmp-devel wget telnet vim zip unzip
- # yum -y install curl-devel libxslt-devel pcre-devel libjpeg libpng libxml2 libcurl4-openssl-dev
- # yum -y install libcurl-devel libcurl freetype-config freetype freetype-devel unixODBC libxslt
- yum clean all
- yum -y update
- yum -y install gcc-c++ gd libxml2-devel libjpeg-devel libpng-devel net-snmp-devel wget telnet vim zip unzip
- yum -y install curl-devel libxslt-devel pcre-devel libjpeg libpng libcurl4-openssl-dev
- yum -y install libcurl-devel libcurl freetype-config freetype freetype-devel unixODBC libxslt
- yum -y install gcc automake autoconf libtool openssl-devel
- yum -y install perl-devel perl-ExtUtils-Embed
- yum -y install cmake ncurses-devel.x86_64 openldap-devel.x86_64 lrzsz openssh-clients gcc-g77 bison
- yum -y install libmcrypt libmcrypt-devel mhash mhash-devel bzip2 bzip2-devel
- yum -y install ntpdate rsync svn patch iptables iptables-services
- yum -y install libevent libevent-devel cyrus-sasl cyrus-sasl-devel
- yum -y install gd-devel libmemcached-devel memcached git libssl-devel libyaml-devel auto make
- yum -y groupinstall "Server Platform Development" "Development tools"
- yum -y groupinstall "Development tools"
- yum -y install gcc.x86_64 libxml2.x86_64 libxml2-devel.x86_64 openssl.x86_64 openssl-devel.x86_64 libcurl.x86_64 libcurl-devel.x86_64
- yum -y install gd.x86_64 gd-devel.x86_64 gcc-c++.x86_64 readline.x86_64 readline-devel.x86_64
7.4、时间同步
- cat >/root/ntp.sh <<EOF
- #!/bin/bash
- # ntp.sh
- #NTP服务器数组列表
- ntpServer=(
[0]=1.cn.pool.ntp.org
[1]=0.cn.pool.ntp.org
[2]=2.cn.pool.ntp.org
[3]=3.cn.pool.ntp.org
- )
- #校验#
- serverNum=`echo \${#ntpServer[*]}`
- NUM=
- for ((i=; i<=\$serverNum; i++)); do
- echo -n "正在和NTP服务器:\${ntpServer[\$NUM]}校验中..."
- /usr/sbin/ntpdate \${ntpServer[\$NUM]} >> /dev/null >&
- if [ \$? -eq ]; then
- echo -e "\e[1;32m\t[成功]\e[0m"
- echo -e "\e[1;32m同步成功,退出......\e[0m"
- break
- else
- echo -e "\e[1;31m\t[失败]\e[0m"
- echo -e "\e[1;31m继续同步下一个!!!!!\e[0m"
- let NUM++
- fi
- sleep
- done
- EOF
- chmod +x /root/ntp.sh
- sh /root/ntp.sh
- cat >>/etc/crontab <<EOF
- * * * * /root/ntp.sh
- # */ * * /bin/bash /root/cutlog.sh
- EOF
MySQL双主+keeplived安装部署说明的更多相关文章
- Mysql双主互备+keeplived高可用架构介绍
一.Mysql双主互备+keeplived高可用架构介绍 Mysql主从复制架构可以在很大程度保证Mysql的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力.但是在这种 ...
- Mysql双主互备+keeplived高可用架构(部分)
一.Mysql双主互备+keeplived高可用架构介绍 Mysql主从复制架构可以在很大程度保证Mysql的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力.但是在这种 ...
- 高可用Mysql架构_Mycat集群部署(HAProxy + 两台Mycat+Mysql双主双从)
既然大家都知道了Mysql分布式在大型网站架构中的作用,在这里就不再阐述.本片博客文章是基于我曾经搭建过的一个Mysql集群基础上实现的,实现过双主热备.读写分离.分库分表. 博客链接:http:// ...
- MYSQL 双主配置
MYSQL1. 版本号:5.7.243. 部署方式:双主部署,两台机器即是主又是备 ,双向拷贝,可以同时写入.4. 安装部署路径: a) /home/softb) 配置路径 /etc/my.cnfc) ...
- linux环境下配置mysql双主复制
简单来说,双主复制就是让两台mysql服务器中的数据保持同步,可以用来实现灾备和负载均衡 主机1 IP:192.168.200.128 主机2 IP:192.168.200.131 两台主机系统均为c ...
- mysql双主+keepalived【转】
简单原理 1.在两台服务器上分别部署双主keepalived,主keepalived会在当前服务器配置虚拟IP用于mysql对外提供服务 2.在两台服务器上分别部署主主mysql,用于故障切换 3.当 ...
- MySQL 高可用性—keepalived+mysql双主(有详细步骤和全部配置项解释)
博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 前言:生产环境中一台mysql主机存在单 ...
- keepalived+MySQL双主搭建
keepalived+MySQL双主搭建过程 首先要简单了解一下keepalived: Keepalived是Linux下一个轻量级别的高可用解决方案.高可用(High Avalilability,H ...
- nginx+mysql双主搭建
说明:mysql双主架构经过测试在生产环境中使用没有问题,但是还是建议使用读写分离, Mysql主主同步环境部署: centos 7.4 三台云主机:mysql1 :10.1.1.142 mysql2 ...
随机推荐
- PCIE
---恢复内容开始--- 高速差分总线.串行总线 每一条PCIe链路中只能连接两个设备这两个设备互为是数据发送端和数据接收端.PCIe链路可以由多条Lane组成,目前PCIe链路×1.×2.×4.×8 ...
- oss命令使用
下载文件夹 ossutil64 cp oss://folder/ out_folder/ -r --jobs 20
- WebAPI MVC Change Identity Default Table
看过之前的文章小伙伴们应该已经明白了,当我们新建一个带有身份验证的模板时,会自带Identity Server,并且它的表名和字段名也都是默认的. 那么该如何修改它,并让EF知道呢?不废话,直接上代码 ...
- codeforces24D
CF24D Broken robot 题目背景 小小迪带你吃瓜 题目描述 给出一个 n×m 的矩阵区域,一个机器人初始在第 x 行第 y 列,每一步机器人会等概率 的选择停在原地,左移一步,右移一步, ...
- nginx-添加禁止访问规则
location ~* /application/(admin|index)/static/.*$ { allow all; } location ~* /(applicaion|addos|coe| ...
- 学习Android过程中遇到的问题及解决方法——AS为xutils添加依赖
在使用xutils时遇到不能添加以来的问题,花了很长时间终于解决,网上添加依赖的方法很多,在此针对个人出现的问题作下笔记. 我本想使用jar包,因为在使用smartImageView时是用的jar包来 ...
- Easy Finding POJ - 3740 (DLX)
显然这是一道dfs简单题 或许匹配也能做 然而用了dancing links 显然这也是一道模板题 好的吧 调了一上午 终于弄好了模板 Easy Finding Time Limit: 1000MS ...
- bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)
Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...
- 【APIO2018】铁人两项(圆方树,动态规划)
[APIO2018]铁人两项(圆方树,动态规划) 题面 UOJ 洛谷 BZOJ 题解 嘤嘤嘤,APIO的时候把一个组合数写成阶乘了,然后这题的70多分没拿到 首先一棵树是很容易做的,随意指定起点终点就 ...
- 加载样式TTFB waiting时间长
1.谷歌浏览器64位 2.在调试网页的时候,每回修改一个,刷新时,速度很慢 3.结果很恼火.每调试一回等半天.效率低下.... 解决办法是: "在每个CSS规则的后面加一个空行". ...