# 2、源码安装MYSQL5.7.21

### 1.准备源码包

> Mysql5.7版本，需呀boost库，boost库下载地址： <https://sourceforge.net/projects/boost/files/boost/1.59.0/>

> mysql官网地址： <https://dev.mysql.com/downloads/mysql/> ----> Archives

```bash
mkdir download && cd download/
wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21.tar.gz
```

### 2.准备需要目录

```bash
# 创建 MYSQL 启动用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
chown -R mysql:mysql /usr/local/mysql


# MYSQL 存放日志
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb/

# MYSQL sock 文件目录
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/

# MYSQL 数据存放位置
mkdir /data/mysql -p
```

### 3.安装依赖包

```bash
yum -y install gcc gcc-c++ ncurses ncurses-devel cmake
```

### 4.编译 MYSQL

```bash
tar -vxzf boost_1_59_0.tar.gz
mv boost_1_59_0 /usr/local/boost_1_59_0


tar -vxzf mysql-5.7.21.tar.gz
cd mysql-5.7.21

# 编译
cmake . \
 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 -DMYSQL_DATADIR=/data/mysql \
 -DWITH_BOOST=/usr/local/boost_1_59_0 \
 -DSYSCONFDIR=/etc \
 -DDEFAULT_CHARSET=utf8mb4 \
 -DDEFAULT_COLLATION=utf8mb4_general_ci \
 -DENABLED_LOCAL_INFILE=1 \
 -DEXTRA_CHARSETS=all

cpuNum=$(cat /proc/cpuinfo |grep processor |wc -l)
make -j${cpuNum} && make install 

```

> -DCMAKE\_INSTALL\_PREFIX：mysql安装目录 -DMYSQL\_DATADIR：数据存放目录 -DWITH\_BOOST：boost源码路径 -DSYSCONFDIR：my.cnf配置文件目录 -DEFAULT\_CHARSET：数据库默认字符编码 -DDEFAULT\_COLLATION：默认排序规则 -DENABLED\_LOCAL\_INFILE：允许从本文件导入数据 -DEXTRA\_CHARSETS：安装所有字符集 更多参数： <http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html#cmake-general-options>

### 5. 修改/etc/my.cnf文件和/etc/my.cnf.d/mysql-clients.cnf文件

1）修改/etc/my.cnf文件

```bash
### /etc/my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock

#指定server端字符集
character-set-server = utf8 
collation-server = utf8_general_ci

# 解决问题：TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true

# mysql程序安装目录
basedir=/usr/local/mysql

# mysql数据文件目录
datadir=/data/mysql

pid-file = /data/mysql/mysql.pid
log_error = /data/mysql/mysql-error.log

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

#更改字符集为utf-8
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8
```

2）修改/etc/my.cnf.d/mysql-clients.cnf文件

```bash
###  /etc/my.cnf.d/mysql-clients.cnf
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#

# 在配置文件中添加“[client]”选项和“[mysql]”选项
# 并使用这两个选项下的“socket”参数值，与“[mysqld]”选项下的“socket”参数值，指向的socket文件路径完全一致
# 解决问题： connect to local MySQL server through socket /var/lib/mysql/mysql.sock

[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

[client]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]
```

### 6.初始化数据库并将mysql加入系统服务

```bash
cd /usr/local/mysql/bin/
#两种方式任选一种
#（1）不生成root密码
#./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql  

#（2）生成root随机密码，在/data/mysql/mysql-error.log文件中 
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql


cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql 
chkconfig --add mysql
chkconfig mysql on
service mysql start
```

### 7.登录mysql, 并修改密码

```bash
echo "PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH" >> /etc/profile
echo "export PATH" >> /etc/profile
source /etc/profile

# 获取初始密码
cat /data/mysql/mysql-error.log | grep password 


mysql> set password=password('123654');
Query OK, 0 rows affected, 1 warning (0.00 sec)
```

> 参考：<https://www.jianshu.com/p/41ac166ef477>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/mysql/2-yuan-ma-an-zhuang-mysql5.7.21.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
