1. 下載安裝包
- 下載適合操作系統(tǒng)的5.7版本MySQL,5.7.18版本下載
- 將下載完的安裝包拷貝到目標(biāo)服務(wù)器上(過程略)
- 解壓文件
$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
- 卸載系統(tǒng)自帶的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
- 數(shù)據(jù)庫初始化
在 *nix 系統(tǒng)中购桑,為了保證數(shù)據(jù)庫目錄為與文件的所有者為 mysql 登陸用戶敌土,如果你是以 root 身份運行 mysql 服務(wù)堪伍,需要執(zhí)行下面的命令初始化
$ mysqld --initialize --user=mysql
如果是以 mysql 身份運行,則可以去掉 --user 選項。
另外 --initialize 選項默認(rèn)以“安全”模式來初始化,則會為 root 用戶生成一個密碼并將該密碼標(biāo)記為過期缝驳,登陸后你需要設(shè)置一個新的密碼,而使用 --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>
- 修改數(shù)據(jù)庫密碼
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
- 數(shù)據(jù)庫外網(wǎng)訪問
mysql> use mysql
mysql> update user set host='%' where user='root';
mysql> flush privileges;
- 數(shù)據(jù)庫配置
[mysqld]
####基本配置####
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid
# 時區(qū)調(diào)整(所有節(jié)點統(tǒng)一)
default-time-zone = '+8:00'
# 服務(wù)器ID
server-id = 1
# 端口
port = 3306
# 開啟二進(jìn)制日志并配置日志名
log_bin = master.bin
# 忽略大小寫區(qū)分
lower_case_table_names=1
# 關(guān)閉自動提交
autocommit = 0
# 修改默認(rèn)編碼
character_set_server=utf8
# timestamp列的默認(rèn)值拼弃,null-null夏伊,其他-0000-00-00 00:00:00
explicit_defaults_for_timestamp = 1
# 臨時文件路徑
tmpdir = /tmp
# 定義支持的語法、數(shù)據(jù)校驗等
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
####安全相關(guān)####
# 跳過域名解析
skip_name_resolve = 1
# 最大錯誤連接數(shù)吻氧,阻止破解
max_connect_errors = 1000
# 事務(wù)隔離級別溺忧,只能讀取到已經(jīng)提交的數(shù)據(jù)
transaction_isolation = READ-COMMITTED
####性能相關(guān)####
# 最大IP連接數(shù)
max_connections = 300
# 臨時表大小 64M
tmp_table_size = 67108864
# 限制server接受的數(shù)據(jù)包大小 16M
max_allowed_packet = 16777216
# 服務(wù)器關(guān)閉交互式連接前等待秒數(shù),30分鐘
interactive_timeout = 1800
# 服務(wù)器關(guān)閉非交互式連接前等待秒數(shù)盯孙,30分鐘
wait_timeout = 1800
# 讀入緩沖區(qū)大小
read_buffer_size = 1M
# 隨機讀緩沖區(qū)大小
read_rnd_buffer_size = 2M
# 每一次事物提交都將binlog_cache中的數(shù)據(jù)強制寫到磁盤
sync_binlog = 1
####日志相關(guān)####
# 開啟慢查詢?nèi)罩?slow_query_log = 1
# 慢查詢?nèi)罩久?slow_query_log_file = slow.log
# 慢查詢閾值鲁森,查詢時間超過閾值時寫入到慢日志中
long_query_time = 2
# 未使用索引的查詢也被記錄到慢日志中
log_queries_not_using_indexes = 1
# 指定執(zhí)行過慢的DDL語句寫入慢日志
log_slow_admin_statements = 1
# 從庫將超過查詢閾值的查詢記錄到慢日志
log_slow_slave_statements = 1
# 設(shè)置每分鐘記錄記錄的未使用索引的查詢的數(shù)量10
log_throttle_queries_not_using_indexes = 10
# 少于100行的查詢不會記錄到慢日志中
min_examined_row_limit = 100
# 二進(jìn)制日志自動刪除的天數(shù)
expire_logs_days = 90
# 日志記錄時間戳和系統(tǒng)時間一致
log_timestamps=system
# 錯誤日志路徑
log-error=/var/log/mysqld.log
# 二進(jìn)制文件模式
binlog_format = row
####復(fù)制方式相關(guān)-半同步復(fù)制####
# 插件路徑
#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
# 設(shè)置主需要等待多少個slave應(yīng)答,才能返回給客戶端镀梭,默認(rèn)為1
rpl_semi_sync_master_wait_for_slave_count=1
# 主庫在返回給會話事務(wù)成功之前提交事務(wù)的方式
rpl_semi_sync_master_wait_point=AFTER_SYNC
####復(fù)制錯誤相關(guān)####
# 復(fù)制過程中從服務(wù)器跳過所有錯誤刀森,也可以指定錯誤號
slave_skip_errors = all
####復(fù)制信息相關(guān)####
# 將主庫狀態(tài)和信息記錄到表中
master_info_repository = TABLE
# 將relay日志中的從庫日志位置記錄到表中
relay_log_info_repository = TABLE
# 從庫宕機后,自動放棄所有未執(zhí)行的中繼日志报账,重新從主庫上獲取日志
relay_log_recovery = 1
# 指定中繼日志的位置和文件名
relay_log = relay.log
####GTID相關(guān)####
# 開啟gtid工作模式
gtid_mode = on
# 只允許能保障事物安全,且能夠被日志記錄的SQL語句被執(zhí)行
enforce_gtid_consistency = 1
# 從庫從主庫復(fù)制數(shù)據(jù)時的操作也寫入binlog
log_slave_updates = 1
# 重啟和啟動時埠偿,如何迭代使用binlog文件
binlog_gtid_simple_recovery = 1
####InnoDB相關(guān)####
# 緩沖池字節(jié)大小
innodb_buffer_pool_size = 800M
# 緩沖池實例數(shù)量
innodb_buffer_pool_instances = 8
# 啟動時將熱數(shù)據(jù)加載到內(nèi)存
innodb_buffer_pool_load_at_startup = 1
# 關(guān)閉時將熱數(shù)據(jù)dump到本地磁盤
innodb_buffer_pool_dump_at_shutdown = 1
# page cleaner線程每次刷新臟頁的數(shù)量
innodb_lru_scan_depth = 2000
# 事務(wù)等待獲取資源等待的最長時間
innodb_lock_wait_timeout = 5
# 調(diào)整刷新臟頁的數(shù)量
innodb_io_capacity = 4000
# 刷新臟頁的最大值
innodb_io_capacity_max = 8000
# 數(shù)據(jù)和日志寫入磁盤的方式-直接寫入磁盤
innodb_flush_method = O_DIRECT
# 文件格式透罢,Barracuda支持壓縮頁,新格式
innodb_file_format = Barracuda
# 設(shè)置文件格式最高版本
innodb_file_format_max = Barracuda
# 刷新臟頁臨近頁
innodb_flush_neighbors = 1
# 用來緩沖日志數(shù)據(jù)的緩沖區(qū)大小
innodb_log_buffer_size = 1M
# 單獨的清除線程數(shù)量-0不適用單獨線程
innodb_purge_threads = 4
# 為字段創(chuàng)建索引時冠蒋,限制的字節(jié)長度羽圃,超過直接報錯
innodb_large_prefix = 1
# 線程并發(fā)數(shù)
innodb_thread_concurrency = 64
# 將發(fā)生的所有死鎖信息都記錄到錯誤日志中
innodb_print_all_deadlocks = 1
# 嚴(yán)格檢查模式,寫法有錯誤直接報錯抖剿,不警告
innodb_strict_mode = 1
# 建立索引時用于排序數(shù)據(jù)的排序緩沖區(qū)大小-10M
innodb_sort_buffer_size = 10485760
# 轉(zhuǎn)儲緩沖池中read out and dump 的最近使用的頁的占比
innodb_buffer_pool_dump_pct = 40
# page cleaner線程數(shù)量
innodb_page_cleaners = 4
# 開啟在線回收undo log日志文件
innodb_undo_log_truncate = 1
# 超過這個閾值時觸發(fā)回收
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识窿,半同步復(fù)制以及innodb優(yōu)化。
注意:日志路徑需要保持默認(rèn)脑融,不要配置插件路徑喻频,不然半同步插件安裝失敗導(dǎo)致無法啟動。
3. MySQL 主從復(fù)制開發(fā)
1. 創(chuàng)建復(fù)制用戶并授權(quán)
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;