First, create a user
command:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
Key Parameters :
username - create a login user name,
host - Specifies that the user can log on which host, the local user can localhost, let the user can log in from any remote host, you can use wildcards %.
password - the user's login password, the password can be empty, if empty then the user can log in without a password server .
example:
CREATE USER 'code'@'localhost' IDENTIFIED BY '123456'; CREATE USER 'code'@'192.168.1.101_' IDENDIFIED BY '123456'; CREATE USER 'code'@'%' IDENTIFIED BY '123456'; CREATE USER 'code'@'%' IDENTIFIED BY ''; CREATE USER 'code'@'%';
Second, to authorized users
command:
GRANT privileges ON databasename.tablename TO 'username'@'host';
Key Parameters :
privileges: the user's operating authority, such as SELECT, INSERT, UPDATE, etc. If you want to grant permissions is to use ALL
DatabaseName: database name, * on behalf of all database
tablename: table name, if you want to give the user all databases and tables * available respective operating authority is represented as *. *
host - Specifies that the user can log on which host, the local user can localhost, let the user can log in from any remote host, you can use wildcards %.
password - the user's login password, the password can be empty, if empty then the user can log in without a password server .
example:
GRANT SELECT, INSERT ON test.user TO 'code'@'%'; GRANT ALL ON *.* TO 'code'@'%'; GRANT ALL ON maindataplus.* TO 'code'@'%';
Note:
authorization by the above command is not authorized users to other users, the user may want if authorized by the authorized user to cancel the command :( other rights authorized users)
the GRANT privileges the ON databasename.tablename the TO 'username' @ 'Host 'WITH GRANT OPTION;
Third, change passwords
command:
The SET PASSWORD the FOR ' username ' @ ' Host ' = PASSWORD ( ' NewPassword ' );
- if the user is currently logged on with:
the SET PASSWORD = PASSWORD ( 'NewPassword ");
example:
SET PASSWORD FOR 'code'@'%' = PASSWORD("123456");
Fourth, revoke user privileges
command:
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
Key Parameter Description :
Privilege, DatabaseName, TableName: with the authorization section
example:
REVOKE SELECT ON *.* FROM 'code'@'%';
Note:
If you are a user 'code' @ '%' is authorized when such (or similar):
GRANT SELECT ON test.user TO 'code' @ '%', then use REVOKE SELECT ON * * FROM 'code' @ '%';. Does not revoke the command SELECT test user in the user table of the database operation.
Conversely, if the authorization to use the GRANT SELECT ON * * TO 'code' @ '%';. The REVOKE SELECT ON test.user FROM 'code' @ '%'; command the user can not withdraw the test database the user table the Select permission.
Specific information may command SHOW GRANTS FOR 'code' @ '%'; View.
V. delete user
command:
DROP USER 'username'@'host';
Six, forget the root password
1, modify the database Mysql my.cnf configuration
# We /etc/my.cnf
In paragraph [mysqld] plus one: skip-grant-tables, save and exit;
[mysqld] ...... datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock skip-grant-tables
2. Restart mysqld services
# systemctl restart mysqld
4. Log mysql modify the root password
[root@localhost /]# mysql ...... mysql> update mysql.user set authentication_string=password('123456') where host ='localhost' and user='root'; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>
note:
MySQL 5.7 is an updated user table authentication_string field,
MySQL 5.6 is an updated user table password field,
After the change the user password using the command: Update MySQL. User SET password = password ( ' 123456 ' ) WHERE Host = ' localhost ' and User = ' the root ' ;
Update user rights: flush privileges
The configuration back to restart mysqld
[root@localhost /]# vi /etc/my.cnf [root@localhost /]# systemctl restart mysqld [root@localhost /]# mysql -uroot -p123456
Information Reference:
https://www.cnblogs.com/zhongyehai/p/10695659.html
https://blog.csdn.net/bingcheng529/article/details/88557599
https://www.cnblogs.com/iosdev/archive/2013/07/15/3190431.html