UP | HOME

源码安装 MySQL 5.7 数据库

Table of Contents

1 准备工作

1.1 下载安装文件

MySQL 5.7 版本依赖于 boost 库,需要在官网上下载并安装对应的 mysql 源代码包和 boost 库

  1. mysql-5.7.26.tar.gz
  2. mysql-boost-5.7.26.tar.gz
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.26.tar.gz
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.26.tar.gz

安装包校验码如下

mysql57 $ md5sum mysql-5.7.26.tar.gz
5756a63d37d343a39e8e2cf8b13378ba *mysql-5.7.26.tar.gz
mysql57 $ md5sum mysql-boost-5.7.26.tar.gz
0fb4db48959b0e05a7dba0bbfbb4f8ca *mysql-boost-5.7.26.tar.gz

1.2 安装依赖包

直接使用 yum 安装下面对应的依赖包

yum install -y gcc gcc-c++ cmake ncurses ncurses-devel bison bison-devel

1.3 新建用户及目录

groupadd mysql && \
  useradd -r -g mysql -s /bin/false mysql

mkdir -p /usr/local/mysql/data && \
  chown -R mysql:mysql /usr/local/mysql/data

2 编译安装

2.1 配置 cmake 的编译参数

解压源代码文件

cd /usr/local/mysql
tar xzvf mysql-5.7.26.tar.gz
tar xzvf mysql-boost-5.7.26.tar.gz
cmake . \
      -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
      -DMYSQL_DATADIR=/usr/local/mysql/data \
      -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
      -DSYSCONFDIR=/etc \
      -DEXTRA_CHARSETS=all \
      -DDEFAULT_CHARSET=utf8mb4 \
      -DDEFAULT_COLLATION=utf8mb4_general_ci \
      -DWITH_MYISAM_STORAGE_ENGINE=1 \
      -DWITH_INNOBASE_STORAGE_ENGINE=1 \
      -DWITH_MEMORY_STORAGE_ENGINE=1 \
      -DWITH_PARTITION_STORAGE_ENGINE=1 \
      -DDOWNLOAD_BOOST=0 \
      -DWITH_BOOST=/usr/local/mysql/mysql-5.7.26/boost/boost_1_59_0 \
      -DENABLED_LOCAL_INFILE=1 \
      -DMYSQL_TCP_PORT=3306 \
      -DWITH_READLINE=1 \
      -DMYSQL_USER=mysql \
      -DWITH_SSL=yes

2.2 编译和安装

make && make install

3 配置和建库

3.1 配置数据库

备份操作系统默认的配置文件

sudo mv /etc/my.cnf /etc/my.cnf.1

添加配置文件 /etc/my.cnf, 注意有些系统的发行版中默认包含 my.cnf 文件,在 安装时需要修改

[mysqld]
basedir   = /usr/local/mysql
datadir   = /usr/local/mysql/data
socket    = /usr/local/mysql/mysql.sock
pid-file  = /usr/local/mysql/mysqld.pid
log-error = /usr/local/mysql/error.log

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links       = 0
user                 = mysql
bind-address         = 0.0.0.0
character-set-server = utf8mb4
collation-server     = utf8mb4_general_ci
init-connect         = 'SET NAMES utf8mb4'

# skip-grant-tables to reset root password
# skip-grant-tables
skip-character-set-client-handshake = true

[mysql]
socket    = /usr/local/mysql/mysql.sock
default-character-set = utf8mb4

[client]
default-character-set = utf8mb4

3.2 初始化和建库

初始化数据库目录

chown mysql:mysql -R /usr/local/mysql

./bin/mysqld --initialize-insecure --user=mysql \
             --basedir=/usr/local/mysql \
             --datadir=/usr/local/mysql/data

./bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

添加系统服务

cp support-files/mysql.server /etc/init.d/mysql.server &&
  chmod +x /etc/init.d/mysql.server

启动 MySQL 服务器进程

sudo service mysql.server start

登录进去重置密码

[root@sr650 mysql]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26

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> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>

4 配置环境变量

export PATH=/usr/local/mysql/bin${PATH:+:${PATH}}

5 创建用户和授权

CREATE DATABASE test DEFAULT CHARACTER SET UTF8MB4 COLLATE UTF8MB4_GENERAL_CI;

CREATE USER 'test01'@'%' IDENTIFIED BY 'test01';

GRANT ALL PRIVILEGES ON test.* TO 'test01'@'%';

FLUSH PRIVILEGES;

6 相关连接

Last Updated 2021-07-31 Sat 22:36. Created by Jinghui Hu at 2020-10-15 Thu 07:41.