运行环境
系统版本:CentOS Linux release 7.3.1611 (Core)
软件版本:MYSQL-5.7
硬件要求:无
安装过程
1、基础配置
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
2、安装YUM-MYSQL存储库
YUM-MYSQL存储库由MYSQL官网提供。
[root@localhost ~]# rpm -i https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
3、安装MYSQL
[root@localhost ~]# yum -y install mysql-community-server
4、创建一个数据库实例,创建相关目录并授权
[root@localhost ~]# mkdir -p /data/mysql/data
[root@localhost ~]# mkdir -p /data/mysql/binlog
[root@localhost ~]# mkdir -p /data/mysql/relaylog
[root@localhost ~]# mkdir -p /data/mysql/log
[root@localhost ~]# chown -R mysql.mysql /data/mysql
5、创建一个数据库实例,创建配置文件
下面是一份经过性能优化的配置文件。
不了解的情况下我们只需要关注几个简单的基础配置即可。
[root@localhost ~]# vim /data/mysql/my.cnf
[mysqld]
### basic ###
user = mysql
# 运行用户
server_id = 1
# 主机ID
bind-address = 0.0.0.0
# 监听地址
port = 43306
# 监听端口
pid-file = /data/mysql/mysqld.pid
# 进程文件存放路径
socket = /data/mysql/mysqld.sock
# 套接字文件存放路径
datadir = /data/mysql/data
# 数据存储目录路径
default_storage_engine = InnoDB
# 数据库使用的默认存储引擎
# explicit_defaults_for_timestamp = true
symbolic-links = 0
### skip password ###
skip-grant-tables = false
### errorlog ###
log_error = /data/mysql/log/error.log
# 错误日志文件存储路径
### binlog ###
log-bin = /data/mysql/binlog/mysql-bin
# 开启二进制日志文件功能,设置二进制日志文件目录存储路径
log_bin_index = /data/mysql/binlog/mysql-bin.index
# 二进制日志文件列表索引文件存放路径
binlog_cache_size = 4MB
max_binlog_size = 200MB
# log_bin_trust_function_creators = 1
# expire_logs_days = 30
### relaylog ###
relay-log = /data/mysql/relaylog/mysql-relay-bin
relay_log_index = /data/mysql/relaylog/mysql-relay-bin.index
### slowlog ###
slow_query_log = ON
slow_query_log_file = /data/mysql/log/slow.log
long_query_time = 10
### network ###
back_log = 512
max_allowed_packet = 64MB
max_connections = 1024
max_connect_errors = 100
### buffer ###
# join_buffer_size = 1MB
# read_buffer_size = 1MB
# read_rnd_buffer_size = 1MB
# sort_buffer_size = 1MB
### query_cache ###
query_cache_type = ON
query_cache_size = 256MB
table_open_cache = 10000
thread_cache_size = 64
### innodb ###
innodb_log_group_home_dir = /data/mysql/data
innodb_data_home_dir = /data/mysql/data
innodb_data_file_path = ibdata1:1G:autoextend
# innodb_buffer_pool_size = 2GB
# innodb_buffer_pool_instances = 1
innodb_flush_method = O_DIRECT
innodb_flush_log_at_trx_commit = 2
innodb_log_file_size = 2GB
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16MB
innodb_read_io_threads = 4
innodb_write_io_threads = 4
innodb_lock_wait_timeout = 60
innodb_thread_concurrency = 4
innodb_open_files = 65535
open-files-limit = 65535
[root@localhost ~]# vim /etc/my.cnf
[mysql]
socket = /data/mysql/mysqld.sock
[mysqladmin]
socket = /data/mysql/mysqld.sock
[mysqldump]
socket = /data/mysql/mysqld.sock
6、创建一个数据库实例,初始化
初次运行,我们需要初始化数据库实例,生成基础数据库。
[root@localhost ~]# mysqld --defaults-file=/data/mysql/my.cnf --initialize
7、创建一个数据库实例,修改系统服务控制
将数据库实例服务交由系统管理工具“systemctl”托管。
[root@localhost ~]# vim /lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Community Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PermissionsStartOnly=true
PIDFile=/data/mysql/mysqld.pid
#ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld --defaults-file=/data/mysql/my.cnf --daemonize
TimeoutSec=600
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
8、创建一个数据库实例,启动服务
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# netstat -lnupt |grep mysqld
tcp 0 0 0.0.0.0:43306 0.0.0.0:* LISTEN 11748/mysqld
9、创建一个数据库实例,重置超级管理员密码
我们需要通过错误日志获取初始化时生成的临时密码。
密码必须符合复杂性要求,需要字母大小写+数字+特殊符号。
[root@localhost ~]# cat /data/mysql/log/error.log |grep password
2019-05-06T01:41:01.728543Z 1 [Note] A temporary password is generated for root@localhost: eAywyZq/?0!g
[root@localhost ~]# mysqladmin -uroot -p'eAywyZq/?0!g' password 'ABCabc-123'
10、访问测试
[root@localhost ~]# mysql -uroot -p'ABCabc-123' -A
mysql>