安装软件:
[root@mysql01 ~]# dnf install @mysql
Last metadata expiration check: 0:00:05 ago on Sat 19 Sep 2020 12:49:38 PM CST.
Dependencies resolved.
====================================================================================================================================================================================================================
Package Architecture Version Repository Size
====================================================================================================================================================================================================================
Installing group/module packages:
mysql-server x86_64 8.0.21-1.module_el8.2.0+493+63b41e36 AppStream 22 M
Installing dependencies:
.... omitted for brevity
Transaction Summary
====================================================================================================================================================================================================================
Install 48 Packages
Total download size: 48 M
Installed size: 218 M
Is this ok [y/N]:
启动与初始化(第一个直接回车剩余全Y):
[root@mysql01 ~]# systemctl start mysqld
[root@mysql01 ~]# mysql_secure_installation
....
Press y|Y for Yes, any other key for No:
Please set the password for root here.
New password:
Re-enter new password:
....
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
....
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
....
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
....
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
....
[root@mysql01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21 Source distribution
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> quit
Bye
[root@mysql01 ~]#
数据最好单独存放在一块单独的磁盘上(如果觉得没关系到这一步安装就结束啦)。
关机挂载新的磁盘:
[root@mysql01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 6.7G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─cl-root 253:0 0 17G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
nvme0n2 259:3 0 20G 0 disk
[root@mysql01 ~]#
初始化磁盘:
[root@mysql01 ~]# mkfs.xfs /dev/nvme0n2
meta-data=/dev/nvme0n2 isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@mysql01 ~]# mkdir /data
[root@mysql01 ~]# blkid | grep nvme0n2
/dev/nvme0n2: UUID="3fe3b609-bab0-477d-a56b-de6a3d049f05" TYPE="xfs"
[root@mysql01 ~]# vi /etc/fstab
[root@mysql01 ~]# tail -2 /etc/fstab
# User defined 2019/9/19
UUID=3fe3b609-bab0-477d-a56b-de6a3d049f05 /data xfs defaults 0 0
[root@mysql01 ~]# mount -a
[root@mysql01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 884M 0 884M 0% /dev
tmpfs 901M 0 901M 0% /dev/shm
tmpfs 901M 8.7M 892M 1% /run
tmpfs 901M 0 901M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 2.1G 15G 13% /
/dev/nvme0n1p1 976M 188M 722M 21% /boot
tmpfs 181M 0 181M 0% /run/user/0
/dev/nvme0n2 20G 175M 20G 1% /data
[root@mysql01 ~]#
查看MySQL数据存放目录并停止服务:
[root@mysql01 ~]# 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.21 Source distribution
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> select @@datadir;
+-----------------+
| @@datadir |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)
mysql> quit
Bye
[root@mysql01 ~]# systemctl stop mysqld
[root@mysql01 ~]#
同步数据到挂载的磁盘:
[root@mysql01 ~]# mkdir /data/mysql/data -p
[root@mysql01 ~]# chown -R mysql.mysql /data/mysql
[root@mysql01 ~]# rsync -av /var/lib/mysql/ /data/mysql/data
sending incremental file list
./
#ib_16384_0.dblwr
#ib_16384_1.dblwr
....
sent 172,898,555 bytes received 2,590 bytes 345,802,290.00 bytes/sec
total size is 172,847,357 speedup is 1.00
[root@mysql01 ~]#
修改新数据目录的SELinux属性:
[root@mysql01 ~]# ll -Z /data/mysql/data/ -d
drwxr-xr-x. 6 mysql mysql unconfined_u:object_r:unlabeled_t:s0 4096 Sep 19 14:52 /data/mysql/data/
[root@mysql01 ~]# ll -Z /var/lib/mysql -d
drwxr-xr-x. 6 mysql mysql system_u:object_r:mysqld_db_t:s0 4096 Sep 19 14:52 /var/lib/mysql
[root@mysql01 ~]# chcon -R --reference /var/lib/mysql /data/mysql/data/
[root@mysql01 ~]#
修改MySQL数据文件夹的指向:
[root@mysql01 ~]# cd /etc/my.cnf.d/
[root@mysql01 my.cnf.d]# cp mysql-server.cnf{,.bak}
[root@mysql01 my.cnf.d]# vi mysql-server.cnf
[root@mysql01 my.cnf.d]# cat mysql-server.cnf
[mysqld]
datadir=/data/mysql/data
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
[root@mysql01 my.cnf.d]#
启动数据库进行检验:
[root@mysql01 ~]# systemctl start mysqld
[root@mysql01 ~]# 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.21 Source distribution
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> select @@datadir;
+-------------------+
| @@datadir |
+-------------------+
| /data/mysql/data/ |
+-------------------+
1 row in set (0.00 sec)
mysql> quit
Bye
[root@mysql01 ~]# rm -rf /var/lib/mysql/*
[root@mysql01 ~]#
在官网下载软件(https://downloads.mysql.com/archives/community/
):
[root@mysql01 ~]# cd /usr/local/
[root@mysql01 local]# rz
[root@mysql01 local]# tar -Jxf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
[root@mysql01 local]# ln -s mysql-8.0.20-linux-glibc2.12-x86_64 mysql
[root@mysql01 local]# rm mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
rm: remove regular file 'mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz'? y
[root@mysql01 local]#
[root@mysql01 local]# vi /etc/profile
[root@mysql01 local]# tail -2 /etc/profile
# User defined 2020/9/19
export PATH=$PATH:/usr/local/mysql/bin
[root@mysql01 local]# . /etc/profile
[root@mysql01 local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@mysql01 local]#
关机挂载新的NVMe数据磁盘(nvme0n2):
[root@mysql01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 6.7G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─cl-root 253:0 0 17G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
nvme0n2 259:3 0 20G 0 disk
[root@mysql01 ~]#
初始化磁盘:
[root@mysql01 ~]# mkfs.xfs /dev/nvme0n2
meta-data=/dev/nvme0n2 isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@mysql01 ~]# mkdir /data
[root@mysql01 ~]# blkid | grep nvme0n2
/dev/nvme0n2: UUID="30895c87-eca7-4ec0-a97a-cb90085944e6" TYPE="xfs"
[root@mysql01 ~]# vi /etc/fstab
[root@mysql01 ~]# tail -5 /etc/fstab
UUID=8caa2215-0030-4c28-afe5-27e1557eb558 /boot ext4 defaults 1 2
/dev/mapper/cl-swap swap swap defaults 0 0
# User defined 2020/9/19
UUID=30895c87-eca7-4ec0-a97a-cb90085944e6 /data xfs defaults 0 0
[root@mysql01 ~]# mount -a
[root@mysql01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 884M 0 884M 0% /dev
tmpfs 901M 0 901M 0% /dev/shm
tmpfs 901M 8.7M 892M 1% /run
tmpfs 901M 0 901M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 4.2G 13G 25% /
/dev/nvme0n1p1 976M 188M 722M 21% /boot
tmpfs 181M 0 181M 0% /run/user/0
/dev/nvme0n2 20G 175M 20G 1% /data
[root@mysql01 ~]#
新建Mysql用户,分发目录权限:
[root@mysql01 ~]# useradd -r mysql
[root@mysql01 ~]# chown -R mysql.mysql /usr/local/mysql
[root@mysql01 ~]# mkdir /data/mysql/data -p
[root@mysql01 ~]# chown -R mysql.mysql /data/mysql
[root@mysql01 ~]#
初始化数据库:
[root@mysql01 ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
2020-09-19T03:37:32.891431Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.20-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 1713
2020-09-19T03:37:32.921324Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-09-19T03:37:33.967312Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-09-19T03:37:34.805344Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: UO3Mf(u0w7Fr
[root@mysql01 ~]#
-非安全初始化,root@localhost用户直接可以登录
[root@mysql01 ~]# rm -rf /data/mysql/data/*
[root@mysql01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
2020-09-19T03:59:57.318151Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.20-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 2307
2020-09-19T03:59:57.333293Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-09-19T03:59:57.688674Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-09-19T03:59:58.844761Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@mysql01 ~]#
添加配置文件:
[root@mysql01 ~]# vi /etc/my.cnf
[root@mysql01 ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
[root@mysql01 ~]#
配置服务:
[root@mysql01 ~]# vi /etc/systemd/system/mysqld.service
[root@mysql01 ~]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
[Install]
WantedBy=multi-user.target
[root@mysql01 ~]# systemctl daemon-reload
[root@mysql01 ~]#
启动与登录:
[root@mysql01 ~]# systemctl start mysqld
[root@mysql01 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/etc/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2020-09-19 12:40:43 CST; 2s ago
Main PID: 2500 (mysqld)
Tasks: 39 (limit: 11310)
Memory: 325.7M
CGroup: /system.slice/mysqld.service
└─2500 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
... omitted for brevity
[root@mysql01 ~]# mysql
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
[root@mysql01 ~]#
解决报错找不到某个库文件:
[root@mysql01 ~]# ll /lib64/libtinfo*
lrwxrwxrwx. 1 root root 15 May 11 2019 /lib64/libtinfo.so.6 -> libtinfo.so.6.1
-rwxr-xr-x. 1 root root 208616 May 11 2019 /lib64/libtinfo.so.6.1
[root@mysql01 ~]# ln -s /lib64/libtinfo.so.6.1 /lib64/libtinfo.so.5
[root@mysql01 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL
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>