1. 下載安裝包
- 下載適合操作系統的5.7版本MySQL,5.7.18版本下載
- 將下載完的安裝包拷貝到目標服務器上(過程略)
- 解壓文件
$tar -xvf mysql-5.7.18-1.el7.x86_64.rpm-bundle.tar
mysql-community-server-5.7.18-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.18-1.el7.x86_64.rpm
mysql-community-devel-5.7.18-1.el7.x86_64.rpm
mysql-community-client-5.7.18-1.el7.x86_64.rpm
mysql-community-common-5.7.18-1.el7.x86_64.rpm
mysql-community-embedded-5.7.18-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.18-1.el7.x86_64.rpm
mysql-community-libs-5.7.18-1.el7.x86_64.rpm
mysql-community-server-minimal-5.7.18-1.el7.x86_64.rpm
mysql-community-test-5.7.18-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.18-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.18-1.el7.x86_64.rpm
2. 安裝MySQL
- 卸載系統自帶的mariadb
$ rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
$ rpm -e mariadb-libs-5.5.44-2.el7.centos.x86_64 --nodeps
- 安裝軟件
$rpm -ivh mysql-community-common-5.7.18-1.el7.x86_64.rpm
$rpm -ivh mysql-community-libs-5.7.18-1.el7.x86_64.rpm
$rpm -ivh mysql-community-client-5.7.18-1.el7.x86_64.rpm
$rpm -ivh mysql-community-server-5.7.18-1.el7.x86_64.rpm
- 數據庫初始化
在 *nix 系統中,為了保證數據庫目錄為與文件的所有者為 mysql 登陸用戶,如果你是以 root 身份運行 mysql 服務,需要執行下面的命令初始化
$ mysqld --initialize --user=mysql
如果是以 mysql 身份運行,則可以去掉 --user 選項。
另外 --initialize 選項默認以“安全”模式來初始化,則會為 root 用戶生成一個密碼并將該密碼標記為過期,登陸后你需要設置一個新的密碼,而使用 --initialize-insecure 命令則不使用安全模式,則不會為 root 用戶生成一個密碼。
$cat /var/log/mysqld.log
2017-06-13T06:35:39.958489Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for
more details).2017-06-13T06:35:42.024574Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-06-13T06:35:42.278002Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-06-13T06:35:42.369264Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 85d
6cb08-5002-11e7-a098-00163e30a467.2017-06-13T06:35:42.371839Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-06-13T06:35:42.372296Z 1 [Note] A temporary password is generated for root@localhost: ffv*1h7NGteo
最后一行給了root密碼,用這個密碼登錄mysql
$systemctl start mysqld
$mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18
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.
mysql>
- 修改數據庫密碼
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
- 數據庫外網訪問
mysql> use mysql
mysql> update user set host='%' where user='root';
mysql> flush privileges;
- 數據庫配置
[mysqld]
####基本配置####
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid
# 時區調整(所有節點統一)
default-time-zone = '+8:00'
# 服務器ID
server-id = 1
# 端口
port = 3306
# 開啟二進制日志并配置日志名
log_bin = master.bin
# 忽略大小寫區分
lower_case_table_names=1
# 關閉自動提交
autocommit = 0
# 修改默認編碼
character_set_server=utf8
# timestamp列的默認值,null-null,其他-0000-00-00 00:00:00
explicit_defaults_for_timestamp = 1
# 臨時文件路徑
tmpdir = /tmp
# 定義支持的語法、數據校驗等
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
# 兼容5.7版本之前的用法
show_compatibility_56=on
# 事物的寫入方式-哈希編碼方式
transaction_write_set_extraction=MURMUR32
# 文件路徑
datadir= /var/lib/mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
####安全相關####
# 跳過域名解析
skip_name_resolve = 1
# 最大錯誤連接數,阻止破解
max_connect_errors = 1000
# 事務隔離級別,只能讀取到已經提交的數據
transaction_isolation = READ-COMMITTED
####性能相關####
# 最大IP連接數
max_connections = 300
# 臨時表大小 64M
tmp_table_size = 67108864
# 限制server接受的數據包大小 16M
max_allowed_packet = 16777216
# 服務器關閉交互式連接前等待秒數,30分鐘
interactive_timeout = 1800
# 服務器關閉非交互式連接前等待秒數,30分鐘
wait_timeout = 1800
# 讀入緩沖區大小
read_buffer_size = 1M
# 隨機讀緩沖區大小
read_rnd_buffer_size = 2M
# 每一次事物提交都將binlog_cache中的數據強制寫到磁盤
sync_binlog = 1
####日志相關####
# 開啟慢查詢日志
slow_query_log = 1
# 慢查詢日志名
slow_query_log_file = slow.log
# 慢查詢閾值,查詢時間超過閾值時寫入到慢日志中
long_query_time = 2
# 未使用索引的查詢也被記錄到慢日志中
log_queries_not_using_indexes = 1
# 指定執行過慢的DDL語句寫入慢日志
log_slow_admin_statements = 1
# 從庫將超過查詢閾值的查詢記錄到慢日志
log_slow_slave_statements = 1
# 設置每分鐘記錄記錄的未使用索引的查詢的數量10
log_throttle_queries_not_using_indexes = 10
# 少于100行的查詢不會記錄到慢日志中
min_examined_row_limit = 100
# 二進制日志自動刪除的天數
expire_logs_days = 90
# 日志記錄時間戳和系統時間一致
log_timestamps=system
# 錯誤日志路徑
log-error=/var/log/mysqld.log
# 二進制文件模式
binlog_format = row
####復制方式相關-半同步復制####
# 插件路徑
#plugin_dir=/usr/lib/mysql/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
# 設置主需要等待多少個slave應答,才能返回給客戶端,默認為1
rpl_semi_sync_master_wait_for_slave_count=1
# 主庫在返回給會話事務成功之前提交事務的方式
rpl_semi_sync_master_wait_point=AFTER_SYNC
####復制錯誤相關####
# 復制過程中從服務器跳過所有錯誤,也可以指定錯誤號
slave_skip_errors = all
####復制信息相關####
# 將主庫狀態和信息記錄到表中
master_info_repository = TABLE
# 將relay日志中的從庫日志位置記錄到表中
relay_log_info_repository = TABLE
# 從庫宕機后,自動放棄所有未執行的中繼日志,重新從主庫上獲取日志
relay_log_recovery = 1
# 指定中繼日志的位置和文件名
relay_log = relay.log
####GTID相關####
# 開啟gtid工作模式
gtid_mode = on
# 只允許能保障事物安全,且能夠被日志記錄的SQL語句被執行
enforce_gtid_consistency = 1
# 從庫從主庫復制數據時的操作也寫入binlog
log_slave_updates = 1
# 重啟和啟動時,如何迭代使用binlog文件
binlog_gtid_simple_recovery = 1
####InnoDB相關####
# 緩沖池字節大小
innodb_buffer_pool_size = 800M
# 緩沖池實例數量
innodb_buffer_pool_instances = 8
# 啟動時將熱數據加載到內存
innodb_buffer_pool_load_at_startup = 1
# 關閉時將熱數據dump到本地磁盤
innodb_buffer_pool_dump_at_shutdown = 1
# page cleaner線程每次刷新臟頁的數量
innodb_lru_scan_depth = 2000
# 事務等待獲取資源等待的最長時間
innodb_lock_wait_timeout = 5
# 調整刷新臟頁的數量
innodb_io_capacity = 4000
# 刷新臟頁的最大值
innodb_io_capacity_max = 8000
# 數據和日志寫入磁盤的方式-直接寫入磁盤
innodb_flush_method = O_DIRECT
# 文件格式,Barracuda支持壓縮頁,新格式
innodb_file_format = Barracuda
# 設置文件格式最高版本
innodb_file_format_max = Barracuda
# 刷新臟頁臨近頁
innodb_flush_neighbors = 1
# 用來緩沖日志數據的緩沖區大小
innodb_log_buffer_size = 1M
# 單獨的清除線程數量-0不適用單獨線程
innodb_purge_threads = 4
# 為字段創建索引時,限制的字節長度,超過直接報錯
innodb_large_prefix = 1
# 線程并發數
innodb_thread_concurrency = 64
# 將發生的所有死鎖信息都記錄到錯誤日志中
innodb_print_all_deadlocks = 1
# 嚴格檢查模式,寫法有錯誤直接報錯,不警告
innodb_strict_mode = 1
# 建立索引時用于排序數據的排序緩沖區大小-10M
innodb_sort_buffer_size = 10485760
# 轉儲緩沖池中read out and dump 的最近使用的頁的占比
innodb_buffer_pool_dump_pct = 40
# page cleaner線程數量
innodb_page_cleaners = 4
# 開啟在線回收undo log日志文件
innodb_undo_log_truncate = 1
# 超過這個閾值時觸發回收
innodb_max_undo_log_size = 2G
# 回收undo日志的頻率
innodb_purge_rseg_truncate_frequency = 128
innodb_flush_log_at_trx_commit=1
innodb_support_xa=1
開啟了binlog,gtid,半同步復制以及innodb優化。
注意:日志路徑需要保持默認,不要配置插件路徑,不然半同步插件安裝失敗導致無法啟動。
3. MySQL 主從復制開發
1. 創建復制用戶并授權
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'password';
2. 從庫連接主庫
change master to master_host ='172.19.6.209', master_port = 3306, master_user = 'repl', master_password = 'password', MASTER_AUTO_POSITION = 1, MASTER_RETRY_COUNT = 0, MASTER_HEARTBEAT_PERIOD = 10000;
start slave;
3. 驗證
show slave status;