---
title: "安装 MySQL"
date: 2019-10-30T13:00:37+08:00
lastmod: 2024-07-05T20:36: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'
    # 输入查找到的密码
    ```

## 安装 MySQL5.7 通用二进制包
### 环境
- CentOS 7.9 x86_64 最小安装
- MySQL 5.7.44
- 普通用户 mortal

### 安装 mysql
- 下载 MySQL5.7.44 通用二进制包
    ```bash
    curl -LO https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
    ```

- 解压
    ```bash
    tar zxf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
    mv mysql-5.7.44-linux-glibc2.12-x86_64 /mysql57
    chown -R root.root /mysql57
    ```

- 创建普通用户 mortal，切换到 mortal 用户下
    ```bash
    useradd -m mortal
    echo -e 'mortal hard nofile 5120\nmortal soft nofile 5120\n' \
        > /etc/security/limits.d/mortal
    su - mortal
    ```

- 创建实例目录
    ```bash
    cd ~
    mkdir -p mysql-6033/{db,log,priv-files}
    ```

- 创建配置文件 mysql-6033/my.cnf，内容如下
    ```
    [mysqld]
    symbolic-links=0
    port = 6033
    basedir = /mysql57
    datadir = /home/lijc/mysql-6033/db
    socket = /home/lijc/mysql-6033/sock
    pid-file = /home/lijc/mysql-6033/pid
    secure-file-priv = /home/lijc/mysql-6033/priv-files
    log-timestamps = SYSTEM
    explicit_defaults_for_timestamp = 1
    log-error = /home/lijc/mysql-6033/log/error.log
    slow-query-log = TRUE
    slow-query-log-file = /home/lijc/mysql-6033/log/slow.log
    character-set-server = utf8mb4
    default-storage-engine = innodb
    lower-case-table-names = 1
    ```

### 初始化基础数据
- 初始化实例的数据目录
    ```bash
    /mysql57/bin/mysqld \
        --defaults-file=/home/mortal/mysql-6033/my.cnf \
        --initialize-insecure
    ```

### 启动 mysql
- 启动实例
    ```bash
    /mysql57/bin/mysqld --defaults-file=/home/mortal/mysql-6033/my.cnf --daemonize
    ```

### 设置 root 密码
- 设置 mysql root 账户的密码
    ```bash
    /mysql57/bin/mysqladmin -S /home/mortal/mysql-6033/sock password
    ```

### 客户端链接
- 使用本机客户端登陆 mysql root 账户
    ```bash
    /mysql57/bin/mysql -S /home/mortal/mysql-6033/mysqld.sock -p
    ```

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

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

- 下载 MySQL8 通用二进制包
    ```bash
    cd ~
    # 这是 debug 版本，软件包很大
    curl -LO https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.37-linux-glibc2.17-x86_64.tar.xz
    # 这是 strip 版本，软件包轻量，推荐
    curl -LO https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.37-linux-glibc2.17-x86_64-minimal.tar.xz
    ```

- 解压
    ```bash
    cd ~
    # debug 版本
    tar xf mysql-8.0.37-linux-glibc2.12-x86_64.tar.xz
    mv mysql-8.0.37-linux-glibc2.12-x86_64 mysql
    # strip 版本
    tar xf mysql-8.0.37-linux-glibc2.17-x86_64-minimal.tar.xz
    mv mysql-8.0.37-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
    ```

