zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql二进制安装与mysql密码破解

2023-06-13 09:11:17 时间

1、二进制格式mysql安装

1.1 下载二进制格式的mysql软件包(以5.7版本为例)

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz

1.2 解压软件包至/usr/local/

[root@localhost src]# tar -xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

1.3 创建MySQL用户和组

[root@localhost src]# useradd -M -r -s /sbin/nologin mysql
[root@localhost src]# id mysql 
uid=994(mysql) gid=991(mysql) groups=991(mysql)

1.4 设置软链接

[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.37-linux-glibc2.12-x86_64/'
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root   6 May 19  2020 bin
drwxr-xr-x. 2 root root   6 May 19  2020 etc
drwxr-xr-x. 2 root root   6 May 19  2020 games
drwxr-xr-x. 2 root root   6 May 19  2020 include
drwxr-xr-x. 2 root root   6 May 19  2020 lib
drwxr-xr-x. 3 root root  17 Jul 23 21:08 lib64
drwxr-xr-x. 2 root root   6 May 19  2020 libexec
lrwxrwxrwx. 1 root root  36 Jul 26 21:04 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Jul 26 21:00 mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 May 19  2020 sbin
drwxr-xr-x. 5 root root  49 Jul 23 21:08 share
drwxr-xr-x. 2 root root   6 May 19  2020 src

1.5 修改目录/usr/local/mysql的属主属组

[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql  36 Jul 29 01:58 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Jul 29 01:55 mysql-5.7.37-linux-glibc2.12-x86_64

1.6 添加环境变量

[root@localhost local]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh 
[root@localhost local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

1.7头文件—include,库文件—lib,man文件路径

//头文件—include
[root@localhost mysql]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost mysql]# ll /usr/include/
total 0
lrwxrwxrwx. 1 root root 24 Jul 26 21:21 mysql -> /usr/local/mysql/include
drwxr-xr-x. 2 root root 27 Jul 23 21:08 python3.6m

//库文件—lib
[root@localhost mysql]# vi /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost mysql]# ldconfig

//man文件路径
[root@localhost mysql]# vim /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
#---------------------------------------------------------
# set up PATH to MANPATH mapping
......

1.8 建立数据存放目录

[root@localhost mysql]# mkdir /opt/data
[root@localhost mysql]# chown -R mysql.mysql /opt/data/
[root@localhost mysql]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 26 21:33 data

1.9 初始化数据库

[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-07-26T13:36:41.888466Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-26T13:36:42.079359Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-26T13:36:42.107888Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-26T13:36:42.112926Z 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: fbe08be2-0ce7-11ed-b350-000c29e16842.
2022-07-26T13:36:42.113574Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-26T13:36:42.772428Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-26T13:36:42.772459Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-26T13:36:42.772937Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-26T13:36:42.878217Z 1 [Note] A temporary password is generated for root@localhost: eI+:%lrVH0td
[root@localhost ~]# echo 'eI+:%lrVH0td' > passwd
[root@localhost ~]# cat passwd 
eI+:%lrVH0td

注意

请注意,这个命令的最后会生成一个临时密码,此处密码是eI+:%lrVH0td 再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到

1.10 生成配置文件

[root@localhost ~]# vim /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    #由于版本较低,添加mode防止报错

1.11配置服务启动脚本

[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

1.12 启动 mysql

[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
 
 [root@localhost ~]# ps -ef|grep mysql
root        2584       1  0 21:46 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql       2784    2584  0 21:46 pts/0    00:00:00 /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-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root        2825    1625  0 21:48 pts/0    00:00:00 grep --color=auto mysql

[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           128                        [::]:22                       [::]:*                      
LISTEN      0           80                            *:3306                        *:*         

1.13 设置 mysql 服务开机自启

[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

1.14 下载 libncurses.so.5 ,并使用临时密码登录 mysql

[root@localhost ~]# mysql -p
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory    #进入数据库发现报错,缺少库文件

[root@localhost ~]# yum whatprovides libncurses.so.5    #使用yum搜索该依赖库文件
Last metadata expiration check: 0:20:02 ago on Tue 26 Jul 2022 09:32:27 PM CST.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
Repo        : BaseOS
Matched from:
Provide    : libncurses.so.5

[root@localhost ~]# yum install -y ncurses-compat-libs

1.15 设置新密码

[root@localhost ~]# mysql  -p'eI+:%lrVH0td'
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

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('Passwd123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

1.16配置mysql免输密码登录

[root@localhost ~]# vim .my.cnf
[client]	
user=root    #登录用户名
password=123456    #用户密码

2、mysql配置文件

mysql的配置文件为/etc/my.cnf

配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE(不推荐) --> ~/.my.cnf

mysql常用配置文件参数:

参数

说明

port = 3306

设置监听端口

socket = /tmp/mysql.sock

指定套接字文件位置

basedir = /usr/local/mysql

指定MySQL的安装路径

datadir = /data/mysql

指定MySQL的数据存放路径

pid-file = /data/mysql/mysql.pid

指定进程ID文件存放路径

user = mysql

指定MySQL以什么用户的身份提供服务

skip-name-resolve

禁止MySQL对外部连接进行DNS解析 使用这一选项可以消除MySQL进行DNS解析的时间。 若开启该选项,则所有远程主机连接授权都要使用IP地址方 式否则MySQL将无法正常处理连接请求

3、mysql密码破解

1.停⽌MySQL服务
[root@localhost ~]# systemctl stop mysqld
2.编辑MySQL服务配置文件,跳过授权登陆验证
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables	#添加此行,表示跳过授权表

3.开启MySQL服务,免密登陆MySQL
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# mysql	#直接使用mysql命令即可登录

4.登录成功后修改MySQL密码
mysql> UPDATE mysql.user SET authentication_string=PASSWORD("123456") WHERE user='root' and host='localhost';

5.关闭MySQL服务,删除配置文件中添加的跳过授权表配置
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables	#删除该行

6.再启动MySQL服务,使⽤修改后的密码登陆
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# mysql -uroot -p123456

注意

跳过授权表之后,所有的本地和远程⽤户都不需要⽤户名密码认证即可连接,⾮常危险。