Files
vault-para/000-inbox/clippings/2024/11/MySQL Change a User Password Command Tutorial - nixCraft.md
T

119 lines
6.5 KiB
Markdown
Raw Normal View History

2025-12-29 13:38:39 +08:00
---
page-title: "MySQL Change a User Password Command Tutorial - nixCraft"
url: https://www.cyberciti.biz/faq/mysql-change-user-password/
date: "2024-11-01 14:51:07"
---
> ALTER USER 'user'@'hostname' IDENTIFIED BY 'newPass';
---
[![See all MySQL Database Server related FAQ](https://www.cyberciti.biz/media/new/category/old/mysqllogo.gif)](https://www.cyberciti.biz/faq/category/mysql/ "See all MySQL Database Server related FAQ")
I would like to change a password for a user called tom using UNIX / Linux command line option. How do I change a user password on MySQL server?
You need to use mysql (or mysql.exe on MS-Windows based system) command on a Linux or Unix like operating system. Open a terminal app or ssh session. Type the following command at the shell prompt to login as a root user. The syntax is as follows for Unix like operating system.
| Tutorial details |
| --- |
| Difficulty level | [Easy](https://www.cyberciti.biz/faq/tag/easy/ "See all Easy Linux / Unix System Administrator Tutorials") |
| Root privileges | No |
| Requirements | Linux or Unix terminal |
| Category | [Database Server](https://www.cyberciti.biz/faq/mysql-change-user-password/#Database_Server "See ALL other tutorials in 'Database Server' category") |
| OS compatibility | BSD • [Linux](https://www.cyberciti.biz/faq/category/linux/ "See all Linux distributions tutorials") • [macOS](https://www.cyberciti.biz/faq/category/mac-os-x/ "See all macOS (OS X) tutorials") • [Unix](https://www.cyberciti.biz/faq/category/unix/ "See all Unix tutorials") • [Windows](https://www.cyberciti.biz/faq/category/windows/ "See all MS Windows OS compatible tutorials") • WSL |
| Est. reading time | 3 minutes |
## How to change user password on mysql
Mysql change user password using the following method:
1. Open the bash shell and connect to the server as root user:
**mysql -u root -h localhost -p**
2. Run ALTER mysql command:
**ALTER USER 'userName'@'localhost' IDENTIFIED BY 'New-Password-Here';**
3. Finally type SQL command to reload the grant tables in the mysql database:
FLUSH PRIVILEGES;
Please note that use mysql.exe on MS-Windows host as follows (first change directory where mysql.exe is located \[example: “C:\\Program Files\\mysql\\mysql-5.0.77-win32\\bin“\]. Let us see examples and syntax in details.
## mysql command to change a user password
Login as root from the shell:
`$ mysql -u root -p`
Or admin user that can do DBA duties. For example:
`$ mysql -u admin -h 10.83.200.253 -p`
Where,
- \-u root OR \-u admin : MySQL server admin user name (root is default on most systems).
- \-h 10.83.200.253 : MySQL server IP address or hostname such as server1.cyberciti.biz.
- \-p : Prompt for the password.
Switch to mysql database (type command at mysql> prompt, do not include string “mysql>”):
`mysql> **use mysql;**`
The syntax is as follows for **mysql database server version 5.7.5** or older:
SET PASSWORD FOR 'user-name-here'@'hostname' \= PASSWORD('new-password');
For **mysql database server version 5.7.6 or newer** use the following syntax:
ALTER USER 'user'@'hostname' IDENTIFIED BY 'newPass';
You can also use the following sql syntax:
UPDATE mysql.user SET Password\=PASSWORD('new-password-here') WHERE USER\='user-name-here' AND Host\='host-name-here';
In this example, change a password for a user called tom:
SET PASSWORD FOR 'tom'@'localhost' \= PASSWORD('foobar');
OR
UPDATE mysql.user SET Password\=PASSWORD('foobar') WHERE USER\='tom' AND Host\='localhost';
Sample outputs:
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Feel free to replace the values for “tom” (user), “localhost” (hostname), and “foobar” (password) as per your requirements. Finally, type the following command to reload privileges:
Sample outputs:
Query OK, 0 rows affected (0.00 sec)
To exit from mysql> prompt, enter:
## Changing the MySQL root or user password using the mysqladmin command
We can also use the mysqladmin CLI to alter the MySQL password. The syntax for the mysqladmin command is as follows:
`$ mysqladmin --user={USER_NAME} password "{NEW_PASSWORD_HERE}" $ mysqladmin --user=root password "5b350f65542fdb74e74ef7b815f86ad5" $ mysqladmin --user=root --host=192.168.2.200 --password password "5b350f65542fdb74e74ef7b815f86ad5"`
Where,
- \--user=root : User for login if not current user.
- \--password : Prompt for password to use when connecting to server.
- \--host=192.168.2.200 : Connect to MySQL server host by given IP address or hostname.
- password "5b350f65542fdb74e74ef7b815f86ad5" : Change old password to “5b350f65542fdb74e74ef7b815f86ad5” in current format.
### Verify the new password settings
User or you can test new password using the following shell syntax:
`$ mysql -u tom -p`
When promoted enter new password you set earlier for tom user.
## Sample session
[![Fig.01: Mysql Updating / Changing password (click to enlarge)](https://www.cyberciti.biz/media/new/faq/2007/07/mysql-update-password-300x232.png "HowTo: Mysql Update Password SQL Command")](https://www.cyberciti.biz/faq/mysql-change-user-password/mysql-update-password/)
Fig.01: Mysql Updating / Changing password (click to enlarge)
## Summing up
You learned how to change MySQL or MariaDB user password using the mysql command line on Linux, Unix, macOS, \*BSD and Windows operating systems. For more info please read the mysql manual page by typing the [man command](https://bash.cyberciti.biz/guide/Man_command "Man command - Linux Bash Shell Scripting Tutorial Wiki") or passing the [\--help option](https://bash.cyberciti.biz/guide/Help_command "help command - Linux Bash Shell Scripting Tutorial Wiki") under Unix-like systems. For instance:
`$ man mysql $ mysql --help`
🥺 Was this helpful? Please add [a comment to show your appreciation or feedback](https://www.cyberciti.biz/faq/mysql-change-user-password/#respond "Please add your comment below ↓ to show your appreciation or feedback to the author").
![nixCrat Tux Pixel Penguin](https://www.cyberciti.biz/media/new/cms/2024/04/tux_96.png)
Hi! 🤠
I'm Vivek Gite, and I write [about](https://www.cyberciti.biz/tips/about-us "About the author and nixCraft") Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my [RSS feed](https://www.cyberciti.com/atom/atom.xml "Get nixCraft updates using RSS feed") or [email newsletter](https://newsletter.cyberciti.com/subscription?f=1ojtmiv8892KQzyMsTF4YPr1pPSAhX2rq7Qfe5DiHMgXwKo892di4MTWyOdd976343rcNR6LhdG1f7k9H8929kMNMdWu3g "Get nixCraft updates using Email") for updates.