解决 ubuntu下mysql 14.14 未设置密码或忘记密码

一、重置密码

1、忘记密码情况下进入mysql

方法一:

sudo cat /etc/mysql/debian.cnf
debian.cnf 里面有一个debian-sys-maint用户,这个用户只有Debian或Ubuntu服务器才有,debian-sys-maint是个
Mysql安装自带的用户,具体作用是重启及运行mysql服务。如果忘了root密码,可以通过这个用户来重设密码。
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = 0KyepIunhkSPalP0
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = 0KyepIunhkSPalP0
socket   = /var/run/mysqld/mysqld.sock
basedir  = /usr

 

使用这个文件中的用户名和密码进入mysql:

mysql -u debian-sys-maint -p

注意!输入的密码是由第一条指令执行结果的 password = 0KyepIunhkSPalP0 得到,注意输入你自己的密码

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 182
Server version: 5.5.54-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2016, 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.

方法二:

修改 etc/mysql/my.cnf 文件, 在 [mysqld]  后面添加一句    skip-grant-tables ,跳过数据库验证

重启mysql

service mysql restart

输入 mysql 回车

注意:修改完成后,删除/etc/my.conf文件中[mysqld] 下面的skip-grant-tables ,再重启mysql

 

 

2、修改密码

选择mysql数据库(用户名和密码均存储在此数据库的user表中)

use mysql;

 

查看user表中的列

show fields from user;  或者(describe user;)

打印如下内容:可以看到表中存在 Password 这列 用来存放密码(注:新的版本这个字段是authentication_string,如果是,下面的操作将Password替换成authentication_string即可)

+------------------------+-----------------------------------+------+-----+---------+-------+
| Field                  | Type                              | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host                   | char(60)                          | NO   | PRI |         |       |
| User                   | char(16)                          | NO   | PRI |         |       |
| Password               | char(41)                          | NO   |     |         |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N       |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N       |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N       |       |
| File_priv              | enum('N','Y')                     | NO   |     | N       |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N       |       |
| References_priv        | enum('N','Y')                     | NO   |     | N       |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N       |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N       |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N       |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N       |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N       |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |
| ssl_cipher             | blob                              | NO   |     | NULL    |       |
| x509_issuer            | blob                              | NO   |     | NULL    |       |
| x509_subject           | blob                              | NO   |     | NULL    |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0       |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0       |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0       |       |
| max_user_connections   | int(11) unsigned                  | NO   |     | 0       |       |
| plugin                 | char(64)                          | YES  |     |         |       |
| authentication_string  | text                              | YES  |     | NULL    |       |
+------------------------+-----------------------------------+------+-----+---------+-------+
42 rows in set (0.00 sec)

 

修改密码,本文将密码修改成 123456 , 用户可自行定义

//注意:命令后的分号不要忘了
update mysql.user set Password=password('123456') where user='root'; update user set plugin="mysql_native_password"; flush privileges; quit;

 

重启mysql

service mysql restart

二、修改密码

1. 查看mysqladmin命令

sudo mysqladmin  

password选项是用于修改密码的

 ···········  
 flush-privileges Reload grant tables (same as reload) kill id,id,... Kill mysql threads password [new-password] Change old password to new-password in current format old-password [new-password] Change old password to new-password in old format ping Check if mysqld is alive processlist Show list of active threads in server reload Reload grant tables refresh Flush all tables and close and open logfiles
  ···········  

2. 修改密码

sudo mysqladmin -u root -p password

提示 输入旧密码,设置新密码:

Enter password: 
New password: 
Confirm new password: 

3. 重启mysql服务

sudo service mysql restart

 

三、修改port

修改 mysqld.cnf 文件里的port即可

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

 

四、添加用户

   1.进入mysql

sudo mysql -u root -p

   输入密码,进入mysql

  2.添加用户

insert into mysql.user(Host,User,Password) values("localhost","test",password("123456"));

 

 原文链接:

https://blog.csdn.net/skh2015java/article/details/80156278

上一篇:petalinux环境安装和基本编译


下一篇:effective C++读书笔记(条款02) 2020/12/15