// 创建用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
// 解压
[root@localhost ~]# cd /usr/src/[root@localhost src]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz nginx-1.20.1.tar.gz
[root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
// 软连接
[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql[root@localhost local]# chown -R mysql.mysql mysql/[root@localhost local]# ln -s /usr/local/mysql/include /usr/include/mysql
// 创建数据存放目录
[root@localhost local]# mkdir /opt/data [root@localhost local]# chown -R mysql.mysql /opt/data
// 配置环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@localhost local]# . /etc/profile.d/mysql.sh
// 初始化
[root@localhost local]# mysqld --initialize-insecure --user mysql --datadir /opt/data2021-10-26T06:46:13.459861Z 0[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation formore details).
2021-10-26T06:46:13.592952Z 0[Warning] InnoDB: New log files created, LSN=457902021-10-26T06:46:13.613937Z 0[Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-26T06:46:13.669318Z 0[Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6968897c-3628-11ec-b95c-000c29ba9d68.
2021-10-26T06:46:13.670365Z 0[Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-26T06:46:14.690255Z 0[Warning] CA certificate ca.pem is self signed.
2021-10-26T06:46:14.789009Z 1[Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
//编写配置文件
[root@localhost local]# cat > /etc/my.cnf << EOF[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port =3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
> EOF
// 写服务控制脚本
cat> /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=Mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/mysql/support-files/mysql.server stop
[Install]
WantedBy=multi-user.target
basedir=/usr/local/mysql
datadir=/opt/data
EOF[root@localhost src]# vim /usr/local/mysql/support-files/mysql.server 46basedir=/usr/local //修改这两行
47datadir=/opt/data
// 启动服务
[root@localhost local]# systemctl daemon-reload [root@localhost local]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost local]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 01280.0.0.0:80 0.0.0.0:*
LISTEN 01280.0.0.0:22 0.0.0.0:*
LISTEN 080 *:3306 *:*
LISTEN 0128[::]:22 [::]:*
// 设置密码
[root@localhost ~]# yum -y install ncurses-compat-libs[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c)2000, 2021, 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>set password = password("syb123");
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye