文章目录

  • 1. 下载MySQL软件包
  • 1.2 将刚才下载的软件包放到虚拟机中的src目录中
  • 1.3 创建一个名为MySQL的系统用户
  • 1.4 解压mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz安装包
  • 1.5 把/usr/local目录中刚才解压的MySQL软件包名字修改成MySQL
  • 1.6 修改MySQL的属组
  • 1.7 建立数据存放目录
  • 1.8 初始化数据库
  • 1.9 进入到mysql
  • 1.9.1 设置MySQL开机自启
  • 2 mysql密码破解(只能用root用户来操作)
  • 2.1 修改配置文件,加入skip-drants-table
  • 2.2 重启mysql
  • 2.3 修改密码
  • 2.4 恢复配置文件,删除skip-drants-table
  • 2.5 重启mysql
  • 2.6 验证新密码是否能登录


1. 下载MySQL软件包

mysql二进制的校对规则 mysql二进制安装教程_linux_02

  • 选择(GPL)下载
  • 点击下载文档
  • 选择第一个
  • 然后选择以下内容并下载第一歌软件包

1.2 将刚才下载的软件包放到虚拟机中的src目录中

mysql二进制的校对规则 mysql二进制安装教程_MySQL_03

1.3 创建一个名为MySQL的系统用户

[root@localhost src]# useradd -r -M -s /sbin/nologin mysql
[root@localhost src]# id mysql 
uid=975(mysql) gid=973(mysql) 组=973(mysql)

1.4 解压mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz安装包

[root@localhost src]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[root@localhost src]# ls /usr/local/       //查看一下是否解压完成
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.37-linux-glibc2.12-x86_64  sbin  share  src

1.5 把/usr/local目录中刚才解压的MySQL软件包名字修改成MySQL

[root@localhost local]# mv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@localhost local]#

1.6 修改MySQL的属组

