一、下载

进入到mysql官网下载自己对应版本的mysql,

下载地址: https://dev.mysql.com/downloads/mysql/5.7.html#downloads

我这里下载mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz版本

也可以进入linux后用命令下载

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

二、环境准备

安装前,我们可以检测系统是否自带安装 MySQL:

rpm -qa | grep mysql

如有,类似

mysql-libs-5.1.52-1.el6_0.1.x86_64

那可以选择进行卸载:

rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64  // 普通删除模式
rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64  // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

检查否存在 mariadb 数据库,如有,卸载之,卸载同上

rpm -qa | grep mariadb

如有,类似

mariadb-libs-5.5.56-2.el7.x86_64

则 卸载

rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

用工具将下载的mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz上传到linux服务器的/opt/目录下面

三、安装

3.1 解压文件

进入安装包所在目录,解压文件

重命名

mv mysql-5.7.23-linux-glibc2.12-x86_64 mysql

3.2 添加系统mysql组和mysql用户

检查mysql组和用户是否存在,如无创建

1
2
3
4
5
6
cat /etc/group | grep mysql
类似
mysql:x:490:
cat /etc/passwd | grep mysql
#类似
mysql:x:496:490::/home/mysql:/bin/bash

以上为存在的情况,如无,执行添加命令:

1
2
3
groupadd mysql
useradd -r -g mysql mysql
#useradd -r参数表示mysql用户是系统用户,不可用于登录系统

3.3 安装数据库

创建data目录

cd mysql
mkdir data

将/opt/mysql的所有者及所属组改为mysql

chown -R mysql.mysql /opt/mysql

在/opt/mysql目录下创建my.cnf

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[client]
# 设置mysql客户端默认字符集
default-character-set = utf8

[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir = /opt/mysql
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir = /opt/mysql/data
# 允许最大连接数
max_connections = 1000
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server = utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine = INNODB

#federated
federated

# 大小写忽略
lower_case_table_names = 1

log-bin-trust-function-creators = 1
long_query_time = 1
log-queries-not-using-indexes
slow_query_log = 1
slow_query_log_file = /opt/mysql/slow_query_log.log
log-bin = mysql-bin
log_timestamps = SYSTEM
log-error = /opt/mysql/error.log
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 60
expire_logs_days = 7
back_log = 128

explicit_defaults_for_timestamp = true
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
master_info_repository = TABLE
relay_log_info_repository = TABLE

max_connect_errors = 40
wait_timeout = 60
interactive_timeout = 7200

innodb_buffer_pool_size = 2048M
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 128M
innodb_log_files_in_group =3

max_binlog_size = 512M
max_allowed_packet = 32M
key_buffer_size = 256M
sort_buffer_size = 2M
join_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M

query_cache_size = 256M
query_cache_type = 1
query_cache_limit = 50M
thread_cache_size = 200
binlog_cache_size = 2M
max_binlog_cache_size = 8M

tmp_table_size = 32M
max_heap_table_size = 32M

gtid_mode = on
enforce_gtid_consistency = 1
event_scheduler = on
server-id = 1

初始化 mysqld

./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/

如下图,临时密码请记住:

把启动脚本放到开机初始化目录

cp support-files/mysql.server /etc/init.d/mysql

修改/etc/init.d/mysql如下:

启动mysql服务

service mysql start

登录mysql,密码为初始密码

./bin/mysql -u root -p

修改密码

mysql> set password=password('123456');
mysql> grant all privileges on . to root@'%' identified by '123456';
mysql> flush privileges;

如果提示如下错误:

1
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

则需要执行下面语句修改密码:

mysql> alter user user() identified by "123456";

添加远程访问权限

mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;

重启mysql生效

service mysql restart