search
HomeDatabaseMysql TutorialMySQLroot用户忘记密码解决方案(安全模式,修改密码的三种方式)_MySQL

1.关闭正在运行的MySQL
2.启动MySQL的安全模式,命令如下:

mysqld --skip-grant-tables

or

mysqld-nd --skip-grant-tables
3.使用root用户[免密码]登陆MySQL
mysql -u root -p

输入密码时,直接回车
4.选择MySQL系统库
use mysql


5.查看当前系统用户root的密码
select user,host,password from user where user="root"

查看的password是经过加密的,若以后想要恢复当前密码可以先运行这条命令备份一下当前的密码
6.修改root用户的密码
update user set password=PASSWORD("your_password") where user="root"

这里是直接修改了root用户在所有登陆位置的密码,若你仅仅只想修改root在某一处的密码,可以在上一条命令中增加一个限定条件host='somewhere'
比如,下面的命令修改了root用户在本机localhost的登陆密码
update user set password=PASSWORD("your_password") where user="root" and host="localhost"

上面的操作是直接对MySQL系统库mysql进行修改,安全性较低,一旦出现误操作,成本高,难恢复,并且仅限于对mysql库有UPDATE权限的用户,MySQL本身为我们提供了一种更加简便的操作方式,在此作一下简单的介绍
修改当前登陆用户的密码,使用SELECT CURRENT_USER();可查看当前登陆用户
SET PASSWORD = PASSWORD('cleartext password');

修改bob用户在%.example.org位置上的登陆密码,注意这里的host地址%.example.org是必须要存在的

SET PASSWORD FOR 'bob'@'%.example.org' = PASSWORD('cleartext password');

当然我们也可以通过GRANT的方式修改密码
GRANT USAGE ON *.* TO 'bob'@'%.example.org' IDENTIFIED BY 'cleartext password';

关于修改密码的详细内容还是请见官方文档(5.6)
http://dev.mysql.com/doc/refman/5.6/en/set-password.html
7.刷新一下系统的权限
flush privileges;

8.关闭MySQL的安全模式,重新启动即可

注:

在第2步,启动安全模式的时候,命令行可能会一直处于挂起状态,此时Ctrl+c也不能终止运行,这时候只要通过netstat -ao查看MySQL端口是否处于监听状态,如是即代表MySQL已经进入了安全模式,出现这种现象是主要因为MySQL不提倡安全模式长时间运行

使用mysql命令连接数据库时可能会出现如下错误:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
网上关于此错误的描述有很多,我们知道MySQL的默认端口是3306,当以其他端口启动服务时,使用mysql命令又没有指定对应的端口,当然就无法连接Server啦
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL's Role: Databases in Web ApplicationsMySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: Building Your First DatabaseMySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AM

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL: A Beginner-Friendly Approach to Data StorageMySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AM

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use