MySQL安装初始化

一、yum安装MySQL/MariaDB

1、下载安装MySQL源

https://dev.mysql.com/downloads/repo/yum/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@Rocky8-11 ~]# wget  https://dev.mysql.com/get/mysql80-community-release-el8-5.noarch.rpm
[root@Rocky8-11 ~]# ls
anaconda-ks.cfg mysql80-community-release-el8-5.noarch.rpm
[root@Rocky8-11 ~]# yum install mysql80-community-release-el8-5.noarch.rpm

[root@Rocky8-11 ~]# yum repolist
repo id repo name
appstream Rocky Linux 8 - AppStream
baseos Rocky Linux 8 - BaseOS
epel Extra Packages for Enterprise Linux 8 - x86_64
extras Rocky Linux 8 - Extras
mysql-connectors-community MySQL Connectors Community
mysql-tools-community MySQL Tools Community
mysql80-community MySQL 8.0 Community Server

2、yum安装MySQL 8.0

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
[root@Rocky8-11 ~]# yum search mysql-server
Last metadata expiration check: 21:29:53 ago on Sat 06 May 2023 04:36:41 PM CST.
========= Name Exactly Matched: mysql-server =========
mysql-server.x86_64 : The MySQL server and related files

[root@Rocky8-11 ~]# yum install -y mysql-server
Installed:
checkpolicy-2.9-1.el8.x86_64 mariadb-connector-c-config-3.1.11-2.el8_3.noarch
mecab-0.996-2.module+el8.6.0+1057+4d6a1721.x86_64 mysql-8.0.30-1.module+el8.6.0+1057+4d6a1721.x86_64
mysql-common-8.0.30-1.module+el8.6.0+1057+4d6a1721.x86_64 mysql-errmsg-8.0.30-1.module+el8.6.0+1057+4d6a1721.x86_64
mysql-server-8.0.30-1.module+el8.6.0+1057+4d6a1721.x86_64 policycoreutils-python-utils-2.9-20.el8.noarch
protobuf-lite-3.5.0-15.el8.x86_64 python3-audit-3.0.7-4.el8.x86_64
python3-libsemanage-2.9-9.el8.x86_64 python3-policycoreutils-2.9-20.el8.noarch
python3-setools-4.3.0-3.el8.x86_64
Complete!

