- 安装依赖包
#yum -y install libaio libaio-devel
- 更改服务区时区
由于是国内的业务设置为中国的时区
#cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- 定时同步时间保证服务器时间同步
#yum install -y ntpdate
#crontab -e
* */1 * * * /usr/sbin/ntpdate pool.ntp.org && /sbin/hwclock -w
- 添加mysql用户组和用户
#groupadd mysql
#useradd -g mysql -d /usr/local/mysql -s /sbin/nologin -M -n mysql
- 关闭swap
用于提高系统性能
#vim /etc/sysctl.conf
在最后一行加上vm.swappiness=0
#sysctl -p
- 创建目录
#mkdir -p /opt/mysql/ && cd /opt/mysql/
#mkdir -p /data/db/mysql/{3316/{data,logs,tmp},backup,scripts/log}
#chown -R mysql:mysql /data/db/mysql
#chown -R mysql:mysql /opt/mysql/mysql-5.7.23-linux-glibc2.12-x86_64/
- 下载并解压
#wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
#tar -zvxf /opt/mysql/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
#rm -r /opt/mysql/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
#ln -s /opt/mysql/mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql
#chown -R mysql:mysql /usr/local/mysql
- 创建配置文件
#touch /etc/my.cnf
#chmod 777 /etc/my.cnf
#vim /etc/my.cnf
配置文件内容如下:
[client]
port = 3316
socket = /tmp/mysql3316.sock
[mysql]
prompt="\\u@HOSTNAME \\R:\\m:\\s [\\d]> "
no-auto-rehash
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/db/mysql/3316/data
port = 3316
socket = /tmp/mysql3316.sock
character-set-server = utf8mb4
skip_name_resolve = 1
open_files_limit = 65535
back_log = 600
max_connections =1000
max_connect_errors = 1000000
table_open_cache = 1000
secure_file_priv=/tmp/
max_allowed_packet = 256M
lower_case_table_names =1
event_scheduler = 0
default_password_lifetime=0
tmpdir = /data/db/mysql/3316/tmp
log_timestamps=system
explicit_defaults_for_timestamp=1
interactive_timeout = 1800
wait_timeout = 1800
lock_wait_timeout = 3600
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
thread_cache_size = 32
tmp_table_size = 32M
key_buffer_size = 8M
read_buffer_size = 4M
read_rnd_buffer_size = 8M
bulk_insert_buffer_size = 8M
sort_buffer_size = 256K
join_buffer_size = 256K
slow_query_log = 1
slow_query_log_file = slow.log
log-error = error.log
log_warnings = 2
pid-file = mysql.pid
long_query_time = 2
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
server-id = 3316
log-bin = /data/db/mysql/3316/logs/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_size = 1G
max_binlog_cache_size = 2G
expire_logs_days = 7
plugin_dir=/usr/local/mysql/lib/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 = 3000
gtid-mode = on
enforce-gtid-consistency=1
log_slave_updates
binlog_format = row
skip_slave_start = 1
max_relay_log_size = 128M
relay_log_purge = 1
relay-log=relay-bin
relay-log-index=relay-bin.index
myisam_repair_threads = 1
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 1G
innodb_file_per_table = 1
innodb_print_all_deadlocks = 1
innodb_stats_on_metadata = 0
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT
innodb_buffer_pool_size = 8G
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 1G
innodb_log_files_in_group = 3
innodb_log_group_home_dir = /data/db/mysql/3316/data
innodb_undo_directory = /data/db/mysql/3316/data
innodb_undo_logs = 128
innodb_undo_tablespaces = 3
innodb_undo_log_truncate = 1
innodb_flush_neighbors = 0
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=8
master_info_repository = TABLE
relay_log_info_repository = TABLE
relay_log_recovery = 1
binlog_group_commit_sync_delay=1000
binlog_group_commit_sync_no_delay_count=50
slave_preserve_commit_order = ON
show_compatibility_56=on
[mysqldump]
quick
max_allowed_packet = 32M
#chmod 644 /etc/my.cnf
- 配置mysql环境变量
#echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
#source /etc/profile
- 初始化mysql
#echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
#source /etc/profile
- 启动mysql
#/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
- 首次登陆mysql
##查看root初始密码
#cat /data/db/mysql/3316/data/error.log |grep "root@localhost"|awk -F " " '{print $11}' |head -n 1
## 登陆mysql
#mysql -uroot -p -P3316 -S /tmp/mysql3316.sock
##修改root密码
#alter user user() identified by 'yzw@com';
##重新登陆mysql
#mysql -uroot -p'yzw@com' -P3316 -S /tmp/mysql3316.sock
- 创建数据库
> CREATE DATABASE mohurd;
> create user 'test'@'%' identified by 'test@123456';
> grant all privileges on mohurd.* to mohurd@'%';
> flush privileges;
- 设置开机启动
#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#echo "/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &" >>/etc/rc.local
#chmod +x /etc/rc.d/rc.local
- 开放mysql端口
#firewall-cmd --get-active-zones
#firewall-cmd --zone=public --add-port=3316/tcp --permanent
#firewall-cmd --reload
- 主从搭建需要注意的点
配置文件中的一下配置 server-id 每台机器不同 注释掉 skip_slave_start = 1