MySQL user management and authorization - enable remote access

Linux starts or stops the Mysql service

#service mysqld start
#service mysqld stop


Common operations:

select a database:
use 'databasename';

Query database instance:
show databases;

Query the table for the currently selected DB instance:
show tables;

Query user permissions:
show grants for 'username'@'%';

Query Mysql version number:
select version();

Query Mysql port number:
show global variables like 'port';


Create a user:

Command: CREATE USER 'username'@'host' IDENTIFIED BY 'password';

Description: username - the username you will create, host - specify the host on which the user can log in, if it is a local user, you can use localhost, If you want the user to log in from any remote host, you can use the wildcard %. password - the user's login password, the password can be empty, if it is empty, the user can log in to the server without a password.

Example:
CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '';
CREATE USER 'pig'@'%';


Set and change user password

command: SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword'); if it is the current login user, use SET PASSWORD = PASSWORD("newpassword");

Example:
SET PASSWORD FOR 'username'@'%' = PASSWORD("123456");

update user set password=PASSWORD('newpwd') where user='root';


Delete user

command: DROP USER 'username'@'host';

user authorization:

command: GRANT privileges ON databasename.tablename TO 'username'@'host'

Description: privileges - user's operating authority, such as SELECT, INSERT, UPDATE, etc. ( For a detailed list, see the end of this article). If you want to grant all permissions, use ALL.;databasename - database name, tablename - table name, if you want to grant the user the corresponding operation permissions on all databases and tables, you can use * to indicate, such as *.*.

Example: GRANT SELECT, INSERT ON test.user TO 'username'@'%';
GRANT ALL ON *.* TO 'username'@'%';

Note: The user authorized by the above command cannot be given to other users Authorization, if you want the user to be authorized, use the following command:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;


revoke user privilege

command: REVOKE privilege ON databasename.tablename FROM 'username'@'host ';

Description: privilege, databasename, tablename - same as authorization part.

Example: REVOKE SELECT ON *.* FROM 'pig'@'%';

Note: If you grant user 'username'@'%' like this (or similar): GRANT SELECT ON test.user TO 'username'@'%', then use REVOKE SELECT ON *.* The FROM 'username'@'%'; command cannot revoke the user's SELECT operation on the user table in the test database. On the contrary, if the authorization is GRANT SELECT ON *.* TO 'pig'@'%'; REVOKE SELECT The ON test.user FROM 'username'@'%'; command cannot revoke the user's Select permission to the user table in the test database. For

specific information, use the command SHOW GRANTS FOR 'username'@'%'; to view.

Open Mysql remote Access:
1. Change the table method It

may be that your account is not allowed to log in from the remote, only on localhost. At this time, as long as you are on the localhost computer, after logging in to mysql, change the "host" item in the "user" table in the "mysql" database, from "localhost" to "%"

# mysql -u root -p
mysql>use mysql;
mysql>update user set host = '%' where user = 'username';
mysql>select host, user from user;


2. Authorization Law
-- Give any host permission to access the data
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION

If you want myuser to use mypassword to connect to the mysql server from any host.

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY 'mypassword' WI
TH GRANT OPTION;


If you want to allow the user myuser to connect to the mysql server from the host with ip 192.168.1.6 and use mypassword as the password

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3'IDENTIFIED BY
'mypassword' WITH GRANT OPTION;

mysql>FLUSH PRIVILEGES -- the modification takes effect
mysql>EXIT

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484530&siteId=291194637