1. 程式人生 > >MySQL MHA高可用方案(二)

MySQL MHA高可用方案(二)

2.1 部署前注意事項

所有MySQL的部署方法與2.2章節的大至一樣,需要修改的如下所示:

01:server_id不能相同

02:相應日誌檔案的檔名不要相同,例如

slow_query_log_file=/data/3306/slowlog/db01_mysql_slow.log 這是db01伺服器

slow_query_log_file=/data/3306/slowlog/db02_mysql_slow.log 這是db02伺服器

slow_query_log_file=/data/3306/slowlog/db03_mysql_slow.log 這是db03伺服器

slow_query_log_file=/data/3306/slowlog/db03_mysql_slow.log 這是db04

伺服器

2.2 前期安裝步驟

#解除安裝已存在的mysql外掛

[[email protected] ~]# rpm -qa|grep mysql

mysql-libs-5.1.73-7.el6.x86_64

[[email protected] ~]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64

[[email protected] ~]# rpm -qa|grep mysql

[[email protected] ~]#


#建立mysql使用者,UID為800,不建立家目錄,不讓其登入

[[email protected] ~]# useradd -u800 -s /sbin/nologin -M mysql

[[email protected] ~]# id mysql

uid=800(mysql) gid=800(mysql) groups=800(mysql)


#建立相應的目錄(/apps /data/3306/{data,errlog,relaylog,binlog,slowlog}

[[email protected] ~]# mkdir /apps /data/3306/{data,errlog,binlog,relaylog,slowlog} -p

[[email protected] ~]# ls -ld /apps/

drwxr-xr-x 2 root root 4096 Nov 14 09:33 /apps/

[[email protected] ~]# tree /data/

/data/

|-- 3306

| |-- binlog

| |-- data

| |-- errlog

| |-- relaylog

| `-- slowlog

`-- lost+found

7 directories, 0 files

#建立tools目錄,上傳mysql軟體包進行安裝

[[email protected] ~]# mkdir tools

[[email protected] ~]# cd tools/

[[email protected] tools]# rz <==該命令如果沒有,可以用yum install lrzsz -y來安裝

[[email protected] tools]# ll mysql-5.7.19-linux-glibc2.12-x86_64_.tar.gz

-rw-r--r-- 1 root root 640650826 Sep 27 16:24 mysql-5.7.19-linux-glibc2.12-x86_64_.tar.gz

[[email protected] tools]# tar xf mysql-5.7.19-linux-glibc2.12-x86_64_.tar.gz -C /apps/

[[email protected] tools]# mv /apps/mysql-5.7.19-linux-glibc2.12-x86_64/ /apps/mysql-5.7.19

[[email protected] tools]# ln -sv /apps/mysql-5.7.19/ /apps/mysql

`/apps/mysql' -> `/apps/mysql-5.7.19/'

[[email protected] tools]# chown -R mysql:mysql /apps/mysql/


2.3  配置my.cnf檔案

[[email protected] tools]# cat /data/3306/my.cnf

[client]

port=3306

socket=/data/3306/mysql.sock


[mysql]

no-auto-rehash


[mysqld]

###### BASIC CONFIGURATION

user=mysql

port=3306

basedir=/apps/mysql

datadir=/data/3306/data

socket=/data/3306/mysql.sock

pid_file=/data/3306/mysql.pid

character_set_server=utf8mb4

skip_character_set_client-handshake=1

###### SERVER ID CONFIGURATION

server_id=11


###### Gtid REPLICATION

gtid_mode=on

log_slave_updates=1

enforce_gtid_consistency=1

binlog_gtid_simple_recovery=1

slave_parallel_type=LOGICAL_CLOCK

slave_parallel_workers=4

slave_preserve_commit_order=1

plugin_dir=/apps/mysql/lib/plugin

plugin_load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"

loose_rpl_semi_sync_master_enabled=1

loose_rpl_semi_sync_slave_enabled=1

loose_rpl_semi_sync_master_timeout=5000

rpl_semi_sync_master_wait_point=AFTER_SYNC

rpl_semi_sync_master_wait_for_slave_count=1


###### LOG CONFIGURATION

log_output=FILE

binlog_format=row

expire_logs_days=15

max_binlog_size=1G

binlog_cache_size=1G

max_binlog_cache_size=2048M

sync_binlog=1

master_info_repository=table

log_bin=/data/3306/binlog/db01_mysql_bin

binlog_rows_query_log_events=on

log_bin_trust_function_creators=1

relay-log=/data/3306/relaylog/db01_relay_bin

relay_log_purge=off

relay_log_recovery=1

relay_log_info_repository=table

log_slow_admin_statements=1

slow_query_log=on

long_query_time=2

log_queries_not_using_indexes=on

slow_query_log_file=/data/3306/slowlog/db01_mysql_slow.log

log-error=/data/3306/errlog/db01_mysql_error.log


###### Basic optimization ##################################

autocommit=on

skip_external_locking=on

skip_name_resolve=off

max_connections=800

max_connect_errors=1000

transaction_isolation=READ-COMMITTED

explicit_defaults_for_timestamp=1

sort_buffer_size=32M

join_buffer_size=128M

tmp_table_size=72M

max_allowed_packet=16M

sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"

interactive_timeout=1800

wait_timeout=1800

read_buffer_size=16M

read_rnd_buffer_size=32M

query_cache_type=1

query_cache_size=1M

table_open_cache=2000

thread_cache_size=768

myisam_max_sort_file_size=10G

myisam_sort_buffer_size=135M

key_buffer_size=32M

read_buffer_size=8M

read_rnd_buffer_size=4M

back_log=1024

#flush_time=0

open_files_limit=65536

table_definition_cache=1400

#binlog_row_event_max_size=8K

#sync_master_info=10000

