В последнее время приходится часто сталкиваться со сменой/установкой паролей для пользователей баз данных. Вот небольшой пример как это можно сделать легко и непринужденно через консоль.
Для того чтобы привязать пароль в момент создания аккаунта при помощи CREATE USER, добавляем IDENTIFIED BY:
mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
Для установки или изменения пароля пользователя используем SET PASSWORD:
mysql> SET PASSWORD FOR 'user'@'localhost' = PASSWORD('password');
Только такие пользовати как root имеющие доступ на изменения в базе mysql могут менять пароли для остальных пользователей. Если у вас не анонимное соединение с базой данных, то вы можете сменить свой пароль, исключив пункт FOR:
mysql> SET PASSWORD = PASSWORD('password');
You can also use a
GRANT USAGEstatement at the global level (ON *.*) to assign a password to an account without affecting the account’s current privileges:mysql>GRANT USAGE ON *.* TO 'jeffrey'@'localhost' IDENTIFIED BY 'biscuit';Passwords can be assigned from the command line by using the mysqladmin command:
shell>mysqladmin -uuser_name-hhost_namepassword "newpwd"The account for which this command resets the password is the one with a
usertable row that matchesuser_namein theUsercolumn and the client host from which you connect in theHostcolumn.Although it is generally preferable to assign passwords using one of the preceding methods, you can also do so by modifying the
usertable directly:
- To establish a password when creating a new account, provide a value for the
Passwordcolumn:shell>mysql -u root mysqlmysql>INSERT INTO user (Host,User,Password)->VALUES('localhost','jeffrey',PASSWORD('biscuit'));mysql>FLUSH PRIVILEGES;- To change the password for an existing account, use
UPDATEto set thePasswordcolumn value:shell>mysql -u root mysqlmysql>UPDATE user SET Password = PASSWORD('bagel')->WHERE Host = 'localhost' AND User = 'francis';mysql>FLUSH PRIVILEGES;When you assign passwords using
CREATE USERorGRANTwith anIDENTIFIED BYclause or with the mysqladmin password command, they take care of encrypting the password for you.When you assign an account a nonempty password using
SET PASSWORD,INSERT, orUPDATE, you must use thePASSWORD()function to encrypt the password.PASSWORD()is necessary because theusertable stores passwords in encrypted form, not as plaintext. If you forget that fact, you are likely to set passwords like this:shell>mysql -u root mysqlmysql>INSERT INTO user (Host,User,Password)->VALUES('localhost','jeffrey','biscuit');mysql>FLUSH PRIVILEGES;The result is that the literal value
'biscuit'is stored as the password in theusertable, not the encrypted value. Whenjeffreyattempts to connect to the server using this password, the value is encrypted and compared to the value stored in theusertable. However, the stored value is the literal string'biscuit', so the comparison fails and the server rejects the connection:shell>mysql -u jeffrey -pbiscuit testAccess deniedNote
PASSWORD()encryption differs from Unix password encryption. See Section 5.5.1, “User Names and Passwords”.
Хороший пост! Подчерпнул для себя много нового и интересного! Пойду ссылку другу дам в аське
Link | Ноябрь 18th, 2010 at 0:16
Неплохой пост, но много лишнего.
Link | Ноябрь 19th, 2010 at 14:09
Разительно интересно. Только чего-то не хватает. Может иметься, стоит добавить каких-нибудь картинок тож фото?
Link | Декабрь 2nd, 2010 at 10:57