Перейти к содержимому

Mysql как узнать пароль root

  • автор:

MySQL Default Password – How To Find And Reset It

When installing MySQL, you may noticed that it does not ask for a password. This become troublesome when you want to connect your application to MySQL database. In this article, I will show you on how to find MySQL default password.

MySQL Default Password

Well, straight forward, by default the username is root and there is no password setup. You may need to reset the root password.

Also, in case you have accidently put a password during installation process and can’t recall the password, you need to reset the password.

There is no way to view the password as it’s already written as hashed.

How To Reset MySQL Default Password

Windows OS

1. Ensure that your logged on account has administrator rights.

2. On the windows search box, search for services.msc and click to open.

3. Scroll down to all services with its status. Find MySQL services, right-click on it and click stop services.

4. Create a text file which contains the SQL statement in a single line as below:

ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass’;

Change MyNewPass to your new desired password.

5. Save it to a text file. For example, save it as C:\new-password.txt.

6. On the windows search box, search for cmd and click Run as administrator.

7. Start the MySQL with init_file system variable set to text file name created as below:

C:\> cd “C:\Program Files\MySQL\MySQL Server 5.7\bin” C:\> mysqld –init-file=C:\\mysql-init.txt

You may replace your MySQL installation location after cd command.

Linux

1. Open terminal.

2. Stop MySQL server.

sudo service mysql stop

sudo /usr/local/mysql/support-files/mysql.server stop

3. Start MySQL in safe mode.

sudo mysqld_safe –skip-grant-tables

4. Open another terminal and login as root by run below command.

3. Once MySQL is logged in via terminal, run below queries.

UPDATE mysql.user SET authentication_string=PASSWORD(‘password’) WHERE User=’root’; FLUSH PRIVILEGES;

which be looks like:

mysql>UPDATE mysql.user SET authentication_string=PASSWORD(‘password’) WHERE User=’root’; FLUSH PRIVILEGES;

(Note: In MySQL 5.7, the password field in mysql.user table field was removed, now the field name is ‘authentication_string’.)

If you are using MySQL 5.7 and above you need to run command as below:

mysql> use mysql; mysql>update user set authentication_string=password(‘password’) where user=’root’; FLUSH PRIVILEGES;

4. Now, you can exit MySQL safe mode.

If you received error ‘access denied’ you can run below command with the new password:

mysqladmin -u root -p shutdown

5. Start MySQL service again by run below command.

sudo service mysql start

What If Still Failed To Reset MySQL Default Password?

If by using ALTER USER still not able to reset password, you may try to modify the user table directly by repeating the steps above and run below query:

How To Set, Change, and Retrieve Your MySQL Root Password

MySQL Commands

Ever forgotten or just wanted to change your MySQL password? You’ve come to the right place – in this guide, we’ll walk you through how to set, change and recover your MySQL root password as well as other useful MySQL commands.

RunCloud uses MariaDB, an enhanced drop-in replacement for MySQL, as a database. MariaDB is a community-driven package by the original developers of MySQL, with the promise of future compatibility, hence, terminal commands and software tools for MySQL database can be used for MariaDB as well.

On RunCloud, there are a few ways to manage your MySQL server and database. You can manage either by accessing the server using a terminal and run the commands from there or you can safely install a MySQL database manager like phpMyAdmin as a web app using the RunCloud script installer.

Some administrators do not like to use a public-facing web app like phpMyAdmin because of the potential security vulnerabilities that it poses. But it remains popular as it is fairly simple to use, even for non-technical people, and it provides an easily accessible tool that can save you time when you just need to run some simple tasks.

However, if you want to avoid any of these risks then SSH is the way to go. If you have no prior experience managing MySQL, don’t worry, with RunCloud, it is as easy as ABC.

First, you need a terminal to work on. On a Windows machine, you can download the PuTTY software. If you are on a Mac or Linux, there is already a built-in Terminal app installed.

The MySQL root password that is displayed on your terminal screen at the end of the RunCloud agent installation on your server (example below is installation on DigitalOcean) is meant to be kept for safekeeping elsewhere, but if you forgot to copy it down, you can still retrieve it.

Retrieve your root password User

Pic1: Keep the MySQL password somewhere safe!

