---
title: "安装 MySQL"
date: 2019-10-30T13:00:28+08:00
lastmod: 2022-03-28T20:00:00+08:00
tags: ["centos", "yum", "mysql"]
categories: ["database"]
---

## CentOS7 yum 安装 MySQL5.7
### 环境
- CentOS 7.4 x86_64 最小安装
- MySQL 5.7.20

### 安装 mysql yum 源
- 下载 rpm 包
    ```bash
    # repo 地址: https://repo.mysql.com/
    cd /root/
    curl -O https://repo.mysql.com/mysql57-community-release-el7.rpm
    ```

- 安装 mysql 源
    ```bash
    rpm -ivh /root/mysql57-community-release-el7.rpm
    ```

- 更新 yum 缓存
    ```bash
    yum clean all
    yum makecache fast
    ```

### 安装 mysql
- yum 安装 mysql 包
    ```bash
    yum install mysql-community-server
    ```

- 启动 mysql
    ```bash
    systemctl start mysqld
    ```

- 查找 mysql 默认 root 密码
    ```bash
    grep 'temporary password' /var/log/mysqld.log
    ```

- 修改 root 密码
    ```bash
    mysqladmin -uroot -p password 'Pass-1234'
    # 输入查找到的密码
    ```

## 安装 MySQL8 通用二进制包
### 环境
- CentOS 7.9 x86_64 最小安装
- MySQL 8.0.28 多实例
- 普通用户 mortal

### 安装 mysql
- 创建普通用户 mortal，切换到 mortal 用户下
    ```bash
    useradd -m mortal
    su - mortal
    ```

- 下载 MySQL8 通用二进制包
    ```bash
    cd ~
    # 这是 debug 版本，软件包很大
    curl -LO https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
    # 这是 strip 版本，软件包轻量，推荐
    curl -LO https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz
    ```

- 解压
    ```bash
    cd ~
    # debug 版本
    tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
    mv mysql-8.0.28-linux-glibc2.12-x86_64 mysql
    # strip 版本
    tar xf mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz
    mv mysql-8.0.28-linux-glibc2.17-x86_64-minimal mysql
    ```

- 创建三个实例的目录
    ```bash
    cd ~
    mkdir -p instance-{3301..3303}/{file,log}
    ```

- 创建各实例的配置文件 instance-{3301..3303}/my.cnf
    ```bash
    cd ~
    for i in {3301..3303}; do
        cat > instance-$i/my.cnf <<-EOF
    [mysqld]
    mysqlx = OFF
    port = $i
    basedir = /home/mortal/mysql
    datadir = /home/mortal/instance-$i/db
    socket = /home/mortal/instance-$i/mysqld.sock
    pid-file = /home/mortal/instance-$i/mysqld.pid
    secure-file-priv = /home/mortal/instance-$i/file
    log-timestamps = SYSTEM
    log-error = /home/mortal/instance-$i/log/error.log
    slow-query-log = TRUE
    slow-query-log-file = /home/mortal/instance-$i/log/slow.log
    character-set-server = utf8mb4
    default-storage-engine = innodb
    #default-authentication-plugin = mysql_native_password
    authentication-policy = mysql_native_password
    lower-case-table-names = 1
    EOF
    done
    ```

### 初始化基础数据
- 初始化每个实例的数据目录
    ```bash
    cd ~
    mysql/bin/mysqld \
        --defaults-file=/home/mortal/instance-3301/my.cnf \
        --initialize-insecure
    mysql/bin/mysqld \
        --defaults-file=/home/mortal/instance-3302/my.cnf \
        --initialize-insecure
    mysql/bin/mysqld \
        --defaults-file=/home/mortal/instance-3303/my.cnf \
        --initialize-insecure
    ```

### 启动 mysql
- 启动每个实例的 mysql
    ```bash
    cd ~
    mysql/bin/mysqld --defaults-file=/home/mortal/instance-3301/my.cnf --daemonize
    mysql/bin/mysqld --defaults-file=/home/mortal/instance-3302/my.cnf --daemonize
    mysql/bin/mysqld --defaults-file=/home/mortal/instance-3303/my.cnf --daemonize
    ```

### 设置 root 密码
- 设置 mysql root 账户的密码
    ```bash
    cd ~
    mysql/bin/mysqladmin -S /home/mortal/instance-3301/mysqld.sock password
    mysql/bin/mysqladmin -S /home/mortal/instance-3302/mysqld.sock password
    mysql/bin/mysqladmin -S /home/mortal/instance-3303/mysqld.sock password
    ```

### 客户端链接
- 使用本机客户端登陆 mysql root 账户
    ```bash
    cd ~
    mysql/bin/mysql -S /home/mortal/instance-3301/mysqld.sock -p
    mysql/bin/mysql -S /home/mortal/instance-3302/mysqld.sock -p
    mysql/bin/mysql -S /home/mortal/instance-3303/mysqld.sock -p
    ```

