手工部署二進位制mysql8.0.x及其升級測試
阿新 • • 發佈:2021-01-14
手工部署二進位制mysql8.0.x及其升級測試
一、環境介紹
這裡以部署8.0.19為例,後續以此升級到8.0.22
作業系統 |
主機名 |
IP |
角色 |
CentOS 7.6 |
db129 |
192.168.93.129 |
主庫 |
CentOS 7.6 |
db130 |
192.168.93.130 |
從庫1 |
CentOS 7.6 |
db131 |
192.168.93.131 |
從庫2 |
二、該測試環境配置阿里雲的yum源
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak
yum clean all yum makecache |
三、建立目錄
mkdir -p /opt/mysql
四、安裝系統依賴包
yum -y install make gcc-c++ cmake bison-devel ncurses-devel readline-devel libaio-devel perl libaio wget lrzsz vim libnuma* bzip2 xz tree
五、關閉selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config setenforce 0
六、修改系統限制引數
cat >> /etc/security/limits.conf <<"EOF" # ###custom # * soft nproc 20480 * hard nproc 65535 * hard nofile 1000000 * soft nofile 1000000 EOF
七、建立mysql使用者
groupadd mysql useradd -g mysql -s /sbin/nologin -d /usr/local/mysql -MN mysql
八、建立對應的目錄
mkdir -p /data/mysql/mysql_3306/{data,logs,tmp}
九、修改檔案描述符
sed -i 's/4096/unlimited/' /etc/security/limits.d/20-nproc.conf cat >>/etc/sysctl.conf <<"EOF" ################################################################ net.ipv4.tcp_keepalive_time =600 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_timestamps=1 net.ipv4.tcp_tw_recycle=1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.ip_local_port_range = 32768 60999 net.ipv4.tcp_max_syn_backlog = 1024 net.core.somaxconn = 1024 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.core.netdev_max_backlog = 1000 net.ipv4.tcp_max_orphans = 2000 #net.nf_conntrack_max = 25000000 #net.netfilter.nf_conntrack_max = 25000000 #net.netfilter.nf_conntrack_tcp_timeout_established = 180 #net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120 #net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60 #net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120 # 結合DDOS和TIME_WAIT過多,建議增加如下引數設定: # Use TCP syncookies when needed net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_synack_retries=3 net.ipv4.tcp_syn_retries=3 net.ipv4.tcp_max_syn_backlog=2048 # Enable TCP window scaling # net.ipv4.tcp_window_scaling: = 1 # Increase TCP max buffer size net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 # Increase Linux autotuning TCP buffer limits net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 # Increase number of ports available net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 300 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.ip_local_port_range = 5000 65000 ################################################################ EOF sysctl -p
十、建立mysql的配置檔案
說明:通常根據記憶體需要修改:innodb_buffer_pool_size = 1024M
一般是機器記憶體的50%~80%
cat >>/etc/bashrc<<"EOF" ulimit -u 65536 EOF source /etc/bashrc \cp /etc/my.cnf /etc/my.cnf.bak \rm -rf /etc/my.cnf cat >/data/mysql/mysql_3306/my.3306.cnf<<"EOF" [client] port = 3306 socket = /data/mysql/mysql_3306/mysql.sock [mysql] prompt="\u@mysqldb \R:\m:\s [\d]> " no-auto-rehash innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:5G [mysqld] user = mysql port = 3306 basedir = /usr/local/mysql datadir = /data/mysql/mysql_3306/data socket = /data/mysql/mysql_3306/mysql.sock pid-file = mysql.3306.pid character-set-server = utf8mb4 skip_name_resolve = 1 lower_case_table_names = 1 innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:5G default_authentication_plugin=mysql_native_password #若你的MySQL資料庫主要執行在境外,請務必根據實際情況調整本引數 default_time_zone = "+8:00" open_files_limit = 65535 back_log = 1024 max_connections = 2000 max_connect_errors = 1000000 table_open_cache = 1024 table_definition_cache = 1024 table_open_cache_instances = 64 thread_stack = 512K external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 3000 interactive_timeout = 600 wait_timeout = 600 tmp_table_size = 32M max_heap_table_size = 32M slow_query_log = 1 log_timestamps = SYSTEM slow_query_log_file = /data/mysql/mysql_3306/logs/slow.log log-error = /data/mysql/mysql_3306/logs/error.log long_query_time = 1 log_queries_not_using_indexes =1 log_throttle_queries_not_using_indexes = 60 min_examined_row_limit = 100 log_slow_admin_statements = 1 log_slow_slave_statements = 1 server-id = 3306 log-bin = /data/mysql/mysql_3306/logs/mysql-binlog sync_binlog = 1 binlog_cache_size = 4M max_binlog_cache_size = 2G max_binlog_size = 1G slave_preserve_commit_order = 1 slave_parallel_type = LOGICAL_CLOCK slave_parallel_workers = 4 log_bin_trust_function_creators = 1 binlog_rows_query_log_events = 1 #注意:MySQL 8.0開始,binlog_expire_logs_seconds選項也存在的話,會忽略expire_logs_days選項 binlog_expire_logs_seconds = 2592000 master_info_repository = TABLE relay_log_info_repository = TABLE gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates = 1 #slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN' binlog_format = row binlog_checksum = 1 relay_log_recovery = 1 relay-log-purge = 1 relay_log=relay-bin key_buffer_size = 32M read_buffer_size = 8M read_rnd_buffer_size = 4M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 lock_wait_timeout = 3600 explicit_defaults_for_timestamp = 1 innodb_thread_concurrency = 0 innodb_sync_spin_loops = 100 innodb_spin_wait_delay = 30 transaction_isolation = REPEATABLE-READ #innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 1024M innodb_buffer_pool_instances = 4 innodb_buffer_pool_load_at_startup = 1 innodb_buffer_pool_dump_at_shutdown = 1 innodb_data_file_path = ibdata1:1G:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 32M innodb_log_file_size = 2G innodb_log_files_in_group = 2 innodb_max_undo_log_size = 4G innodb_undo_directory = /data/mysql/mysql_3306/logs/undolog # 根據您的伺服器IOPS能力適當調整 # 一般配普通SSD盤的話,可以調整到 10000 - 20000 # 配置高階PCIe SSD卡的話,則可以調整的更高,比如 50000 - 80000 innodb_io_capacity = 10000 innodb_io_capacity_max = 20000 innodb_flush_sync = 0 innodb_flush_neighbors = 0 innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_purge_threads = 4 innodb_page_cleaners = 4 innodb_open_files = 65535 innodb_max_dirty_pages_pct = 50 innodb_flush_method = O_DIRECT innodb_lru_scan_depth = 4000 innodb_checksum_algorithm = crc32 innodb_lock_wait_timeout = 10 innodb_rollback_on_timeout = 1 innodb_print_all_deadlocks = 1 innodb_file_per_table = 1 innodb_online_alter_log_max_size = 4G innodb_stats_on_metadata = 0 innodb_undo_log_truncate = 1 #sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' # some var for MySQL 8 log_error_verbosity = 3 innodb_print_ddl_logs = 1 binlog_expire_logs_seconds = 604800 #innodb_dedicated_server = 0 innodb_status_file = 1 #注意: 開啟 innodb_status_output & innodb_status_output_locks 後, 可能會導致log-error檔案增長較快 innodb_status_output = 0 innodb_status_output_locks = 0 #performance_schema performance_schema = 1 performance_schema_instrument = '%memory%=on' performance_schema_instrument = '%lock%=on' #innodb monitor innodb_monitor_enable="module_innodb" innodb_monitor_enable="module_server" innodb_monitor_enable="module_dml" innodb_monitor_enable="module_ddl" innodb_monitor_enable="module_trx" innodb_monitor_enable="module_os" innodb_monitor_enable="module_purge" innodb_monitor_enable="module_log" innodb_monitor_enable="module_lock" innodb_monitor_enable="module_buffer" innodb_monitor_enable="module_index" innodb_monitor_enable="module_ibuf_system" innodb_monitor_enable="module_buffer_page" innodb_monitor_enable="module_adaptive_hash" [mysqldump] quick max_allowed_packet = 32M EOFmy.3306.cnf
十一、解壓安裝包
cd /opt/mysql/ tar -xvJf mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz cd /usr/local ln -s /opt/mysql/mysql-8.0.19-linux-glibc2.12-x86_64 mysql chown -R mysql:mysql /usr/local/mysql chown -R mysql:mysql /data/mysql/ Server_id=1`date +%S%m` sed -i "s/server-id = 3306/server-id = "${Server_id}"/" /data/mysql/mysql_3306/my.3306.cnf grep server-id /data/mysql/mysql_3306/my.3306.cnf
十二、初始化資料
#/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3306/my.3306.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/mysql_3306/data --initialize-insecure /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql_3306/my.3306.cnf --initialize-insecure # 另外開啟一個視窗,檢視日誌 tail -f /data/mysql/mysql_3306/logs/error.log
十三、推薦的啟動方式
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my.3306.cnf & echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile source /etc/profile
十四、準備root密碼檔案
cat >/opt/mysql/init_account.sql<<"EOF" ALTER user 'root'@'localhost' IDENTIFIED BY 'v3FS7nlfSv5M8ORYG2bS'; flush privileges; EOF
cat >/root/.mysql.root.cnf <<"EOF" [client] user=root password="v3FS7nlfSv5M8ORYG2bS" socket=/data/mysql/mysql_3306/mysql.sock EOF mysql -S /data/mysql/mysql_3306/mysql.sock -e "show databases" mysql -S /data/mysql/mysql_3306/mysql.sock < /opt/mysql/init_account.sql
十五、設定快捷登入方式
cat >>~/.bashrc<<"EOF" alias mysql.root="/usr/local/mysql/bin/mysql --defaults-extra-file=/root/.mysql.root.cnf" alias mysql.start="/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my.3306.cnf &" alias mysql.stop="/usr/local/mysql/bin/mysqladmin --defaults-file=/root/.mysql.root.cnf shutdown" alias mysql.restart="sh /opt/mysql/mysql.restart" EOF cat >/opt/mysql/mysql.restart<<"EOF" #/bin/bash is_start_stop=`ps -ef|grep mysqld_safe|grep -v 'grep' |wc -l` if [ ${is_start_stop} -eq 1 ]; then /usr/local/mysql/bin/mysqladmin --defaults-file=/root/.mysql.root.cnf shutdown /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my.3306.cnf & else /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my.3306.cnf & fi EOF source ~/.bashrc
十六、登入重啟等
[root@db131 local]# mysql.root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> exit Bye [root@db131 local]# mysql.stop 2021-01-12T07:38:24.619281Z mysqld_safe mysqld from pid file /data/mysql/mysql_3306/data/mysql.3306.pid ended [1]+ Done /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my.3306.cnf [root@db131 local]# mysql.start [1] 27858 [root@db131 local]# 2021-01-12T07:38:52.676289Z mysqld_safe Logging to '/data/mysql/mysql_3306/logs/error.log'. 2021-01-12T07:38:52.715254Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data
十七、事先準備測試資料
[root@db129 local]# mysql.root -e "create database testdb" [root@db129 local]# mysql.root testdb <test.sql # 根據一張大表,生成多個數據表 [root@db129 local]# cat 1.sh #/bin/bash for i in {1..100}; do /usr/local/mysql/bin/mysql --defaults-extra-file=/root/.mysql.root.cnf -e "use testdb; drop table if exists user"${i}" ; create table user"${i}" like users;" /usr/local/mysql/bin/mysql --defaults-extra-file=/root/.mysql.root.cnf -e "use testdb; insert into user"${i}" (organization_id, organization_name, short_name, business_licence, established_time, effective_begin_time, effective_end_time, legal_person, legal_person_id_card, school_history, employee_number, student_number, student_number_per_year, company_url, register_address, industry_position, hierachical_indentification, contract_number, settle_period, business_linkman, business_linkman_phone, finance_linkman, finance_linkman_phone, bd_linkman, bd_linkman_phone, bd_op_linkman, bd_op_linkman_phone, state, state_op_remarks, service_charge_type, product_type, create_date, update_date, create_user_id, update_user_id, del_flag, remarks, create_user_name, update_user_name, bank_no, payment_mode, service_charge_rate, charge_taken_mode, credit_amount, month_credit_amount, capital_no, seals_code, seals_key, org_out, org_out_days, org_out_remarks, guarantee_cash, phone_check_parent, guarantee_cash_lee_than, settlement_cycle, guarantee_cash_type, guarantee_cash_prop, guarantee_taken_mode, bond_begin_date) select organization_id, organization_name, short_name, business_licence, established_time, effective_begin_time, effective_end_time, legal_person, legal_person_id_card, school_history, employee_number, student_number, student_number_per_year, company_url, register_address, industry_position, hierachical_indentification, contract_number, settle_period, business_linkman, business_linkman_phone, finance_linkman, finance_linkman_phone, bd_linkman, bd_linkman_phone, bd_op_linkman, bd_op_linkman_phone, state, state_op_remarks, service_charge_type, product_type, create_date, update_date, create_user_id, update_user_id, del_flag, remarks, create_user_name, update_user_name, bank_no, payment_mode, service_charge_rate, charge_taken_mode, credit_amount, month_credit_amount, capital_no, seals_code, seals_key, org_out, org_out_days, org_out_remarks, guarantee_cash, phone_check_parent, guarantee_cash_lee_than, settlement_cycle, guarantee_cash_type, guarantee_cash_prop, guarantee_taken_mode, bond_begin_date from users;" done [root@db129 local]#View Code
十八、基於gtid部署一主兩從
# 建立複製賬號 create user 'repl'@'192.168.93.%' IDENTIFIED BY 'repl123'; GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl'@'192.168.93.%'; # 在主庫上匯出資料,推薦全部匯出,儘可能保持一致 mysqldump --defaults-extra-file=/root/.mysql.root.cnf --single-transaction --master-data=2 --set-gtid-purged=on --triggers --routines --events --all-databases>/root/all_databases.dump.sql & scp /root/all_databases.dump.sql [email protected]:/root/ scp /root/all_databases.dump.sql [email protected]:/root/ # 在每個從庫中分別執行匯入操作 mysql.root <all_databases.dump.sql &
十九、部署同步服務
db130部署同步服務
# db130 [root@db130 ~]# head -32 all_databases.dump.sql|tail -8 SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ 'c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74672'; -- -- Position to start replication or point-in-time recovery from -- -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-binlog.000009', MASTER_LOG_POS=990436931; [root@db130 ~]# [root@db130 ~]# mysql.root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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. mysql> reset master; Query OK, 0 rows affected (0.09 sec) mysql> reset slave all; Query OK, 0 rows affected (0.22 sec) mysql> SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ 'c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74672'; mysql>CHANGE MASTER TO MASTER_HOST='192.168.93.129', MASTER_USER='repl', MASTER_PASSWORD='repl123', MASTER_PORT=3306, MASTER_AUTO_POSITION = 1; start slave; show slave status\G; mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000009 Read_Master_Log_Pos: 990436931 Relay_Log_File: db130-relay-bin.000002 Relay_Log_Pos: 425 Relay_Master_Log_File: mysql-binlog.000009 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: 990436931 Relay_Log_Space: 633 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: 14901 Master_UUID: c994129d-54a7-11eb-9b50-000c29a3ed2d 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: Executed_Gtid_Set: c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74672 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set (0.01 sec) ERROR: No query specified mysql>View Code
db131部署同步服務
# db131 [root@db131 ~]# head -32 all_databases.dump.sql|tail -8 SET @@GLOBAL.GTID_PURGED=/*!80000 '+'*/ 'c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74672'; -- -- Position to start replication or point-in-time recovery from -- -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-binlog.000009', MASTER_LOG_POS=990436931; [root@db131 ~]# [root@db131 ~]# mysql.root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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. mysql> reset master; Query OK, 0 rows affected (0.14 sec) mysql> reset slave; Query OK, 0 rows affected (0.03 sec) mysql> mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000009 Read_Master_Log_Pos: 990436931 Relay_Log_File: db131-relay-bin.000002 Relay_Log_Pos: 425 Relay_Master_Log_File: mysql-binlog.000009 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: 990436931 Relay_Log_Space: 633 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: 14901 Master_UUID: c994129d-54a7-11eb-9b50-000c29a3ed2d 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: Executed_Gtid_Set: c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74672 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set (0.00 sec) ERROR: No query specified mysql>View Code
二十、模擬增量資料,驗證同步服務
# 主庫當前狀態 [root@db129 local]# mysql.root -e "show master status" +---------------------+-----------+--------------+------------------+----------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------------+-----------+--------------+------------------+----------------------------------------------+ | mysql-binlog.000009 | 990436931 | | | c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74672 | +---------------------+-----------+--------------+------------------+----------------------------------------------+ [root@db129 local]# sh /usr/local/3.sh & [3] 110976 [root@db129 local]#
#從庫db130 [root@db130 ~]# mysql.root -e "show slave status\G" *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000009 Read_Master_Log_Pos: 991607336 Relay_Log_File: db130-relay-bin.000002 Relay_Log_Pos: 1170830 Relay_Master_Log_File: mysql-binlog.000009 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: 991607336 Relay_Log_Space: 1171038 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: 14901 Master_UUID: c994129d-54a7-11eb-9b50-000c29a3ed2d 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: c994129d-54a7-11eb-9b50-000c29a3ed2d:74673-74681 Executed_Gtid_Set: c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74681 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: [root@db130 ~]#
#從庫db131 [root@db131 ~]# mysql.root -e "show slave status\G" *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000009 Read_Master_Log_Pos: 991607336 Relay_Log_File: db131-relay-bin.000002 Relay_Log_Pos: 1170830 Relay_Master_Log_File: mysql-binlog.000009 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: 991607336 Relay_Log_Space: 1171038 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: 14901 Master_UUID: c994129d-54a7-11eb-9b50-000c29a3ed2d 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: c994129d-54a7-11eb-9b50-000c29a3ed2d:74673-74681 Executed_Gtid_Set: c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74681 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: [root@db131 ~]#
至此,實驗環境部署完畢。
二十一、先升級從庫
#這裡先升級db131這個從庫,在此期間,主庫和db130可以正常使用,在主庫模擬其他增量操作。
步驟如下:
1、升級db131
1.1先停止db131的同步服務
[root@db131 ~]# mysql.root -e "stop slave"
1.2、停止mysqld服務
[root@db131 ~]# mysql.stop 2021-01-13T02:29:52.708073Z mysqld_safe mysqld from pid file /data/mysql/mysql_3306/data/mysql.3306.pid ended [root@db131 ~]#
1.3、備份資料目錄(可選,如果磁碟空間允許,建議操作)
[root@db131 local]# cd /data/mysql/ [root@db131 mysql]# zip -r mysql_3306.zip mysql_3306/ [root@db131 mysql]# du -sh * 16G mysql_3306 4.6G mysql_3306.zip [root@db131 mysql]#
1.4、刪除軟連線
[root@db131 mysql]# cd /usr/local/ [root@db131 local]# unlink mysql
1.5、解壓新版安裝包
[root@db131 local]# cd /opt/mysql/ [root@db131 mysql]# # 解壓 [root@db131 mysql]# tar -xvJf mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz [root@db131 mysql]# cd /usr/local/ [root@db131 local]# ln -s /opt/mysql/mysql-8.0.22-linux-glibc2.12-x86_64 mysql [root@db131 local]# chown -R mysql:mysql /usr/local/mysql [root@db131 local]# chown -R mysql:mysql /data/mysql/
1.6、啟動mysql服務
[root@db131 local]# mysql.start [1] 35193 [root@db131 local]# 2021-01-13T03:13:33.716563Z mysqld_safe Logging to '/data/mysql/mysql_3306/logs/error.log'. 2021-01-13T03:13:33.755643Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data [root@db131 local]# # 啟動完畢後,登入檢視 [root@db131 local]# mysql.root -e "select version()" +-----------+ | version() | +-----------+ | 8.0.22 | +-----------+ [root@db131 local]#
#這裡強調一下,必須再次重啟一次,使之生效 # 從8.0.16開始,變得如此簡單,不在需要mysql_upgrade方式來升級了 [root@db131 local]# mysql.stop 2021-01-13T03:16:40.995849Z mysqld_safe mysqld from pid file /data/mysql/mysql_3306/data/mysql.3306.pid ended [1]+ Done /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my.3306.cnf [root@db131 local]# mysql.start [1] 36872 [root@db131 local]# 2021-01-13T03:16:47.452604Z mysqld_safe Logging to '/data/mysql/mysql_3306/logs/error.log'. 2021-01-13T03:16:47.484759Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data [root@db131 local]# [root@db131 local]# mysql.root -e "select version()" +-----------+ | version() | +-----------+ | 8.0.22 | +-----------+
1.7、檢視同步狀態
[root@db131 local]# mysql.root -e "show slave status \G" *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000009 Read_Master_Log_Pos: 993948146 Relay_Log_File: db131-relay-bin.000006 Relay_Log_Pos: 426 Relay_Master_Log_File: mysql-binlog.000009 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: 993948146 Relay_Log_Space: 635 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: 14901 Master_UUID: c994129d-54a7-11eb-9b50-000c29a3ed2d 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: Executed_Gtid_Set: c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74699 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: [root@db131 local]#View Code
2、升級db130
2.1、先停止db130的同步服務
[root@db130 ~]# mysql.root -e "stop slave"
2.2、停止mysqld服務
[root@db130 ~]# mysql.stop [root@db130 ~]# 2021-01-13T03:22:04.936102Z mysqld_safe mysqld from pid file /data/mysql/mysql_3306/data/mysql.3306.pid ended
2.3、備份資料目錄(可選,如果磁碟空間允許,建議操作)
[root@db130 ~]# cd /data/mysql/ [root@db130 mysql]# tar -czf mysql_3306.tar.gz mysql_3306/ &
2.4、刪除軟連線
[root@db130 mysql]# cd /usr/local/ [root@db130 local]# unlink mysql
2.5、解壓新版安裝包
[root@db130 local]# cd /opt/mysql/ [root@db130 mysql]# # 解壓 [root@db130 mysql]# tar -xvJf mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz [root@db130 mysql]# cd /usr/local/ [root@db130 local]# ln -s /opt/mysql/mysql-8.0.22-linux-glibc2.12-x86_64 mysql [root@db130 local]# chown -R mysql:mysql /usr/local/mysql [root@db130 local]# chown -R mysql:mysql /data/mysql/
2.6、啟動mysql服務
[root@db130 local]# mysql.start [1] 32357 [root@db130 local]# 2021-01-13T09:34:26.845907Z mysqld_safe Logging to '/data/mysql/mysql_3306/logs/error.log'. 2021-01-13T09:34:26.957959Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data [root@db130 local]# # 啟動完畢後,登入檢視 [root@db130 local]# mysql.root -e "select version()" +-----------+ | version() | +-----------+ | 8.0.22 | +-----------+ [root@db130 local]#
#這裡強調一下,必須再次重啟一次,使之生效 # 從8.0.16開始,變得如此簡單,不在需要mysql_upgrade方式來升級了 [root@db130 local]# mysql.stop [root@db130 local]# 2021-01-13T09:37:03.387329Z mysqld_safe mysqld from pid file /data/mysql/mysql_3306/data/mysql.3306.pid ended [1]+ Done /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my.3306.cnf [root@db130 local]# mysql.start [1] 34037 [root@db130 local]# 2021-01-13T09:37:13.664956Z mysqld_safe Logging to '/data/mysql/mysql_3306/logs/error.log'. 2021-01-13T09:37:13.703788Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data [root@db130 local]# mysql.root -e "select version()" +-----------+ | version() | +-----------+ | 8.0.22 | +-----------+ [root@db130 local]#
3、升級主庫db129
注意,為了安全期間,先停止所有從庫的同步服務。
[root@db130 local]# mysql.root -e "stop slave" [root@db131 local]# mysql.root -e "stop slave"
3.1、設定只讀狀態
mysql> set global read_only=0; Query OK, 0 rows affected (0.12 sec)
3.2、停止服務
[root@db129 ~]# mysql.stop
3.3、備份資料
[root@db129 ~]# cd /data/mysql/ [root@db129 mysql]# zip -r mysql_3306.zip mysql_3306/ [root@db129 mysql]# du -sh * 25G mysql_3306 7.7G mysql_3306.zip [root@db129 mysql]#
3.4、刪除軟連線
[root@db129 mysql]# cd /usr/local/ [root@db129 local]# unlink mysql
3.5、解壓安裝包
[root@db129 local]# cd /opt/mysql/ [root@db129 mysql]# tar -xvJf mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz [root@db130 ~]# cd /usr/local/ [root@db130 local]# ln -s /opt/mysql/mysql-8.0.22-linux-glibc2.12-x86_64 mysql [root@db130 local]# chown -R mysql:mysql /usr/local/mysql [root@db130 local]# chown -R mysql:mysql /data/mysql/
3.6、啟動mysql服務
[root@db129 local]# mysql.start [1] 112050 [root@db129 local]# 2021-01-13T10:47:59.274383Z mysqld_safe Logging to '/data/mysql/mysql_3306/logs/error.log'. 2021-01-13T10:47:59.390555Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data [root@db129 local]# # 啟動完畢後,登入檢視 [root@db129 local]# mysql.root -e "select version()" +-----------+ | version() | +-----------+ | 8.0.22 | +-----------+ [root@db129 local]#
#這裡強調一下,必須再次重啟一次,使之生效 # 從8.0.16開始,變得如此簡單,不在需要mysql_upgrade方式來升級了 [root@db129 local]# mysql.stop 2021-01-13T10:49:43.018402Z mysqld_safe mysqld from pid file /data/mysql/mysql_3306/data/mysql.3306.pid ended [1]+ Done /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_3306/my.3306.cnf [root@db129 local]# [root@db129 local]# mysql.start [1] 113723 [root@db129 local]# 2021-01-13T10:49:48.554893Z mysqld_safe Logging to '/data/mysql/mysql_3306/logs/error.log'. 2021-01-13T10:49:48.588319Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data [root@db129 local]# mysql.root -e "select version()" +-----------+ | version() | +-----------+ | 8.0.22 | +-----------+ [root@db129 local]#
3.7、啟動從庫的同步服務
# 啟動db130同步服務 [root@db130 local]# mysql.root -e "start slave" [root@db130 local]# mysql.root -e "show slave status\G" *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000011 Read_Master_Log_Pos: 196 Relay_Log_File: db130-relay-bin.000007 Relay_Log_Pos: 377 Relay_Master_Log_File: mysql-binlog.000011 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: 196 Relay_Log_Space: 856 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: 14901 Master_UUID: c994129d-54a7-11eb-9b50-000c29a3ed2d 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: Executed_Gtid_Set: c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74699 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: [root@db130 local]#View Code
#啟動db131同步服務 [root@db131 ~]# mysql.root -e "start slave" [root@db131 ~]# mysql.root -e "show slave status\G" *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000011 Read_Master_Log_Pos: 196 Relay_Log_File: db131-relay-bin.000007 Relay_Log_Pos: 377 Relay_Master_Log_File: mysql-binlog.000011 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: 196 Relay_Log_Space: 856 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: 14901 Master_UUID: c994129d-54a7-11eb-9b50-000c29a3ed2d 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: Executed_Gtid_Set: c994129d-54a7-11eb-9b50-000c29a3ed2d:1-74699 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: [root@db131 ~]#View Code
附錄:
1、centos永久修改主機名
# centos7修改主機名的方法 hostnamectl set-hostname db129 hostnamectl set-hostname db130 hostnamectl set-hostname db131
2、修改主機對應關係
cat >>/etc/hosts <<"EOF" 192.168.93.129 db129 192.168.93.130 db130 192.168.93.131 db131 EOF
3、關閉防火牆和iptables
systemctl disable --now firewalld systemctl disable --now dnsmasq systemctl disable --now NetworkManager systemctl disable --now iptables setenforce 0 sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config systemctl stop firewalld.service systemctl stop iptables.service echo "systemctl stop iptables.service" >>/etc/rc.local echo "systemctl stop firewalld.service" >>/etc/rc.local chmod +x /etc/rc.d/rc.local
4、阿里雲yum配置
1、禁用 yum外掛 fastestmirror 1.1、修改外掛的配置檔案 cp /etc/yum/pluginconf.d/fastestmirror.conf /etc/yum/pluginconf.d/fastestmirror.conf.bak vi /etc/yum/pluginconf.d/fastestmirror.conf enabled = 1 //由1改為0,禁用該外掛 1.2、修改yum的配置檔案 cp /etc/yum.conf /etc/yum.conf.bak vi /etc/yum.conf plugins=1 //改為0,不使用外掛 2. 獲取阿里雲 repo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 3.清理原來的快取,重新快取 yum clean all yum makecache