How to retrieve MySQL root password

  1. Log in as root into your server through SSH (eg: puTTY/terminal/bash). Alternatively, run the commands that follow as su or sudo as root user. This method is safer.
  2. Navigate to /etc/mysql
  3. View the file my.cnf either using the command cat or use any text editing software (vi/vim/nano).

Retrieve your root password Green

Pic 2: Viewing the MySQL root password

How to Reset/Change MySQL Password

  1. Login to your server using SSH.These commands must be run with root user privileges. Although you can login to your server as root user, it is highly recommended not to. If you can, log in to your server as a system user with root privileges and run these commands as su or sudo commands using those administrative privileges. You can also close the incoming traffic for MySQL port (default port is 3306 unless you changed it to something else) temporarily at the firewall if you want to be extra careful.
  2. Stop the MySQL service.
  3. Restart MySQL service in single-user mode and bypassing password authorization, by running the command below. Since the command —skip-grant-tables enables anyone to connect without a password, it is considered highly insecure, hence, all commands that follow must be done within a brief period. The ampersand (symbol &) at the end is required as it ensures that the command is being run parallel with the commands that follow afterwards.
  4. Login into MySQL to connect.
  5. At the MariaDB> prompt, change the root password by running the commands below. Replace the NEWSTRONGPASSWORD with your chosen new strong password.
  6. At the MariaDB> prompt, run the following commands.
  7. Stop the MySQL server using the following command. Before the MySQL server shuts down, you will be prompted to key in the root password. Key in your new root password.
  8. Start the MySQL server in normal mode.
  9. Verify that you are able to connect to MySQL by running the command mysql again. If you disable incoming traffic for the mysql port, please remember to enable it again.
  10. Lastly, you need to update the MySQL root password in the file /etc/mysql/my.cnf (as shown in Pic2) or you might not be able to add new database or user. You can use vi, vim or nano to edit the file.

Retrieve your root password Blue

Pic 3: Resetting the MySQL root password

Other Basic MySQL Commands

Even though you can run some basic MySQL commands in the RunCloud control panel, it would not hurt to know how to do the same tasks in command line. Below are some of the basic commands that should get you going.

When you run the commands below, replace the database name of your choice at the DatabaseNameHere.

To show the available databases.

Creating a database.

Delete a database. You need a ‘drop’ privilege (or root user) to drop a database and the database must not be used (open for reading or writing by any user) at the particular time. Be careful when running this command as it will not prompt for confirmation and it will drop all the tables inside the database.

To use or operate on a particular database. The MySQL prompt will change to reflect the database name that you have specified.

To find out what database you are currently operating in, run the following command.

To create a MySQL account. Change the UsernameHere and PasswordHere according to you preferences.

To grant the user privileges. (This user will only be able to connect only from the local host)

These are just some of the basic commands that you can run using the command line. The same commands above can also be done using the RunCloud control panel. If you are new to administering a MySQL server using command line, getting used to these commands is a good start!

How to find out the MySQL root password

I cannot figure out my MySQL root password; how can I find this out? Is there any file where this password is stored?

I am following this link but I do not have directadmin directory in local.

Ben's user avatar

21 Answers 21

thanks to @thusharaK I could reset the root password without knowing the old password.

On ubuntu I did the following:

Then run mysql in a new terminal:

And run the following queries to change the password:

In MySQL 5.7, the password field in mysql.user table field was removed, now the field name is ‘authentication_string’.

Quit the mysql safe mode and start mysql service by:

You can’t view the hashed password; the only thing you can do is reset it!

Start it in safe mode:

(above line is the whole command)

This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:

MySQL 5.7 and over:

Exit the MySQL CLI:

Restart MySQL in normal mode, first stopping the safe mode instance:

Your new password is ‘password’.

Reg Edit's user avatar

tk_'s user avatar

MySQL 5.7 and above saves root in MySQL log file.

Please try this:

One thing that tripped me up on a new install of MySQL and wondering why I couldn’t get the default password to work and why even the reset methods where not working. Well turns out that on Ubuntu 18 the most recent version of MySQL server does not use password auth at all for the root user by default. So this means it doesn’t matter what you set it to, it won’t let you use it. It’s expecting you to login from a privileged socket.

