Debian9系统安装MySQL5.7&免密登录&破解数据库密码

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,743评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,296评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,285评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,485评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,581评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,821评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,960评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,719评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,186评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,516评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,650评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,329评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,936评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,757评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,991评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,370评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,527评论 2 349

推荐阅读更多精彩内容