Does mysql need to create a user and then give this user remote permissions to operate the msg database instead of giving remote permissions to the root user?

In MySQL, it is generally recommended not to directly use the root user to remotely connect and operate the database, because the root user has the highest authority, which may cause security risks. Instead, it is recommended to follow the steps below to create a new user and assign the user the appropriate permissions to remotely operate a specific database (e.g. msg database):

  1. Log in to the MySQL server as root.
mysql -u root -p
  1. To create a new user, use the following command: sqlCopy

CREATE USER 'your_username'@'%' IDENTIFIED BY 'your_password';

In the above command:

  • 'your_username' is the username of the new user you want to create.
  • '%' means the user is allowed to connect remotely from any host. If you only want to allow remote connections from specific IP addresses, you can replace '%' with a specific IP address or hostname.
  • 'your_password' is the password you set for the new user.
  1. Grant the new user access to the msg database. You can grant permissions to the new user using: sqlCopy
GRANT ALL PRIVILEGES ON msg.* TO 'your_username'@'%';

In the above command:

  • 'ALL PRIVILEGES' means giving the user all permissions on the msg database. If you only need to grant some permissions, you can replace them with a specific permission list, such as SELECT, INSERT, UPDATE, etc.
  • 'msg. ' indicates that the user is allowed to access all tables in the msg database. If you only want to allow users to access a specific table, you can replace ' ' with the table name.
  1. Refresh permissions for changes to take effect: sqlCopy
FLUSH PRIVILEGES;

Now, you have created a new user and assigned it remote access to the msg database. This way, you can use the new user's credentials to remotely connect to the MySQL server and operate the msg database without having to use the root user to operate, thus improving security. Make sure to use strong passwords when setting them, and carefully manage users' permissions to ensure security.

To delete the user named "your_name" in MySQL, you can use the following SQL statement:

sqlCopy

DELETE FROM mysql.user WHERE User = 'your_name'; FLUSH PRIVILEGES;

This SQL statement will delete the user record with the user name "your_name" from the MySQL user table and FLUSH PRIVILEGES;refresh the permissions through to ensure that the modification takes effect immediately. Please make sure you have sufficient permissions to perform this operation, usually requiring superuser or administrator privileges.

Also, please note that deleting a user deletes the permissions and objects associated with that user, including databases and tables. So before deleting users, make sure you have backed up the necessary data or done relevant data migration work. Deleting a user is a potentially dangerous operation and should be performed with caution.

To change the password of MySQL user A, you can use the MySQL SET PASSWORDstatement or ALTER USERstatements, provided that you have sufficient permissions to perform these operations. Here are examples of both methods:

Method 1: Use the SET PASSWORD statement

sqlCopy

SET PASSWORD FOR 'A'@'localhost' = PASSWORD('新密码');

The above command sets user A's password to the new password. Please replace 'localhost' with user A's hostname, or use '%' instead of 'localhost' if you want to allow connections from any host. Replace 'new password' with user A's new password.

Method 2: Use the ALTER USER statement (applicable to MySQL 5.7 and higher)

If you are using MySQL 5.7 or newer, you can also use ALTER USERthe statement to change the user password:

sqlCopy
ALTER USER 'A'@'localhost' IDENTIFIED BY '新密码';

Likewise, replace 'localhost' with User A's hostname or '%' to allow connections from any host, and replace 'new password' with User A's new password.

Whichever method you use, make sure you have sufficient permissions to perform these operations, which usually requires UPDATEOR ALTERpermissions and mysql.useraccess to the table.

View port number

SHOW VARIABLES LIKE 'port';

The complete logic of mysql remote setting:

Centos7.6 yum installs mysql_centos7.6 yum installs mysql_tenc1239's blog-CSDN blog

Guess you like

Origin blog.csdn.net/tenc1239/article/details/133210607