#sync_relay_log=10000

#sync_relay_log_info=10000

###### INNODB ENGINE CONFIGURATION

default-storage-engine=INNODB

innodb_io_capacity = 4000

innodb_io_capacity_max = 8000

innodb_buffer_pool_size = 500M

innodb_buffer_pool_instances = 8

innodb_buffer_pool_load_at_startup = 1

innodb_buffer_pool_dump_at_shutdown = 1

innodb_lru_scan_depth = 2000

innodb_lock_wait_timeout = 5

innodb_flush_method = O_DIRECT

innodb_log_file_size = 200M

innodb_log_files_in_group = 2

innodb_log_buffer_size = 16M

innodb_undo_logs = 128

innodb_undo_tablespaces = 3

innodb_undo_log_truncate = 1

innodb_max_undo_log_size = 2G

innodb_flush_neighbors = 1

innodb_purge_threads = 4

innodb_large_prefix = 1

innodb_thread_concurrency = 64

innodb_print_all_deadlocks = 1

innodb_strict_mode = 1

innodb_sort_buffer_size = 64M

innodb_flush_log_at_trx_commit=1

innodb_autoextend_increment=64

innodb_concurrency_tickets=5000

innodb_old_blocks_time=1000

innodb_open_files=65536

innodb_stats_on_metadata=0

innodb_file_per_table=1

innodb_checksum_algorithm=0

innodb_data_file_path=ibdata1:200M;ibdata2:200M;ibdata3:200M:autoextend:max:5G

innodb_temp_data_file_path = ibtmp1:200M:autoextend:max:20G

innodb_buffer_pool_dump_pct = 40

innodb_page_cleaners = 4

innodb_purge_rseg_truncate_frequency = 128

log_timestamps=system

#transaction_write_set_extraction=MURMUR32

show_compatibility_56=on

2.4 配置mysql的啟動指令碼

[[email protected] ~]# vim /data/3306/mysqld

http://blog.51cto.com/11576296/2323633 的內容拷貝至檔案中,儲存並退出

說明:

01:該指令碼中停用mysql的方法為:mysqladmin -uroot -pchenliang -S /data/3306/mysql.sock shutdown

02:如果你的mysql服務的root使用者密碼設定的不是chenliang

03:請你更改/data/3306/mysqld指令碼中的Pass變數的值(與你的root使用者的密碼一至)

2.5 後期安裝步驟

# 配置mysql的啟動指令碼

#建立錯誤日誌(看my.cnf檔案中的配置)

#更改/data/3306/目錄下所有檔案的屬主/組為mysql

#更改/data/3306/mysqld的許可權為700

[[email protected] tools]# grep "errlog" /data/3306/my.cnf

log-error=/data/3306/errlog/db01_mysql_error.log

[[email protected] tools]# touch /data/3306/errlog/db01_mysql_error.log

[[email protected] tools]# ll /data/3306/errlog/db01_mysql_error.log

-rw-r--r-- 1 root root 0 Nov 14 10:02 /data/3306/errlog/db01_mysql_error.log

[[email protected] tools]# chown -R mysql:mysql /data/3306/

[[email protected] tools]# chmod 700 /data/3306/mysqld

[[email protected] tools]# ll /data/3306/mysqld

-rwx------ 1 mysql mysql 1603 Nov 14 09:57 /data/3306/mysqld

#配置PATH環境變數

[[email protected] tools]# echo "PATH=/apps/mysql/bin:$PATH" >>/etc/bashrc

[[email protected] tools]# source /etc/bashrc

[[email protected] tools]# echo $PATH

/apps/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin


#初始化mysql服務、啟動mysql服務、更改密碼

[[email protected] tools]# mysqld --defaults-file=/data/3306/my.cnf --user=mysql --basedir=/apps/mysql --datadir=/data/3306/data/ --initialize

[[email protected] tools]# echo $?

0

[[email protected] tools]# /data/3306/mysqld start

Start MySQL [3306] [ OK ]

[[email protected] tools]# lsof -i :3306

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

mysqld 27039 mysql 31u IPv6 32927 0t0 TCP *:mysql (LISTEN)

[[email protected] tools]# grep -i "password" /data/3306/errlog/db01_mysql_error.log

2018-11-14T10:08:51.243406+08:00 1 [Note] A temporary password is generated for [email protected]: w!toDKSy86ki


#因為隨機密碼中有特殊字元"!",所以需用以下這種方式登入,

#如果用mysql -uroot -pw!toDKSy86ki -S /data/3306/mysql.sock登入,會提示如下錯誤

#-bash: !toDKSy86ki: event not found

[[email protected] tools]# mysql -uroot -p -S /data/3306/mysql.sock

Enter password:

mysql>

mysql> alter user 'root'@'localhost' identified by 'chenliang';

Query OK, 0 rows affected (5.02 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

mysql> select user,host,authentication_string from mysql.user where user='root' and host='localhost';

+------+-----------+-------------------------------------------+

| user | host | authentication_string |

+------+-----------+-------------------------------------------+

| root | localhost | *D072DB593E2E7B068E887A452EB1DB3981A5D844 |

+------+-----------+-------------------------------------------+

1 row in set (0.00 sec)

mysql> exit

Bye

[[email protected] tools]# mysql -uroot -pchenliang -S /data/3306/mysql.sock -e "show databases;"

mysql: [Warning] Using a password on the command line interface can be insecure.

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

#加入開機自啟動

[[email protected] tools]# echo -ne "\n#Boot start mysql service. USER:chenliang Time:2018-10-23\n/data/3306/mysqld start\n" >>/etc/rc.local

[[email protected] tools]# tail -2 /etc/rc.local

#Boot start mysql service. USER:chenliang Time:2018-10-23

/data/3306/mysqld start