一、获取软件包
这里我们选择MySQL 社区版5.7.28。MySQL版本分为企业版和社区版, 企业版是收费的,而社区版是开源免费的。
下载地址:https://downloads.mysql.com/archives/community/
二、安装
1、二进制包安装
1、清理环境
[root@db01 ~]# yum remove -y mariadb-libs
2、创建用户
[root@db01 ~]# useradd mysql
3、创建相关目录并授权
[root@db01 ~]# mkdir -p /mysql/{app,data}
[root@db01 ~]# chown -R mysql.mysql /mysql
4、上传、解压缩、软链接
[root@db01 ~]# tar xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz -C /mysql/app/
[root@db01 ~]# cd /mysql/app/ && ln -s mysql-5.7.28-linux-glibc2.12-x86_64 mysql && cd
5、设置环境变量
[root@db01 ~]# vim /etc/profile //末尾添加
export PATH=/mysql/app/mysql/bin:$PATH
[root@db01 ~]# source /etc/profile
6、安装关键依赖软件包
[root@db01 ~]# yum install -y libaio-devel
7、初始化数据
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/mysql/app/mysql --datadir=/mysql/data
参数介绍:
--initialize-insecure 不安全初始化,即没有密码。
--initialize 安全初始化,会自动生成临时密码,4种密码复杂度,12位。
--user=mysql 初始化用户
--basedir=/mysql/app/mysql 软件安装目录
--datadir=/mysql/data 数据存放位置
注:5.7和5.6版本初始化命令不一样
5.7:
/data/app/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/mysql/app/mysql --datadir=/mysql/data
5.6:
/data/app/mysql/scripts/mysql_install_db --user=mysql --basedir=/mysql/app/mysql --datadir=/mysql/data
8、准备配置文件
[root@db01 ~]# vim /etc/my.cnf
[mysqld]
user=mysql
basedir=/mysql/app/mysql
datadir=/mysql/data
socket=/tmp/mysql.sock
9、启动
[root@db01 ~]# /mysql/app/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/mysql/data/db01.err'.
SUCCESS!
[root@db01 ~]# mysql
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>
注册到 systemd:
[root@db01 ~]# vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
ExecStart=/mysql/app/mysql/bin/mysqld --defaults-file=/etc/my.cnf
PIDFile=/mysql/data/mysqld.pid
Restart=on-failure
RestartPreventExitStatus=1
TimeoutSec=0
PrivateTmp=false
LimitNOFILE=65535
LimitNPROC=65535
[Install]
WantedBy=multi-user.target
[root@db01 system]# systemctl daemon-reload
[root@db01 system]# systemctl start mysql
2、yum安装mysql
安装数据库:
[root@db01 ~]# vim /etc/yum.repos.d/mysql.repo # 配置 mysql 5.7 系列版本官方源
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
[root@db01 ~]# yum install -y mysql-community-server
[root@db01 ~]# systemctl start mysqld
[root@db01 ~]# systemctl enable mysqld
登录:
[root@db01 ~]# grep 'temporary password' /var/log/mysqld.log # 查看 mysql 自动生成的临时密码
2021-01-07T08:14:29.977594Z 1 [Note] A temporary password is generated for root@localhost: <%1ivx6slrSJ
[root@db01 ~]# mysql -uroot -p'<%1ivx6slrSJ'
注意密码有复杂性要求,必须包含大小写字母、数字、特殊字符,密码长度至少8。
只安装客户端和工具:
yum install -y mysql-community-client