1. MySQL 5.7.22安装

下载tar包

安装依赖

apt install libaio 

创建用户

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

创建目录

shell> mkdir -p /usr/local/mysql/{data,log,etc,run}
shell> chown -R mysql:mysql /usr/local/mysql
shell> chmod 750 /usr/local/mysql/{data,log,etc,run}

创建 my.cnf

[client]
port = 3306
socket = /usr/local/mysql/run/mysql.sock

[mysqld]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
pid_file = /usr/local/mysql/run/mysql.pid
basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data
default_storage_engine = InnoDB
max_allowed_packet = 128M
max_connections = 2048
open_files_limit = 65535

#skip-name-resolve
lower_case_table_names=1

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'


innodb_buffer_pool_size = 128M
innodb_log_file_size = 128M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0


key_buffer_size = 16M

log-error = /usr/local/mysql/log/mysql_error.log
log-bin = /usr/local/mysql/log/mysql_bin.log
slow_query_log = 1
slow_query_log_file =/usr/local/mysql/log/mysql_slow_query.log
long_query_time = 5


tmp_table_size = 16M
max_heap_table_size = 16M
query_cache_type = 0
query_cache_size = 0

server-id=1

拷贝my.cnf到/etc/下(拷贝后这个文件为root:root)

shell> cp /usr/local/mysql/etc/my.cnf /etc/

初始化

mysqld --initialize --user=mysql   --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

获取生成的临时密码

生成一个临时密码,可以在mysql_error.log文件找到

shell> grep 'temporary password' /usr/local/mysql/log/mysql_error.log 

配置服务

配置服务,使用systemctl管理

shell> cd /lib/systemd/system
shell> touch mysqld.service 

mysqld.service

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# systemd service file for MySQL forking server
#

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/usr/local/mysql/run/mysql.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysql.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 65535

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

加载配置服务

让systemctl加载配置服务

shell> systemctl daemon-reload
shell> systemctl enable mysqld.service (设置开机启动)
shell> systemctl is-enabled mysqld

启动MySQL服务

shell> systemctl start mysqld.service

MySQL用户初始化

  • 重置密码 (用到上面日志文件中生成的初始密码:ibF<RpDX!9wR)
>mysql -uroot -p系统生成的密码
SET PASSWORD = PASSWORD('123456')
  • 删除匿名用户
  • 关闭root用户的远程登录

删除测试数据库

shell> /usr/local/mysql/bin/mysql_secure_installation