# MySQL在Linux下Generic安装
##### 在[官网](https://downloads.mysql.com/archives/community/)选择并下载安装包
> `mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz` [点击下载](https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz)
>
> Product Version:`5.7.28`
>
> Operating System:`Linux - Generic`
>
> OS Version:`Linux - Generic (glibc2.12) (x86, 64-bit)`
##### 下载系统原有MySQL
```shell
rpm -qa | grep mariadb # mariadb是CentOS7自带的一种MySQL版本。查看是否已经安装?
```
```shell
rpm -e --nodeps mariadb-libs-* # 强制卸载mariadb数据库。
```
###### 安装MySQL所需依赖
```shell
yum install -y libaio # 一般CentOS7系统自带,不需要安装。
```
##### 为MySQL独立创建用户和组
> `id mysql` # 先检查是否已经创建过`mysql`用户
```shell
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
```
##### 解压安装包
```shell
tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
```
> /usr/local/mysql-5.7.28-linux-glibc2.12-x86_64
##### 创建配置文件
```shell
cat>/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/my.cnf<<EOF
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
socket=/tmp/mysql.sock
user=mysql
character-set-server=utf8
basedir=/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64
datadir=/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/data
pid-file=/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/data/mysql.pid
log_error=/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/data/mysql.err
EOF
```
##### 修改权限
```shell
chown -R mysql:mysql /usr/local/mysql-5.7.28-linux-glibc2.12-x86_64
```
##### 初始化
```shell
/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/bin/mysqld \
--defaults-file=/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/my.cnf \
--initialize
```
###### 查看错误日志(包含初始密码)
```shell
tail -f /usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/data/mysql.err
```
##### mysql_ssl_rsa_setup
```shell
/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/bin/mysql_ssl_rsa_setup \
--defaults-file=/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/my.cnf
```
##### 启动MySQL
```shell
/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/bin/mysqld_safe \
--defaults-file=/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/my.cnf &
```
---
##### 登录MySQL
```shell
/usr/local/mysql-5.7.28-linux-glibc2.12-x86_64/bin/mysql -uroot -p
```
##### 修改密码
```mysql
alter user 'root'@'localhost' identified by 'root';
```
##### 允许root远程连接
```mysql
use mysql;
update user set host = '%' where user = 'root';
select host, user from user;
flush privileges;
```
###### 开放端口
```shell
firewall-cmd --zone=public --add-port=3306/tcp --permanent;
firewall-cmd --reload;
firewall-cmd --list-port;
```