Linux环境下安装MySQL

Linux 环境下安装 MySQL

下载

1
2
3
4
5
6
7
8
# 下载 mysql
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.20-el7-x86_64.tar.gz
# 解压缩
tar -zxvf mysql-8.0.20-el7-x86_64.tar.gz
# 重命名
mv mysql-8.0.20-el7-x86_64 /data/software/mysql
# 进入目录
cd /data/software/mysql

创建用户及用户组

1
2
groupadd mysql
useradd -g mysql mysql

创建日志及进程文件

1
2
3
mkdir /var/log/mariadb /var/run/mariadb
touch /var/log/mariadb/mariadb.log /var/run/mariadb/mariadb.pid
chown -R mysql:mysql /var/log/mariadb /var/run/mariadb

修改配置

vim /etc/my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[client]
port=3306
socket=/tmp/mysql.sock

[mysqld]
port=3306
user=mysql
socket=/tmp/mysql.sock
basedir=/data/software/mysql
datadir=/data/software/mysql/data

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

初始化

1
2
3
4
5
6
[root@localhost mysql]# /data/software/mysql/bin/mysqld --initialize --user=mysql --basedir=/data/software/mysql/ --datadir=/data/software/mysql/data/
2020-05-07T15:40:08.278047Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2020-05-07T15:40:08.278154Z 0 [System] [MY-013169] [Server] /data/software/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 2524
2020-05-07T15:40:08.291519Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-07T15:40:08.558052Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-05-0715:40:09.093362Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ToX3>VJQuFrt

启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# service mysqld start
Starting MySQL [ OK ]
[root@localhost mysql]# bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20

Copyright (c) 2000, 2020, 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> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.01 sec)

# 修改 root 用户可以远程连接
mysql> update mysql.user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

# 刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

开放端口

1
2
3
4
# 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重载入
firewall-cmd --reload