问题发现
Ubuntu在终端通过指令
sudo apt-get update
sudo apt-get install mysql-server
安装MySQL时正常情况应该会出现一个窗口要求设定初始root用户密码,但我在安装过程中并未出现,在随后启动并用root用户登录MySQL时
#1.启动 2.root用户登录
service mysql start
mysql -u root -p
发现要求输入root用户的密码,几次尝试后发现root用户初始密码不是Ubuntu用户密码或其他我能猜到的值,输入错误会报错
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
解决策略
执行如下指令查看mysql初始设置好的一个用户
sudo cat /etc/mysql/debian.cnf
随后可以看到类似如下信息
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = 38gDNRwYGm0DprMR
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = 38gDNRwYGm0DprMR
socket = /var/run/mysqld/mysqld.sock
可以看到该初始用户的 账号为debian-sys-maint 密码为 38gDNRwYGm0DprMR
然后我们用该账号登录mysql
mysql -u debian-sys-maint -p
输入密码 38gDNRwYGm0DprMR 登录 【 Tips 可直接将密码从上面复制到输入区】
在mysql指令模式下修改root用户密码,依次执行如下指令
mysql> use mysql;
mysql> update mysql.user set authentication_string=password('newpassword') where user='root'and Host = 'localhost';
mysql> update user set plugin="mysql_native_password";
mysql> flush privileges;
mysql> quit;
用修改后的密码即可登录root用户 ↓ 成功登录
hadoop@Leobiggu:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.25-0ubuntu0.18.10.2 (Ubuntu)Copyright (c) 2000, 2019, 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>
附1:将密码改为 hadoop 实例
附2:小细节
1、在MySQL指令模式下,输入一条完整指令后一定要加分号 ; !!!!!
若不加分号直接回车的话会另起一行,且该行指令实际会与上一行拼在一起,直到后面遇到分号形成一条完整指令才能被执行
2、可以用 sudo mysql -u root -p 实现无密码(可用任意密码)登录root用户
附3:MySQL完全卸载指令
#依次执行以下三条,过程可能出现卸载确认窗口,选YES即可
sudo apt-get autoremove --purge mysql-server
sudo apt-get remove mysql-common
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P