Centos 7 install and configure mysql

The default database of CentOS7 is mariadb, and the configuration is not used, so I decided to change it to mysql, but it seems that there is no mysql in the yum source of CentOS7 by default. In order to solve this problem, we need to download the mysql repo source first.

1. Download the mysql repo source

$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

2. Install the mysql-community-release-el7-5.noarch.rpm package

$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm After

installing this package, you will get two mysql yum repo sources: /etc/yum.repos.d/mysql-community.repo, /etc/ yum.repos.d/mysql-community-source.repo.

3. Install mysql

$ sudo yum install mysql-server

and install it according to the prompts, but there is no password after the installation is complete, you need to reset the password

4. Reset the mysql password

$ mysql -u root

login may report this error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) , the reason is the access permission problem of /var/lib/mysql. The following command changes the owner of /var/lib/mysql to the current user:

$ sudo chown -R root:root /var/lib/mysql

or mysql is not started, you need to start and

restart the mysql service first

$ service mysqld restart

Then log in to reset the password:

$ mysql -u root //Enter the mysql console directly
mysql > use mysql;
mysql > update user set password=password('123456') where user='root'; mysql
> exit; MySQL create user and authorization 1. Create user command: CREATE USER 'username '@'host' IDENTIFIED BY 'password'; Description: username: the username you will create host: specify on which host the user can log in, if it is a local user, use localhost, if you want the user to be able to log in from any remote host To log in, 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'@'%';
2. Authorization:
Command:
GRANT privileges ON databasename.tablename TO 'username'@'host'
Description:
privileges: the user's operating authority, such as SELECT, INSERT, UPDATE, etc., if you want to grant the authority Then 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 *, such as *.*
Example:
GRANT SELECT, INSERT ON test.user TO 'pig'@ '%';
GRANT ALL ON *.* TO 'pig'@'%';
GRANT ALL ON maindataplus.* TO 'pig'@'%';
Note:
A user authorized with the above command cannot authorize other users. If you want the user to be authorized, use the following command:

GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
3. 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 'pig'@'%' = PASSWORD("123456");
4. Revoke user privilege
Command :
REVOKE privilege ON databasename.tablename FROM 'username'@ 'host';
Description:
privilege, databasename, tablename: same as the authorization part

Example :
REVOKE SELECT ON *.* FROM 'pig'@'%';
Note:
If you authorize user 'pig'@'%' like this (or similar): GRANT SELECT ON test.user TO 'pig'@'%', then use REVOKE SELECT ON *.* FROM ' The pig'@'%'; command cannot undo 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'@'%'; then REVOKE SELECT ON test.user FROM 'pig'@'%'; command cannot revoke the user's access to the user table in the test database Select permission.

The specific information can be viewed with the command SHOW GRANTS FOR 'pig'@'%';.

5. Delete user
Command :
DROP USER 'username'@'host';

Guess you like

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