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;