mysqlroot⽤户设置密码
下⾯的⽂章主要介绍了⽤户密码的设置⽅式以及密码的修改⽅式以及mysql⾃带的测试库test库安全建议
A root account password can be set several ways. The following discussion demonstrates three methods:
#使⽤set password⽅式
Use the SET PASSWORD statement #使⽤set password⽅式
#使⽤update⽅式
Use the UPDATE statement #使⽤update⽅式
#使⽤mysqladmin⽅式
Use the mysqladmin
mysqladmin command-line client program #使⽤mysqladmin⽅式
To assign passwords using SET PASSWORD, connect to the server as root and issue a SET PASSWORD statement for each root account listed in the mysql.user table.
For Windows, do this:(windows设置⽅式)
mysql -u root
shell>mysql -u root
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
mysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('
mysql>SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('
SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'::1' = PASSWORD('newpwd');
mysql>SET PASSWORD FOR 'root'@'::1' = PASSWORD('
SET PASSWORD FOR 'root'@'%' = PASSWORD('newpwd');
mysql>SET PASSWORD FOR 'root'@'%' = PASSWORD('
The last statement is unnecessary if themysql.usertable has norootaccount with a host value of%.
For Unix, do this:
shell>mysql -u root #使⽤root帐号登录,登陆后修改相应的密码
mysql -u root #使⽤root帐号登录,登陆后修改相应的密码
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
mysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('
mysql>SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('
SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'::1' = PASSWORD('newpwd');
mysql>SET PASSWORD FOR 'root'@'::1' = PASSWORD('
SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpwd');
mysql>SET PASSWORD FOR 'root'@'
You can also use a single statement that assigns a password to all root accounts by using UPDATE to modify the mysql.user table directly. This method works on any platform:
由于mysql的⽤户是由⽤户名和授权登录的地址段组成,可以使⽤下⾯的命令⼀次性更新所有的root密码
shell>mysql -u root
mysql -u root
WHERE User = 'root';
mysql>UPDATE mysql.user SET Password = PASSWORD('
UPDATE mysql.user SET Password = PASSWORD('newpwd')') WHERE User = 'root';
#更新mysql user表中的root的密码
FLUSH PRIVILEGES; (注意update⽅式更新的密码,必须刷新权限,如果不刷新更新的密码不⽣效)
mysql>FLUSH PRIVILEGES; (注意update⽅式更新的密码,必须刷新权限,如果不刷新更新的密码不⽣效)
The FLUSH statement causes the server to reread the grant tables. Without it, the password change remains unnoticed by
the server until you restart it.
mysqladmin, execute the following commands:
To assign passwords to the root accounts using mysqladmin
#mysqladmin⽅式更新⽤户密码(windows和linux都有效)
mysqladmin -u root password "newpwd"
shell>mysqladmin -u root password "
mysqladmin -u root -h host_name password "newpwd"
shell>mysqladmin -u root -h
Those commands apply both to Windows and to Unix. The double quotation marks around the password are not always necessary, but you should use them if the password contains spaces or other characters that are special to your command interpreter.
注意 mysqladmin命令不能⽤于下⾯两个⽤户的更新
1)'root'@'127.0.0.1'
2) 'root'@'::1'
mysqladmin method of setting the root account passwords does not work for the'root'@'127.0.0.1' or
The mysqladmin
'root'@'::1'account. Use the SET PASSWORD method shown earlier.
After the root passwords have been set, you must supply the appropriate password whenever you connect as root to the
mysqladmin, use this command:
server. For example, to shut down the server with mysqladmin
mysqladmin -u root -p shutdown Enter password:(enter root password here)
shell>mysqladmin -u root -p shutdown
The mysql
mysql commands in the following instructions include a-poption based on the assumption that you have assigned the root account passwords using the preceding instructions and must specify that password when connecting to the server.
匿名⽤户设置密码⽅式
Assigning Anonymous Account Passwords
To assign passwords to the anonymous accounts, connect to the server as root, then use either SET PASSWORD or UPDATE.⽅式⼀:
To use SET PASSWORD on Windows, do this:
mysql -u root -p
shell>mysql -u root -p
Enter password:(enter root password here)
SET PASSWORD FOR ''@'localhost' = PASSWORD('newpwd');
mysql>SET PASSWORD FOR ''@'localhost' = PASSWORD('
To use SET PASSWORD on Unix, do this:
mysql -u root -p
shell>mysql -u root -p
Enter password:(enter root password here)
SET PASSWORD FOR ''@'localhost' = PASSWORD('newpwd');
mysql>SET PASSWORD FOR ''@'localhost' = PASSWORD('
mysql>SET PASSWORD FOR ''@'
SET PASSWORD FOR ''@'host_name' = PASSWORD('newpwd');
⽅式⼆:
To set the anonymous-user account passwords with a single UPDATE statement, do this (on any platform):
shell>mysql -u root -p
mysql -u root -proot的初始密码
Enter password:(enter root password here)
WHERE User = '';
UPDATE mysql.user SET Password = PASSWORD('newpwd')')->WHERE User = '';
mysql>UPDATE mysql.user SET Password = PASSWORD('
FLUSH PRIVILEGES;
mysql>FLUSH PRIVILEGES;
The FLUSH statement causes the server to reread the grant tables. Without it, the password change remains unnoticed by the server until you restart it.
Removing Anonymous Accounts
移除匿名帐号的⽅法
If you prefer to remove any anonymous accounts rather than assigning them passwords, do so as follows on Windows: mysql -u root -p
shell>mysql -u root -p
Enter password:(enter root password here)
DROP USER ''@'localhost';
mysql>DROP USER ''@'localhost';
On Unix, remove the anonymous accounts like this:
mysql -u root -p
shell>mysql -u root -p
Enter password:(enter root password here)
mysql>DROP USER ''@'localhost';
DROP USER ''@'localhost';
DROP USER ''@'host_name';
mysql>DROP USER ''@'
test数据库的安全风险
Securing Test Databases
By default, the mysql.db table contains rows that permit access by any user to the test database and other databases with names that start with test_. (These rows have an empty User column value, which for access-checking purposes matches any user name.) This means that such databases can b
e used even by accounts that otherwise possess no privileges. If you want to remove any-user access to test databases, do so as follows:
test数据库任何账户都可以访问,这样就存在着风险,⽤户恶意写⼊数据使数据库写满
mysql -u root -p
shell>mysql -u root -p
Enter password:(enter root password here)
DELETE FROM mysql.db WHERE Db LIKE 'test%';
mysql>DELETE FROM mysql.db WHERE Db LIKE 'test%';
FLUSH PRIVILEGES;
mysql>FLUSH PRIVILEGES;
The FLUSH statement causes the server to reread the grant tables. Without it, the privilege change remains unnoticed by the server until you restart it.
With the preceding change, only users who have global database privileges or privileges granted explicitly for thetestdatabase can use it. However, if you prefer that the database not exist at all, drop it:
DROP DATABASE test;
mysql>DROP DATABASE test;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论