English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to securely store usernames and passwords in a MySQL database?

To securely store usernames and passwords in the MySQL database, we can use MD5()。

Let's look at an example. First, we will create a table. The CREATE command is used to create tables.

mysql> create table UserNameAndPasswordDemo
- > (
- > U_Id int(10) unsigned NOT NULL AUTO_INCREMENT,
- > UserId varchar(255) DEFAULT NULL,
- > UserPassword varchar255) DEFAULT NULL,
- > primary key(U_Id),
- > UNIQUE KEY `UserId` (`UserId`)
- > ;

With MD5() Inserting records and securely storing passwords.

mysql> INSERT INTO UserNameAndPasswordDemo(UserId, UserPassword) VALUES ('[email protected]', MD5('john123'));
mysql> INSERT INTO UserNameAndPasswordDemo(UserId, UserPassword) VALUES (MD5('[email protected]'), MD5('123Carol'));

Now, we will implement the SELECT statement to display the records, including MD5The password. Here, we only display the records of UserId '[email protected]'.

mysql> SELECT *from UserNameAndPasswordDemo where UserId='[email protected]';

Below is the output.

+------+-------------+----------------------------------+
| U_Id | UserId  | UserPassword  |
+------+-------------+----------------------------------+
| 1    | [email protected] | 6e0b7076126a29d5dfcbd54835387b7b |
+------+-------------+----------------------------------+
1 row in set (0.00 sec)

Display all records.

mysql> SELECT *from UserNameAndPasswordDemo;

Below is the output, where we use MD5Saved username and password-

+------+----------------------------------+----------------------------------+
| U_Id | UserId  | UserPassword  |
+------+----------------------------------+----------------------------------+
| 1    | [email protected]  | 6e0b7076126a29d5dfcbd54835387b7b |
| 2    | 5f565a3d794f85e5db4f3bb7b5811a25 | f1d2fb85f7d6ce7428b9b3fd569be42b |
+------+----------------------------------+----------------------------------+
2 rows in set (0.00 sec)