This will not work, even if you are using the correct password.

Instead, you need to use:

that will work with out any password. then once you in you need type in

Then log out and now it will accept your password.

Jakov's user avatar

Kit Ramos's user avatar

Follow these steps to reset password in Windows system

Stop Mysql service from task manager

Create a text file and paste the below statement

MySQL 5.7.5 and earlier:

SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘yournewpassword’);

MySQL 5.7.6 and later:

ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘yournewpassword’;

Save as mysql-init.txt and place it in ‘C’ drive .

Open command prompt and paste the following

C:\> mysqld —init-file=C:\\mysql-init.txt

Lokesh kumar Chippada's user avatar

You cannot find it. It is stored in a database, which you need the root password to access, and even if you did get access somehow, it is hashed with a one-way hash. You can reset it: How to Reset the Root Password

Kuro Neko's user avatar

This worked for me:

On terminal type the following

$ sudo mysql -u root -p

Enter password://just press enter

Unless the package manager requests you to type the root password during installation, the default root password is the empty string. To connect to freshly installed server, type:

To change the password, get back the unix shell and type:

The new password is ‘root’. Now connect to the server:

Oops, the password has changed. Use the new one, root :

Bingo! New do something interesting

As addition to the other answers, in a cpanel installation, the mysql root password is stored in a file named /root/.my.cnf . (and the cpanel service resets it back on change, so the other answers here won’t help)

you can view mysql root password , well i have tried it on mysql 5.5 so do not know about other new version well work or not

The default password which worked for me after immediate installation of mysql server is : mysql

LinusGeffarth's user avatar

The procedure changes depending the version of MySql. Follow the procedure exactly as described for your version:

HINTS — Read before the instructions page for your version of MySql*

In step 5: Instead of run CMD, create a shortcut on your desktop calling CDM.exe. Then right-click on the shortcut and select «Execute as Administrator».

In step 6: Skip the first proposed version of the command and execute the second one, the one with the —defaults-file parameter

Once you execute the command, if everything is ok, the CMD window remains open and the command of step 6 continues executing. Simply close the window (click ‘x’), and then force close MySQl from the Task Manager.

Delete the file with the SQL commands, and start again MySQL. The password must be changed now.

. just change the version in the link (5.5, 5.6, 5.7)

In your «hostname».err file inside the data folder MySQL works on, try to look for a string that starts with:

«A temporary password is generated for roor@localhost «

then slash command followed by the string you wish to look for

Then press n, to go to the Next result.

nDCasT's user avatar

I solved this a different way, this may be easier for some.

I did it this way because I tried starting in safe mode but cannot connect with the error: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

What I did was to connect normally as root:

Then I created a new super user:

Then log in as myuser

Trying to change the password gave me no errors but did nothing for me so I dropped and re-created the root user

The root user is now working with the new password

Using Debian / Ubuntu mysql packages, you can login with user debian-sys-maint , which has all the expected privileges, the password is stored in the file /etc/mysql/debian.cnf

Answers provided here did not seem to work for me, the trick turned out to be: ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘test’;

Kuro Neko's user avatar

System:

  • CentOS Linux 7
  • mysql Ver 14.14 Distrib 5.7.25

Procedure:

Open two shell sessions, logging in to one as the Linux root user and the other as a nonroot user with access to the mysql command.

In your root session, stop the normal mysqld listener and start a listener which bypasses password authentication (note: this is a significant security risk as anyone with access to the mysql command may access your databases without a password. You may want to close active shell sessions and/or disable shell access before doing this):

# systemctl stop mysqld
# /usr/sbin/mysqld —skip-grant-tables -u mysql &

In your nonroot session, log in to mysql and set the mysql root password:

$ mysql
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘MyNewPass’);
Query OK, 0 rows affected, 1 warning (0.01 sec)

In your root session, kill the passwordless instance of mysqld and restore the normal mysqld listener to service:

# kill %1
# systemctl start mysqld

In your nonroot session, test the new root password you configured above:

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
.
mysql>

Sorry, you have been blocked

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.

Cloudflare Ray ID: 7a25f059de622dea • Your IP: Click to reveal 88.135.219.175 • Performance & security by Cloudflare

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *