软件下载:https://downloads.mysql.com/archives/community/
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
修改主机名
hostnamectl set-hostname master
环境准备
1 安装依赖
yum install libaio numactl -y
2 创建用户以及用户组
groupadd mysql
useradd -m -r -g mysql mysql
3 配置 ulimit
我们一般使用 mysql 用户启动数据库,如果不设置文件打开数和进程数,后期会有问题
打开 /etc/security/limits.conf 文件,在文件末尾写上
mysql soft nproc 65536
mysql hard nproc 65536
mysql soft nofile 65536
mysql hard nofile 65536
打开 /etc/security/limits.d/90-nproc.conf 把参数调整为
- soft nproc 65536
root soft nproc unlimited - soft nofile 65536
root soft nofile unlimited
作为生产环境安装首先要调整操作系统环境:
调整文件句柄和进程数
vim /etc/systemd/system.conf centos7调整如下2个值
DefaultLimitNOFILE=
DefaultLimitNOFILE=655350
DefaultLimitAS=
DefaultLimitNPROC=
DefaultLimitNPROC=655350
内核参数优化
少用交换分区
vm.swappiness <= 5
vm.dirty_ratio <= 20
vm.dirty_background_ratio <= 10
net.ipv4.tcp_max_syn_backlog = 819200
net.core.netdev_max_backlog = 400000
net.core.somaxconn = 4096
解决time_wait过高导致数据库连接不上
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
禁用selinux 和关闭防火墙
文件系统mysql推荐使用xfs文件系统
io调度 SAS :deadline SSD&PCI-E: noop
4 在 hosts 文件加上本机解析
如果不设置 hosts 本机解析,偶然会出现一些诡异的问题,所以还是加上比较好
host_ip=$(ifconfig | grep inet | grep cast | awk '{print $2}' | awk -F: '{print $NF}' | head -1)
echo "$host_ip hostname
" >> /etc/hosts
6 关闭 iptable
数据库一般不允许通外网,如果对防火墙不是很熟悉,可以关掉; 如果网络环境不够安全,可以开启
service iptables stop
chkconfig iptables off
7 关闭 selinux
selinux 可能会造成无法写入数据,如果对它不熟悉,建议关掉
查看 selinux 状态
getenforce
临时关闭
setenforce 0
永久关闭
打开 /etc/sysconfig/selinux
SELINUX=enforcing 改为 SELINUX=disabled
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
安装
这次安装数据目录为 /data/mysql_db/,日志目录为 /data/mysql_log/,实例名为 mysql_test,启动用户为 mysql
1 解压文件
把二进制文件解压出来,放到 /user/local/ 目录下
tar -xvzf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql80
mysql配置环境变量
export MYSQL_HOME=/usr/local/mysql80
export PATH=${MYSQL_HOME}/bin:$PATH
PS1=uname -n
":"'$USER'":"'$PWD'":>"; export PS1
2 创建数据和日志目录
mkdir -p /data/mysql_db
mkdir -p /data/mysql_log
3 初始化目录
/usr/local/mysql80/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql80 --datadir=/data/mysql_db --user=mysql ## --initialize生成随机密码,--initialize-insecure不生成密码##
mysql8.0需要初始化是加上不区分大小写
/usr/local/mysql80/bin/mysqld --initialize-insecure --lower-case-table-names=1 --basedir=/usr/local/mysql80 --datadir=/data/mysql_db --user=mysql ## --initialize生成随机密码,--initialize-insecure不生成密码##
/usr/local/mysql80/bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql80 --datadir=/data/mysql_db
4 生成配置文件
一台机器上有可能会存在多个 MySQL 实例,我们可以把 my.cnf 文件放到对应实例的数据目录下,由于 MySQL 会受到默认配置文件的影响,所以我们要把 /etc/my.cnf 移走, 然后创建新的 my.cnf 文件
rm -f /etc/my.cnf
vim /data/mysql_db/my.cnf
本次安装参考配置,具体参数结合服务器情况调整
[client]
port = 3306
socket = /data/mysql_db/mysql.sock
[mysqld]
default-character-set =utf8mb4
datadir = /data/mysql_db
basedir = /usr/local/mysql80
tmpdir = /tmp
socket = /data/mysql_db/mysql.sock
pid-file = /data/mysql_db/mysql.pid
skip-external-locking = 1
skip-name-resolve = 1
port = 3306
server_id = 8201
lower_case_table_names = 1
innodb_large_prefix=1
innodb_file_format=Barracuda
max_connections=3000
default-storage-engine = InnoDB
character-set-server = utf8mb4
default_password_lifetime=0
log
binlog_cache_size = 16M
log_bin = /data/mysql_log/mysql-bin
log_bin_index = /data/mysql_log/mysql-bin.index
binlog_format = row
expire_logs_days = 15
relay_log_recovery=ON
relay_log=/data/mysql_log/mysql-relay-bin
relay_log_index=/data/mysql_log/mysql-relay-bin.index
log_error = /data/mysql_log/mysql-error.log
log_queries_not_using_indexes = 1
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql_log/mysql-slow.log
innodb
innodb_buffer_pool_size = 4G
innodb_buffer_pool_instances = 8
innodb_log_group_home_dir = /data/mysql_log/
innodb_undo_directory = /data/mysql_log/
innodb_undo_logs = 128
innodb_flush_neighbors = 1
innodb_log_file_size = 1G
innodb_file_per_table = on
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1
log_timestamps=system
[mysqldump]
quick
max_allowed_packet = 64M
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 32M
read_buffer = 16M
write_buffer = 16M
[mysql]
prompt="\u@\h \R:\m:\s [\d]> "
5 生成启动文件
cp /usr/local/mysql80/support-files/mysql.server /etc/init.d/mysqld
vim /etc/init.d/mysqld
把
basedir=
datadir=
改为, 手动指定配置文件和数据目录
basedir=/usr/local/mysql80
datadir=/data/mysql_db
把数据目录的所有者改为 mysql
chown mysql.mysql /etc/init.d/mysqld
chown mysql.mysql -R /data/mysql*
启动实例
以 mysql 用户来启动
su - mysql
[mysql@chengqm ~]$ /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
进入 mysql 客户端
mysql -S /data/mysql_db/mysql.sock