一、安装mysql-5.7.31
(1) 下载安装包
#下载前先卸载mariadb的一些东西
yum remove -y mariadb*
#然后下载安装包
cd /usr/local/soft
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
(2) 解压
#解压
tar -xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
#创建软链接
ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql-5.7.31
(3) 新建mysql用户以及数据目录
useradd -r -s /sbin/nologin mysql
mkdir -pv /usr/local/soft/mysql/{data,logbin,logs}
(4) 准备配置文件
vim /etc/my.cnf
#参考以下配置
[client]
port = 3306
socket = /usr/local/soft/mysql/mysql.sock
[mysqld]
##########################################
port = 3306
bind-address = 0.0.0.0
character-set-server=utf8mb4
default-storage-engine=innoDB
basedir=/usr/local/soft/mysql-5.7.31
datadir=/usr/local/soft/mysql/data
socket=/usr/local/soft/mysql/mysql.sock
log-error=/usr/local/soft/mysql/logs/mysqld-error.log
slow_query_log_file=/usr/local/soft/mysql/logs/slow.log
default_authentication_plugin=mysql_native_password
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
###
skip_name_resolve = 1
default_time_zone = "+8:00"
##########################################
open_files_limit = 65535
back_log = 300
max_connections = 1000
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
skip-external-locking
max_allowed_packet = 32M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 96M
max_heap_table_size = 8M
log_timestamps = SYSTEM
##########################################
slow_query_log=1
long_query_time=1
log-queries-not-using-indexes = TRUE
log_throttle_queries_not_using_indexes = 60
log_queries_not_using_indexes = 1
min_examined_row_limit = 100
log-slow-admin-statements = TRUE
log-slow-admin-statements = TRUE
##########################################
#disable-log-bin
log-bin=/usr/local/soft/mysql/logbin/mysql-bin
log-bin-index=/usr/local/soft/mysql/logbin/mysql-bin.index
server-id=3306
sync_binlog=1
binlog_cache_size = 4M
max_binlog_size = 1G
max_binlog_cache_size = 2G
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
slave-rows-search-algorithms = ‘INDEX_SCAN,HASH_SCAN‘
log_slave_updates = 0
binlog_format = ROW
binlog_checksum = 1
relay_log_recovery = 1
relay-log-purge = 1
##########################################
key_buffer_size = 64M
read_buffer_size = 8M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
explicit_defaults_for_timestamp = 1
##########################################
transaction-isolation = READ-COMMITTED
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
innodb_buffer_pool_size = 5120M
innodb_buffer_pool_instances = 4
innodb_buffer_pool_load_at_startup = on
innodb_buffer_pool_dump_at_shutdown = on
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_io_capacity = 5000
innodb_io_capacity_max = 10000
innodb_flush_sync = 0
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 2
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 85
innodb_flush_method=O_DIRECT
innodb_lru_scan_depth = 4000
innodb_lock_wait_timeout = 120
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_online_alter_log_max_size = 4G
innodb_stats_on_metadata = 0
innodb_undo_log_truncate = 1
innodb_file_per_table = 1
innodb_deadlock_detect = on
innodb_autoinc_lock_mode = 2
innodb_status_file = 1
##########################################
log_error_verbosity = 3
#innodb_print_ddl_logs = 1
#binlog_expire_logs_seconds = 604800
#innodb_dedicated_server = 0
innodb_status_output = 0
innodb_status_output_locks = 0
##########################################
max_relay_log_size = 512M
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
##########################################
connect_timeout = 10
ft_min_word_len = 1
net_retry_count = 100
log_bin_trust_function_creators = 1
##########################################
[mysqldump]
quick
max_allowed_packet = 32M
##########################################
[mysqld_safe]
user=mysql
log-error=/usr/local/soft/mysql/logs/mysql.log
pid-file=/usr/local/soft/mysql/mysql.pid
(5) 初始化数据库
#初始化前配置
touch /usr/local/soft/mysql/logbin/mysql-bin.index
touch /usr/local/soft/mysql/logs/mysql.log
chown -R mysql:mysql /usr/local/soft/mysql
#开始初始化
/usr/local/soft/mysql-5.7.31/bin/mysqld --initialize --user=mysql --basedir=/usr/local/soft/mysql-5.7.31 --datadir=/usr/local/soft/mysql/data --lower-case-table-names=1
(6) 配置环境变量
vim /etc/profile
export PATH=/usr/local/soft/mysql-5.7.31/bin:$PATH
(7) 准备服务启动脚本,并启动数据库服务
#准备启动脚本
cp /usr/local/soft/mysql-5.7.31/support-files/mysql.server /etc/init.d/mysqld
#设置开机启动
chkconfig --add mysqld
#启动
/etc/init.d/mysqld start
(8) 登录数据库,并修改密码
#查询root初始化密码
grep password /usr/local/soft/mysql/logs/mysqld-error.log
#登录,输上面命令查询到的密码
mysql -uroot -p
#修改root密码
> alter user user() identified by ‘PASSWORD‘;
#添加用于远程连接的用户
> create user ‘admin‘@‘%‘ identified by ‘PASSWORD‘;
> grant all privileges on *.* to ‘admin‘@‘%‘;
> flush privileges;
二、主从同步实现
(1) 按以上方式再安装一台mysql主机作为从节点。注意修改my.cnf中的server-id值,不要与主节点相同
vim /etc/my.cnf
...
server-id=3307
...
(2) 在主节点上新建一个用于同步的账号并授权
> grant replication slave on *.* to repl@‘%‘ identified by ‘PASSWORD‘;
> flush privileges;
(3) 查看主节点的二进制日志及位置
> show master logs;
(4) 从节点上配置同步
> CHANGE MASTER TO
MASTER_HOST= ‘主节点IP‘ ,
MASTER_PORT=3306,
MASTER_USER= ‘repl‘ ,
MASTER_PASSWORD= ‘PASSWORD‘ ,
MASTER_LOG_FILE= ‘主节点日志文件名‘ ,
MASTER_LOG_POS=主节点日志文件复制点;
(5) 开启同步
#开启同步
> start slave;
#查看状态
> show slave status\G;