Debian9系统安装MySQL5.7&免密登录&破解数据库密码
安装MySQL5.7
root@newbie-unknown85882:~# cd /tmp
root@newbie-unknown85882:/tmp# ls
hwcheck puppetlabs-release-trusty.deb rtime_ntp_dont_rm
root@newbie-unknown85882:/tmp# wget https://dev.mysql.com/get/mysql-apt-config_0.8.6-1_all.deb
--2018-03-14 16:11:21-- https://dev.mysql.com/get/mysql-apt-config_0.8.6-1_all.deb
正在解析主机 dev.mysql.com (dev.mysql.com)... 137.254.60.11
正在连接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://repo.mysql.com//mysql-apt-config_0.8.6-1_all.deb [跟随至新的 URL]
--2018-03-14 16:11:25-- https://repo.mysql.com//mysql-apt-config_0.8.6-1_all.deb
正在解析主机 repo.mysql.com (repo.mysql.com)... 104.91.211.59
正在连接 repo.mysql.com (repo.mysql.com)|104.91.211.59|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:35772 (35K) [application/x-debian-package]
正在保存至: “mysql-apt-config_0.8.6-1_all.deb”
mysql-apt-config_0.8.6-1_all.deb 100%[============================================================================>] 34.93K 229KB/s in 0.2s
2018-03-14 16:11:26 (229 KB/s) - 已保存 “mysql-apt-config_0.8.6-1_all.deb” [35772/35772])
root@newbie-unknown85882:/tmp# ls
hwcheck mysql-apt-config_0.8.6-1_all.deb puppetlabs-release-trusty.deb rtime_ntp_dont_rm
root@newbie-unknown85882:/tmp# dpkg -i mysql-apt-config_0.8.6-1_all.deb
正在选中未选择的软件包 mysql-apt-config。
(正在读取数据库 ... 系统当前共安装有 87263 个文件和目录。)
正准备解包 mysql-apt-config_0.8.6-1_all.deb ...
正在解包 mysql-apt-config (0.8.6-1) ...
正在设置 mysql-apt-config (0.8.6-1) ...
正在设定 mysql-apt-config
-------------------------
MySQL APT Repo features MySQL Server along with a variety of MySQL components. You may select the appropriate product to choose the version that you wish to
receive.
Once you are satisfied with the configuration then select last option 'Ok' to save the configuration. Advanced users can always change the configurations
later, depending on their own needs.
1. MySQL Server & Cluster (Currently selected: mysql-5.7) 3. MySQL Preview Packages (Currently selected: Disabled)
2. MySQL Tools & Connectors (Currently selected: Enabled) 4. Ok
Which MySQL product do you wish to configure? 4
Warning: apt-key should not be used in scripts (called from postinst maintainerscript of the package mysql-apt-config)
OK
root@newbie-unknown85882:/tmp# apt-get update
忽略:1 http://apt.x.netease.com:8660/debian stretch InRelease
命中:2 http://sa.apt.netease.com:8660/debian stretch InRelease
命中:3 http://apt.x.netease.com:8660/debian stretch-updates InRelease
命中:4 http://apt.x.netease.com:8660/debian-security stretch/updates InRelease
命中:5 http://apt.x.netease.com:8660/debian stretch Release
获取:7 http://repo.mysql.com/apt/debian stretch InRelease [19.2 kB]
获取:9 http://repo.mysql.com/apt/debian stretch/mysql-apt-config amd64 Packages [566 B]
获取:10 http://repo.mysql.com/apt/debian stretch/mysql-5.7 amd64 Packages [5,685 B]
获取:11 http://repo.mysql.com/apt/debian stretch/mysql-tools amd64 Packages [2,227 B]
忽略:12 http://apt.puppetlabs.com trusty InRelease
命中:13 http://apt.puppetlabs.com trusty Release
获取:8 http://101.96.8.139/repo.mysql.com/apt/debian stretch/mysql-5.7 Sources [864 B]
已下载 28.5 kB,耗时 11秒 (2,440 B/s)
正在读取软件包列表... 完成
root@newbie-unknown85882:/tmp# apt-get install -y mysql-server
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
将会同时安装下列软件:
libmecab2 libnuma1 mysql-client mysql-community-client mysql-community-server
下列【新】软件包将被安装:
libmecab2 libnuma1 mysql-client mysql-community-client mysql-community-server mysql-server
升级了 0 个软件包,新安装了 6 个软件包,要卸载 0 个软件包,有 86 个软件包未被升级。
需要下载 36.9 MB 的归档。
解压缩后会消耗 254 MB 的额外空间。
获取:1 http://apt.x.netease.com:8660/debian stretch/main amd64 libnuma1 amd64 2.0.11-2.1 [33.3 kB]
获取:2 http://apt.x.netease.com:8660/debian stretch/main amd64 libmecab2 amd64 0.996-3.1 [256 kB]
获取:3 http://repo.mysql.com/apt/debian stretch/mysql-5.7 amd64 mysql-community-client amd64 5.7.21-1debian9 [8,873 kB]
获取:4 http://repo.mysql.com/apt/debian stretch/mysql-5.7 amd64 mysql-client amd64 5.7.21-1debian9 [12.4 kB]
获取:5 http://repo.mysql.com/apt/debian stretch/mysql-5.7 amd64 mysql-community-server amd64 5.7.21-1debian9 [27.7 MB]
获取:6 http://repo.mysql.com/apt/debian stretch/mysql-5.7 amd64 mysql-server amd64 5.7.21-1debian9 [12.4 kB]
已下载 36.9 MB,耗时 9秒 (3,824 kB/s)
正在预设定软件包 ...
正在设定 mysql-community-server
-------------------------------
Please provide a strong password that will be set for the root account of your MySQL database. Leave it blank to enable password less login using UNIX
socket based authentication.
Enter root password:
Now that you have selected a password for the root account, please confirm by typing it again. Do not share the password with anyone.
Re-enter root password:
正在选中未选择的软件包 libnuma1:amd64。
(正在读取数据库 ... 系统当前共安装有 87268 个文件和目录。)
正准备解包 .../0-libnuma1_2.0.11-2.1_amd64.deb ...
正在解包 libnuma1:amd64 (2.0.11-2.1) ...
正在选中未选择的软件包 mysql-community-client。
正准备解包 .../1-mysql-community-client_5.7.21-1debian9_amd64.deb ...
正在解包 mysql-community-client (5.7.21-1debian9) ...
正在选中未选择的软件包 mysql-client。
正准备解包 .../2-mysql-client_5.7.21-1debian9_amd64.deb ...
正在解包 mysql-client (5.7.21-1debian9) ...
正在选中未选择的软件包 libmecab2:amd64。
正准备解包 .../3-libmecab2_0.996-3.1_amd64.deb ...
正在解包 libmecab2:amd64 (0.996-3.1) ...
正在选中未选择的软件包 mysql-community-server。
正准备解包 .../4-mysql-community-server_5.7.21-1debian9_amd64.deb ...
正在解包 mysql-community-server (5.7.21-1debian9) ...
正在选中未选择的软件包 mysql-server。
正准备解包 .../5-mysql-server_5.7.21-1debian9_amd64.deb ...
正在解包 mysql-server (5.7.21-1debian9) ...
正在设置 libnuma1:amd64 (2.0.11-2.1) ...
正在设置 libmecab2:amd64 (0.996-3.1) ...
正在处理用于 libc-bin (2.24-11+deb9u1) 的触发器 ...
正在处理用于 systemd (232-25+deb9u1) 的触发器 ...
正在处理用于 man-db (2.7.6.1-2) 的触发器 ...
正在设置 mysql-community-client (5.7.21-1debian9) ...
正在设置 mysql-client (5.7.21-1debian9) ...
正在设置 mysql-community-server (5.7.21-1debian9) ...
update-alternatives: 使用 /etc/mysql/mysql.cnf 来在自动模式中提供 /etc/mysql/my.cnf (my.cnf)
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
正在设置 mysql-server (5.7.21-1debian9) ...
正在处理用于 systemd (232-25+deb9u1) 的触发器 ...
root@newbie-unknown85882:/tmp# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
免密登录
设置允许Linux的root用户可以不需要密码就能使用MySQL的root用户。有两种方法实现这样的效果,首先介绍一种简单的方法,使用环境变量MYSQL_PWD快速登录:
root@newbie-unknown85882:~# vim /root/.profile
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n || true
export MYSQL_PWD=fai #在配置文件中添加此行,fai为root密码。
".profile" 9L, 148C 已写入
root@newbie-unknown85882:~#
root@newbie-unknown85882:~# source /root/.profile #让更改后的配置文件立即生效
root@newbie-unknown85882:~# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
#现在就可以直接使用mysql命令登录MySQL数据库了!
另一种方法更加安全,使用.my.cnf 快速登录:
root@newbie-unknown85882:~# vim ~/.my.cnf
#在~/目录新建一个.my.cnf文件,如果这个文件已经存在,直接修改即可。
[client]
password=fai
user=root
".my.cnf" [新] 3L, 32C 已写入
root@newbie-unknown85882:~# chmod 400 ~/.my.cnf
#由于.my.cnf文件中明文写了root密码,因此要注意设置这个文件的文件权限。
root@newbie-unknown85882:~# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
#现在就可以直接使用mysql命令登录MySQL数据库了!
如果需要指定一个配置文件而不使用默认的~/.my.cnf的话就需要使用--defaults-file=file_name参数了!
可以来折腾一下:
root@newbie-unknown85882:~# rm -rf ~/.my.cnf
root@newbie-unknown85882:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@newbie-unknown85882:~# cd /home
root@newbie-unknown85882:/home# vim mysql.key
[client]
password=fai
user=root
"mysql.key" [新] 4L, 33C 已写入
root@newbie-unknown85882:/home# mysql --defaults-file=/home/mysql.key
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> ^DBye
#用指定的配置文件已成功登录mysql
root@newbie-unknown85882:/home# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
#现在直接使用mysql命令是不能登录数据库的,看来还是使用默认的~/.my.cnf方便!那就把配置整回去吧!
root@newbie-unknown85882:/home# rm -rf mysql.key
root@newbie-unknown85882:/home# mysql --defaults-file=/home/mysql.key
mysql: [ERROR] Could not open required defaults file: /home/mysql.key
mysql: [ERROR] Fatal error in defaults handling. Program aborted!
root@newbie-unknown85882:/home# vim ~/.my.cnf
[client]
password=fai
user=root
"~/.my.cnf" [新] 4L, 33C 已写入
root@newbie-unknown85882:/home# chmod 400 ~/.my.cnf
root@newbie-unknown85882:/home# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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 respecte
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
#现在又可以直接使用mysql命令直接登录数据库了!
破解数据库密码
假如忘记了数据库密码怎么办?
root@newbie-unknown85882:/home# mysql -u root -p
Enter password: #忘记密码
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root@newbie-unknown85882:/home# vim /etc/mysql/mysql.conf.d/mysqld.cnf
#[mysqld]字段以下添加:
skip-grant-tables
/etc/mysql/mysql.conf.d/mysqld.cnf [+] 23,1 顶端
"/etc/mysql/mysql.conf.d/mysqld.cnf" 46L, 1576C 已写入
root@newbie-unknown85882:/home# systemctl restart mysql
root@newbie-unknown85882:/home# mysql -u root -p
Enter password: (直接点击回车,密码为空,输入任意字符都可以登录;或者直接用mysql命令登录也可以。)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> update user set authentication_string=password('fai123') where user='root';
ERROR 1046 (3D000): No database selected #需要切换到mysql库,不切换库就会报错。
mysql> use 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> update user set authentication_string=password('fai123') where user='root';#把mysql数据库密码改为fai123
Query OK, 1 row affected, 1 warning (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
root@newbie-unknown85882:/home# vim /etc/mysql/mysql.conf.d/mysqld.cnf
#把配置文件里的skip-grant-tables删掉
root@newbie-unknown85882:/home# systemctl restart mysql
root@newbie-unknown85882:/home# mysql -u root -p
Enter password: (此时mysql数据库密码已为变fai123)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
#现在能重新登录数据库了!
破解密码告一段落,接下来还原之前的配置。
mysql> use 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('fai') where user='root';#更改密码
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> ^DBye
root@newbie-unknown85882:/home# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> ^DBye
root@newbie-unknown85882:/home# vim ~/.my.cnf
[client]
password=fai
user=root
~/.my.cnf [+] 1,4 全部
"~/.my.cnf" [新] 4L, 33C 已写入
root@newbie-unknown85882:/home# chmod 400 ~/.my.cnf
root@newbie-unknown85882:/home# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> ^DBye
网友评论