[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root  root    6 8月  12 2018 bin
drwxr-xr-x. 2 root  root    6 8月  12 2018 etc
drwxr-xr-x. 2 root  root    6 8月  12 2018 games
drwxr-xr-x. 2 root  root    6 8月  12 2018 include
drwxr-xr-x. 2 root  root    6 8月  12 2018 lib
drwxr-xr-x. 2 root  root    6 8月  12 2018 lib64
drwxr-xr-x. 2 root  root    6 8月  12 2018 libexec
drwxr-xr-x. 9 mysql mysql 129 6月  28 06:49 mysql        //已经修改完成
drwxr-xr-x. 2 root  root    6 8月  12 2018 sbin
drwxr-xr-x. 5 root  root   49 5月  24 10:16 share
drwxr-xr-x. 2 root  root    6 8月  12 2018 src


//添加环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh   //重新读取下
[root@localhost local]# 

//将MySQL目录下面的include目录软连接到/usr/include/mysql
[root@localhost local]# ln -s /usr/local/mysql/include /usr/include/mysql
//告知lib库的路径
[root@localhost local]# vim /etc/ld.so.conf.d/mysql.conf
[root@localhost local]# cat /etc/ld.so.conf.d/mysql.conf 
/usr/local/mysql/lib
[root@localhost local]# ldconfig     //重新读取配置文件
//配置man文档
[root@localhost local]# vim /etc/man_db.conf 
#---------------------------------------------------------
# every automatically generated MANPATH includes these fields
#
#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    //在文档中添加这一行
#---------------------------------------------------------

1.7 建立数据存放目录

[root@localhost local]# mkdir /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data/
[root@localhost local]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 6月  28 07:19 data

1.8 初始化数据库

[root@localhost local]# mysqld --initialize --user mysql --datadir /opt/data
2022-06-28T11:24:07.816771Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-06-28T11:24:08.153179Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-06-28T11:24:08.219741Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-06-28T11:24:08.283127Z 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: d3756dcf-f6d4-11ec-89e4-000c2940890e.
2022-06-28T11:24:08.284915Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-06-28T11:24:09.397182Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-06-28T11:24:09.397223Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-06-28T11:24:09.397945Z 0 [Warning] CA certificate ca.pem is self signed.
2022-06-28T11:24:09.539935Z 1 [Note] A temporary password is generated for root@localhost: Qsao9#M_iT7m             //这个密码是随机生成的临时密码用来登录到MySQL修改密码。此处密码是:Qsao9#M_iT7m


//生成配置文件
[root@localhost local]# vim /etc/my.cnf
[root@localhost local]# 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 local]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost support-files]# cp mysql.server mysqld
[root@localhost support-files]# chown -R mysql.mysql mysqld
[root@localhost support-files]# vim mysqld


# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql       //修改这一行
datadir=/opt/data             //修改这一行

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start. 

//启动MySQL
[root@localhost support-files]# /usr/local/mysql/support-files/mysqld 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                    
LISTEN                    0                         128                                                  0.0.0.0:111                                               0.0.0.0:*                       
LISTEN                    0                         32                                             192.168.122.1:53                                                0.0.0.0:*                       
LISTEN                    0                         128                                                  0.0.0.0:22                                                0.0.0.0:*                       
LISTEN                    0                         5                                                  127.0.0.1:631                                               0.0.0.0:*                       
LISTEN                    0                         128                                                     [::]:111                                                  [::]:*                       
LISTEN                    0                         128                                                     [::]:22                                                   [::]:*                       
LISTEN                    0                         5                                                      [::1]:631                                                  [::]:*                       
LISTEN                    0                         80                                                         *:3306                                                    *:*      //有这个端口就说明启动成功了

//下载库
[root@localhost ~]# dnf provides libncurses.so.5     //查找libncurses.so.5库
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:2:01:56 前,执行于 2022年06月28日 星期二 05时54分17秒。
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
仓库        :base
匹配来源:
提供    : libncurses.so.5

[root@localhost ~]# yum -y install ncurses-compat-libs      //安装过程已省略

1.9 进入到mysql

[root@localhost ~]# mysql -uroot -p'Qsao9#M_iT7m'
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.37

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> 


//修改密码
mysql> set password = password('cxr123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

//验证密码是否有效
[root@localhost ~]# mysql -uroot -p'cxr123!'
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 4
Server version: 5.7.37 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>        //成功登录

1.9.1 设置MySQL开机自启

[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service      //复制一份sshd.service文件
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# vim mysqld.service 
[root@localhost system]# cat mysqld.service             //配置文件
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@localhost system]# 


//关闭selinux
[root@localhost system]# vim /etc/selinux/config 

SELINUX=disabled     //修改成禁用模式
[root@localhost system]# vim /etc/selinux/config 
[root@localhost system]# reboot          //重启一下

连接断开

[root@localhost ~]# systemctl daemon-reload    
[root@localhost ~]# systemctl status mysqld      //查看MySQL状态
● mysqld.service - mysql server daemon
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@localhost ~]# systemctl start mysqld.service    //开启MySQL服务
[root@localhost ~]# systemctl status mysqld.service     //再次查看MySQL,已经打开服务了
● mysqld.service - mysql server daemon
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-06-28 08:20:23 EDT; 12s ago
  Process: 4766 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited, status=0/SUCCESS)
 Main PID: 4798 (mysqld_safe)
    Tasks: 28 (limit: 11160)
   Memory: 229.2M
   CGroup: /system.slice/mysqld.service
           ├─4798 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
           └─5030 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-fil>

6月 28 08:20:22 localhost.localdomain systemd[1]: Starting mysql server daemon...
6月 28 08:20:23 localhost.localdomain mysqld[4766]: Starting MySQL. SUCCESS!
6月 28 08:20:23 localhost.localdomain systemd[1]: Started mysql server daemon.

2 mysql密码破解(只能用root用户来操作)

2.1 修改配置文件,加入skip-drants-table

[root@localhost ~]# vim /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 ~]# vim /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
skip-grants-table                              //在文件中加入这一行

2.2 重启mysql

[root@localhost ~]# systemctl restart mysqld

2.3 修改密码

[root@localhost ~]# mysql     //直接输入MySQL登录进去(已经绕过密码)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37 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> use mysql;      //进入到MySQL库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string = password('123456') where User = 'root' and Host = 'localhost';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql>

2.4 恢复配置文件,删除skip-drants-table

[root@localhost ~]# vim /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

2.5 重启mysql

[root@localhost ~]# systemctl restart mysqld.service

2.6 验证新密码是否能登录

[root@localhost ~]# mysql -uroot -p'123456'        //登录成功
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.37 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> 
Bye
[root@localhost ~]#