[root@Rocky8-11 ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.

[root@Rocky8-11 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*

3、查询初始密码,查询数据库运行状态

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
30
31
32
33
34
35
36
37
38
39
40
41
[root@Rocky8-11 ~]# grep password /var/log/mysql/mysqld.log 
2023-04-27T12:31:35.953177Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

[root@Rocky8-11 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30 Source distribution

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> status
--------------
mysql Ver 8.0.30 for Linux on x86_64 (Source distribution)

Connection id: 8
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.30 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /var/lib/mysql/mysql.sock
Binary data as: Hexadecimal
Uptime: 6 min 7 sec

Threads: 2 Questions: 5 Slow queries: 0 Opens: 120 Flush tables: 3 Open tables: 36 Queries per second avg: 0.013
--------------

4、修改密码

方法一:

1
2
3
4
mysql> alter user root@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql>

方法二:

1
2
3
[root@Rocky8-11 ~]# mysqladmin -uroot -p'123456' password '654321'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

用新密码登录mysql:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@Rocky8-11 ~]# mysql -uroot -p654321
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.30 Source distribution

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

5、下载安装MariaDB源

https://mariadb.org/download/?t=repo-config&d=CentOS+7&v=10.11&r_m=aliyun

1
2
3
4
5
6
7
8
9
10
11
12
[root@Rocky8-12 ~]# cat /etc/yum.repos.d/mariadb.repo
# MariaDB 10.11 CentOS repository list - created 2023-05-07 06:47 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
# rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# baseurl = https://rpm.mariadb.org/10.11/centos/$releasever/$basearch
baseurl = https://mirrors.aliyun.com/mariadb/yum/10.11/centos/$releasever/$basearch
module_hotfixes = 1
# gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey = https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1

6、yum安装MariaDB

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@Rocky8-12 ~]# yum search  MariaDB-server 
MariaDB 224 kB/s | 483 kB 00:02
Last metadata expiration check: 0:00:01 ago on Sun 07 May 2023 02:53:17 PM CST.
========= Name Exactly Matched: MariaDB-server =========
MariaDB-server.x86_64 : MariaDB database server binaries
============= Name & Summary Matched: MariaDB-server ==========
MariaDB-server-debuginfo.x86_64 : Debug information for package MariaDB-server
========== Name Matched: MariaDB-server ==========
mariadb-server.x86_64 : The MariaDB server and related files
mariadb-server-galera.x86_64 : The configuration files and scripts for galera replication
mariadb-server-utils.x86_64 : Non-essential server utilities for MariaDB/MySQL applications

[root@Rocky8-12 ~]# yum install -y mariadb-server

Installed:
MariaDB-client-10.11.2-1.el8.x86_64 MariaDB-common-10.11.2-1.el8.x86_64 MariaDB-server-10.11.2-1.el8.x86_64
MariaDB-shared-10.11.2-1.el8.x86_64 boost-program-options-1.66.0-13.el8.x86_64 galera-4-26.4.14-1.el8.x86_64
libpmem-1.6.1-1.el8.x86_64 perl-DBI-1.641-4.module+el8.6.0+891+677074cb.x86_64 perl-Math-BigInt-1:1.9998.11-7.el8.noarch
perl-Math-Complex-1.59-421.el8.noarch socat-1.7.4.1-1.el8.x86_64

Complete!

[root@Rocky8-12 ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

[root@Rocky8-12 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.11.2-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
MariaDB [(none)]> status
--------------
mysql Ver 15.1 Distrib 10.11.2-MariaDB, for Linux (x86_64) using readline 5.1

Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.11.2-MariaDB MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8mb3
Conn. characterset: utf8mb3
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 1 min 15 sec

Threads: 1 Questions: 4 Slow queries: 0 Opens: 17 Open tables: 10 Queries per second avg: 0.053
--------------

MariaDB [(none)]>

二、二进制安装MySQL 8.0

1、安装环境包

1
[root@Rocky8-11 ~]# yum install -y libaio numactl-libs

2、准备用户

1
2
[root@Rocky8-11 ~]# groupadd  mysql 
[root@Rocky8-11 ~]# useradd -r -g mysql -s /bin/false mysql

3、准备二进制程序

https://dev.mysql.com/downloads/mysql/

20230509213749

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@Rocky8-11 ~]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz
[root@Rocky8-11 ~]# tar xf mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz -C /usr/local
[root@Rocky8-11 ~]# cd /usr/local/
[root@Rocky8-11 local]# ln -s mysql-8.0.33-linux-glibc2.28-x86_64/ mysql
[root@Rocky8-11 local]# chown -R mysql.mysql /usr/local/mysql/
[root@Rocky8-11 local]# mkdir -p /data/mysql

[root@Rocky8-11 local]# ll
total 0
drwxr-xr-x. 2 root root 6 Oct 11 2021 bin
drwxr-xr-x. 2 root root 6 Oct 11 2021 etc
drwxr-xr-x. 2 root root 6 Oct 11 2021 games
drwxr-xr-x. 2 root root 6 Oct 11 2021 include
drwxr-xr-x. 2 root root 6 Oct 11 2021 lib
drwxr-xr-x. 3 root root 17 May 6 13:15 lib64
drwxr-xr-x. 2 root root 6 Oct 11 2021 libexec
lrwxrwxrwx 1 root root 36 May 9 22:38 mysql -> mysql-8.0.33-linux-glibc2.28-x86_64/
drwxr-xr-x 9 mysql mysql 129 May 9 22:31 mysql-8.0.33-linux-glibc2.28-x86_64
drwxr-xr-x. 2 root root 6 Oct 11 2021 sbin
drwxr-xr-x. 5 root root 49 May 6 13:15 share
drwxr-xr-x. 2 root root 6 Oct 11 2021 src

4、准备PATH环境

1
2
3
4
[root@Rocky8-11 local]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@Rocky8-11 local]# . /etc/profile.d/mysql.sh
[root@Rocky8-11 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

5、准备配置文件

1
2
3
4
5
6
7
8
9
10
[root@Rocky8-11 local]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[client]
socket=/data/mysql/mysql.sock

6、初始化数据库

方法一:生成随机密码

1
2
3
4
5
6
[root@Rocky8-11 ~]# mysqld --initialize  --user=mysql  --datadir=/data/mysql
[root@Rocky8-11 ~]# grep password /data/mysql/mysql.log
2023-05-09T14:54:51.466558Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: su,V+SoSl1uj

[root@Rocky8-11 ~]# awk '/temporary password/{print $NF}' /data/mysql/mysql.log
su,V+SoSl1uj

方法二:生成空密码

1
[root@Rocky8-11 ~]# mysql --initialize-insecure --user=mysql --datadir=/data/mysql
1
2
3
4
[root@Rocky8-11 ~]# ls /data/mysql/
auto.cnf client-cert.pem '#ib_16384_1.dblwr' '#innodb_redo' mysql.ibd private_key.pem server-key.pem undo_002
ca-key.pem client-key.pem ib_buffer_pool '#innodb_temp' mysql.log public_key.pem sys
ca.pem '#ib_16384_0.dblwr' ibdata1 mysql performance_schema server-cert.pem undo_001

7、准备服务脚本并启动

1
2
3
4
5
6
7
8
9
10
11
12
[root@Rocky8-11 ~]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
[root@Rocky8-11 ~]# chkconfig --add mysqld
[root@Rocky8-11 ~]# service mysqld start
Starting MySQL... [ OK ]
[root@Rocky8-11 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*

8、修改root密码

1
2
3
[root@Rocky8-11 ~]# mysqladmin -uroot -p'su,V+SoSl1uj' password 123456
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

9、登录测试

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
[root@Rocky8-11 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)

mysql>

MySQL安装加固脚本(老版mysql使用)

1
[root@Rocky8-11 ~]# mysql_secure_installation