0. 准备工作
- 下载MySQL安装包
官网下载地址:https://downloads.mysql.com/archives/community/
清华大学镜像站:https://mirrors.tuna.tsinghua.edu.cn/mysql/
我这里使用mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar
和mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
- 卸载系统自带的MySQL
# 查询系统自带的MySQL
[root@mysql01 ~]# rpm -qa | grep -i mysql
[root@mysql01 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
# 卸载查询到的旧应用
[root@mysql01 ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
# 删除旧应用使用过的目录和配置文件
[root@mysql01 ~]# rm -rf /usr/lib/mysql /usr/include/mysql /etc/my.cnf /var/lib/mysql
- 安装依赖的软件
yum install libaio -y
1. 使用rpm包安装MySQL
1.1 rpm压缩包解包
[root@mysql01 ~]# tar -xvf mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar
# 解包后会出现10个文件mysql-community-client-5.7.25-1.el7.x86_64.rpm
mysql-community-common-5.7.25-1.el7.x86_64.rpm
mysql-community-devel-5.7.25-1.el7.x86_64.rpm
mysql-community-embedded-5.7.25-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.25-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
mysql-community-server-5.7.25-1.el7.x86_64.rpm
mysql-community-test-5.7.25-1.el7.x86_64.rpm
1.2 安装rpm包
# 按以下顺序安装,其余的rpm包可以不安装
[root@mysql01 ~]# rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm
[root@mysql01 ~]# rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm
[root@mysql01 ~]# rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm
[root@mysql01 ~]# rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm
1.3 启动MySQL服务
[root@mysql01 ~]# systemctl start mysqld
# 设置MySQL开机自启动
sudo systemctl enable mysqld
# 停止MySQL服务
sudo systemctl stop mysqld
# 查看MySQL服务状态
sudo systemctl status mysqld
1.4 登陆MySQL
# 查看临时密码
[root@mysql01 ~]# grep 'temporary password' /var/log/mysqld.log
2020-04-28T11:25:22.879776Z 1 [Note] A temporary password is generated for root@localhost: 80)?wcoS(mrB
[root@mysql01 ~]# mysql -uroot -p"80)?wcoS(mrB"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
到这里,MySQL就安装成功了,还有一些通用设置,在本文"4.通用设置"相关内容中说明。
1.5 卸载MySQL
如果要卸载的话,执行以下操作:
[root@mysql01 ~]# systemctl stop mysqld
[root@mysql01 ~]# rpm -e --nodeps mysql-community-server-5.7.25-1.el7.x86_64
[root@mysql01 ~]# rpm -e --nodeps mysql-community-libs-5.7.25-1.el7.x86_64
[root@mysql01 ~]# rpm -e --nodeps mysql-community-client-5.7.25-1.el7.x86_64
[root@mysql01 ~]# rpm -e --nodeps mysql-community-common-5.7.25-1.el7.x86_64
[root@mysql01 ~]# rm -rf /etc/my.*
[root@mysql01 ~]# rm -rf /var/lib/mysql
[root@mysql01 ~]# rm -rf /var/log/mysqld.log
[root@mysql01 ~]# rm -rf /var/run/mysqld
[root@mysql01 ~]# userdel -r mysql
2. 使用yum安装MySQL
使用yum安装,如果不做任何配置,那么安装的MySQL版本会很低,按照以下方式来安装MySQL-5.7的最新版:
[root@mysql01 ~]# yum -y localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
[root@mysql01 ~]# yum -y install mysql-community-server
安装完成之后启动服务,查看临时密码,登陆MySQL以及MySQL服务启停的方式和使用rpm包安装之后的操作完全一样,卸载操作也都是一样的
[root@mysql01 ~]# rpm -qa | grep mysql
mysql-community-libs-5.7.30-1.el7.x86_64
mysql57-community-release-el7-8.noarch
mysql-community-common-5.7.30-1.el7.x86_64
mysql-community-client-5.7.30-1.el7.x86_64
mysql-community-server-5.7.30-1.el7.x86_64
[root@mysql01 ~]# rpm -e --nodeps mysql-community-libs-5.7.30-1.el7.x86_64
[root@mysql01 ~]# rpm -e --nodeps mysql57-community-release-el7-8.noarch
[root@mysql01 ~]# rpm -e --nodeps mysql-community-common-5.7.30-1.el7.x86_64
[root@mysql01 ~]# rpm -e --nodeps mysql-community-client-5.7.30-1.el7.x86_64
[root@mysql01 ~]# rpm -e --nodeps mysql-community-server-5.7.30-1.el7.x86_64
[root@mysql01 ~]# rm -rf /etc/my.*
[root@mysql01 ~]# rm -rf /var/lib/mysql
[root@mysql01 ~]# rm -rf /var/log/mysqld.log
[root@mysql01 ~]# rm -rf /var/run/mysqld
[root@mysql01 ~]# userdel -r mysql
3. 使用二进制压缩包安装MySQL
-
解压,创建软链接
[root@mysql01 local]# tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz [root@mysql01 local]# mv mysql-5.7.28-linux-glibc2.12-x86_64 mysql-5.7.28 [root@mysql01 local]# ln -s mysql-5.7.28 mysql [root@mysql01 local]# ll | grep mysql lrwxrwxrwx 1 root root 12 2020-04-29 10:24 mysql -> mysql-5.7.28 drwxr-xr-x 9 root root 129 2020-04-29 10:23 mysql-5.7.28
-
创建mysql用户和组
[root@mysql01 mysql]# groupadd mysql [root@mysql01 mysql]# useradd -r -g mysql mysql
-
创建相关目录并修改属主
[root@mysql01 mysql]# mkdir -p /data/mysql [root@mysql01 mysql]# mkdir -p /var/lib/mysql [root@mysql01 mysql]# mkdir -p /var/run/mysqld [root@mysql01 mysql]# chown -R mysql.mysql /usr/local/mysql [root@mysql01 mysql]# chown -R mysql.mysql /data/mysql [root@mysql01 mysql]# chown -R mysql.mysql /var/lib/mysql [root@mysql01 mysql]# chown -R mysql.mysql /var/run/mysqld
-
编辑
/etc/my.cnf
文件,内容如下[client] default-character-set = utf8mb4 [mysql] socket = /var/lib/mysql/mysql.sock default-character-set = utf8mb4 [mysqld] server-id = 1 port = 3306 user = mysql socket = /var/lib/mysql/mysql.sock basedir = /usr/local/mysql datadir = /data/mysql character_set_server = utf8mb4 character-set-client-handshake = FALSE collation-server = utf8mb4_unicode_ci default-storage-engine = INNODB log_error = error.log max_connections = 800 max_connect_errors = 1000
-
初始化数据库
# 这样初始化数据库之后 # 会生成临时密码(显示到屏幕上,并且会往日志中记一份) # 有密码复杂度校验,长度:超过12位,复杂度:字符数字混乱组合 # 密码过期时间180天 bin/mysqld --initialize --user=mysql # 测试的时候,为了避免以上繁琐的安全机制,使用以下命令初始化 # 不生成临时密码 # 不对密码进行复杂性校验 # 密码不会自动过期 bin/mysqld --initialize-insecure --user=mysql # 初始化之后在提前设置的data目录会生成如下文件 # 要检查error.log查看其中是否有错误信息 root@mysql01 mysql]# bin/mysqld --initialize-insecure --user=mysql [root@mysql01 mysql]# ll /data/mysql/ total 110664 -rw-r----- 1 mysql mysql 56 2020-04-29 11:26 auto.cnf -rw------- 1 mysql mysql 1680 2020-04-29 11:26 ca-key.pem -rw-r--r-- 1 mysql mysql 1112 2020-04-29 11:26 ca.pem -rw-r--r-- 1 mysql mysql 1112 2020-04-29 11:26 client-cert.pem -rw------- 1 mysql mysql 1676 2020-04-29 11:26 client-key.pem -rw-r----- 1 mysql mysql 931 2020-04-29 11:26 error.log -rw-r----- 1 mysql mysql 425 2020-04-29 11:26 ib_buffer_pool -rw-r----- 1 mysql mysql 12582912 2020-04-29 11:26 ibdata1 -rw-r----- 1 mysql mysql 50331648 2020-04-29 11:26 ib_logfile0 -rw-r----- 1 mysql mysql 50331648 2020-04-29 11:26 ib_logfile1 drwxr-x--- 2 mysql mysql 4096 2020-04-29 11:26 mysql drwxr-x--- 2 mysql mysql 8192 2020-04-29 11:26 performance_schema -rw------- 1 mysql mysql 1676 2020-04-29 11:26 private_key.pem -rw-r--r-- 1 mysql mysql 452 2020-04-29 11:26 public_key.pem -rw-r--r-- 1 mysql mysql 1112 2020-04-29 11:26 server-cert.pem -rw------- 1 mysql mysql 1680 2020-04-29 11:26 server-key.pem drwxr-x--- 2 mysql mysql 8192 2020-04-29 11:26 sys
-
启动MySQL服务
[root@mysql01 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # 修改/etc/init.d/mysqld文件 # 这两行配置的值默认是空的,改为提前设置好的mysql目录和mysql数据目录 basedir=/usr/local/mysql datadir=/data/mysql [root@mysql01 mysql]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! # 停止MySQL服务 # /etc/init.d/mysqld stop # 重启MySQL服务 # /etc/init.d/mysqld restart
-
设置MySQL开机自启动
chkconfig --add mysqld chkconfig --list mysqld
-
登陆MySQL
# 不用输入密码,直接回车 [root@mysql01 mysql]# bin/mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.28 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
-
配置环境变量
# /etc/profile export MYSQL_HOME=/usr/local/mysql export PATH=$MYSQL_HOME/bin:$PATH [root@mysql01 mysql]# source /etc/profile
4. 通用设置
- 修改root密码以及允许root用户远程连接,登陆到MySQL之后,执行以下SQL
# MySQL-5.7有复杂的密码校验,如果想设置简单密码,需要执行以下语句
# 使用.tar.gz包安装的时候,如果使用mysqld --initialize-insecure命令进行的初始化,则不需要执行以下语句
set global validate_password_policy=0;
set global validate_password_mixed_case_count=0;
set global validate_password_number_count=3;
set global validate_password_special_char_count=0;
set global validate_password_length=3;
# 使用root用户登陆系统,执行此SQL就是修改root用户密码
mysql> SET PASSWORD = '123456';
mysql> USE mysql;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> select user, host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
# 删除多余用户,同一个user只留host=%的记录即可
mysql> drop user 'root'@'localhost';
mysql> select user, host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
+---------------+-----------+
mysql> FLUSH PRIVILEGES;