二进制部署MySQL
下载二进制MySQL的包
MySQL :: Download MySQL Community Server (Archived Versions)
传输到自己的虚拟机上
[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]#
查看传输的文件是否变形
[root@localhost ~]# md5sum mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
79b971fc3e3368f2a1e07fbafae0b914 mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]#
创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin -u 306 mysql
[root@localhost ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
将文件解压
[root@localhost ~]# tar xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.39-linux-glibc2.12-x86_64 share
[root@localhost local]#
创建软连接
[root@localhost ~]# ln -s /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost ~]# ll /usr/local/
total 0
drwxr-xr-x. 2 root root 6 Jun 22 2021 bin
drwxr-xr-x. 2 root root 6 Jun 22 2021 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 2 root root 6 Jun 22 2021 lib
drwxr-xr-x. 3 root root 17 Dec 26 11:01 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 root root 46 Dec 29 22:30 mysql -> /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 Dec 29 22:30 mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Dec 26 11:01 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
[root@localhost ~]#
修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql*
[root@localhost ~]# ll /usr/local/
total 0
drwxr-xr-x. 2 root root 6 Jun 22 2021 bin
drwxr-xr-x. 2 root root 6 Jun 22 2021 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 2 root root 6 Jun 22 2021 lib
drwxr-xr-x. 3 root root 17 Dec 26 11:01 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 mysql mysql 46 Dec 29 22:30 mysql -> /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Dec 29 22:30 mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Dec 26 11:01 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
[root@localhost ~]#
添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysqld.sh
[root@localhost ~]# source /etc/profile.d/mysqld.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysqld
[root@localhost ~]# vi /etc/ld.so.conf.d/mysqld.conf
[root@localhost ~]# cat /etc/ld.so.conf.d/mysqld.conf
/usr/local/mysql/lib
[root@localhost ~]# ldconfig
修改/etc/man_db.conf
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man //在下面添加一条
MANDATORY_MANPATH /usr/local/mysql/man
建立数据存放目录并修改属主属组
[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Dec 29 23:10 data
[root@localhost ~]#
初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data
2022-12-29T16:13:55.608410Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-12-29T16:13:55.745143Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-12-29T16:13:55.768088Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-12-29T16:13:55.772320Z 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: cb37bbd0-8793-11ed-9fac-000c29ddd98b.
2022-12-29T16:13:55.772839Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-12-29T16:13:55.982866Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-12-29T16:13:55.982893Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-12-29T16:13:55.983211Z 0 [Warning] CA certificate ca.pem is self signed.
2022-12-29T16:13:56.065944Z 1 [Note] A temporary password is generated for root@localhost: LyFG#UUYU5rW
[root@localhost ~]# echo 'LyFG#UUYU5rW' > pass
[root@localhost ~]# cat pass
LyFG#UUYU5rW
[root@localhost ~]#
查看是否有配置文件,有就备份,没有就创建
[root@localhost ~]# cat /etc/my.cnf
cat: /etc/my.cnf: No such file or directory
[root@localhost ~]#
创建配置文件
[root@localhost ~]# vi /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@localhost ~]#
配置服务启动的服务脚本
[root@localhost ~]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost support-files]# file mysql.server
mysql.server: POSIX shell script, ASCII text executable
[root@localhost support-files]# vim mysql.server
修改
basedir=/usr/local/mysql
datadir=/opt/data
启动服务脚本
[root@localhost support-files]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost support-files]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost support-files]# cd
[root@localhost ~]#
配置一个service文件
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@localhost ~]# vim /usr/lib/systemd/system/mysqld.service
[root@localhost ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl status mysqld
● mysqld.service - mysqld server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disa>
Active: inactive (dead)
[root@localhost ~]#
设置开机自启并且现在启动
[root@localhost ~]# /usr/local/mysql/support-files/mysql.server stop
[root@localhost ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost ~]# systemctl status mysqld
● mysqld.service - mysqld server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-12-30 00:24:10 CST; 31s ago
Process: 22768 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=>
Main PID: 22781 (mysqld_safe)
Tasks: 28 (limit: 12241)
Memory: 171.7M
CGroup: /system.slice/mysqld.service
├─22781 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/da>
└─22983 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --pl>
Dec 30 00:24:09 localhost.localdomain systemd[1]: Starting mysqld server daemon...
Dec 30 00:24:10 localhost.localdomain mysql.server[22768]: Starting MySQL. SUCCESS!
Dec 30 00:24:10 localhost.localdomain systemd[1]: Started mysqld server daemon.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]#
安装一个依赖包
[root@localhost ~]# yum -y install ncurses-compat-libs
CentOS Linux 8 - Extras 52 B/s | 1.5 kB 00:30
Dependencies resolved.
===================================================================================================
Package Architecture Version Repository Size
===================================================================================================
Installing:
ncurses-compat-libs x86_64 6.1-9.20180224.el8 baseos 328 k
Transaction Summary
===================================================================================================
Install 1 Package
Total download size: 328 k
Installed size: 1.0 M
Downloading Packages:
[MIRROR] ncurses-compat-libs-6.1-9.20180224.el8.x86_64.rpm: Curl error (28): Timeout was reached for http://vault.centos.org/centos/8/BaseOS/x86_64/os/Packages/ncurses-compat-libs-6.1-9.20180224.el8.x86_64.rpm [Connection timed out after 30001 milliseconds]
ncurses-compat-libs-6.1-9.20180224.el8.x86_64.rpm 9.7 kB/s | 328 kB 00:33
---------------------------------------------------------------------------------------------------
Total 9.7 kB/s | 328 kB 00:33
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : ncurses-compat-libs-6.1-9.20180224.el8.x86_64 1/1
Running scriptlet: ncurses-compat-libs-6.1-9.20180224.el8.x86_64 1/1
Verifying : ncurses-compat-libs-6.1-9.20180224.el8.x86_64 1/1
Installed:
ncurses-compat-libs-6.1-9.20180224.el8.x86_64
Complete!
[root@localhost ~]#
登陆,修改密码
[root@localhost ~]# cat pass
LyFG#UUYU5rW
[root@localhost ~]# mysql -uroot -p'LyFG#UUYU5rW'
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 2
Server version: 5.7.39
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> set password = password('dianjijixu123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@localhost ~]#
重新登录
[root@localhost ~]# mysql -uroot -p'dianjijixu123!'
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 3
Server version: 5.7.39 MySQL Community Server (GPL)
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>
[root@localhost ~]# vim ~/.my.cnf
[root@localhost ~]# cat ~/.my.cnf
[client]
user = root
password = dianjijixu123!
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.39 MySQL Community Server (GPL)
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>