How to quickly modify the host attribute of a MySQL user

How to quickly modify the host attribute of a MySQL user

When you log in to MySQL remotely, the account you use has special requirements.

The default host attribute of an account is localhost, which means that this account can only be used locally. If you want to use an account to log in remotely, you must change the host attribute value of the account to %.

The executed SQL statements are as follows:

update user set host = '%' where user = 'root';

Supplement: mysql modify root password modify account login host

1. Forgot the root password

The remote server has started a mysql service with a hive account. I can log in to the remote server through the command line using mysql -hlocalhost -uxxx -pxxx, but I can't log in using the navicat client, and navicat still displays the IP address of my own machine.

The initial suspicion is that the password of account A in MySQL is not set correctly. Therefore, you need to use the root account to reset the hive account.

The annoying thing is that I didn’t install MySQL. And in the test environment, I don’t know who to ask for the password. Then use the ultimate weapon: change the root password.

2. Reset mysql root password.

First of all, please note that the MySQL root account and the server root account are not the same concept, so don't confuse them.

First, stop the mysql service:

sudo service mysql stop

If you have a root account on the server, you don't need sudo. The same applies to all the following operations. The above command works on Ubuntu and Debian. Use mysqld instead of mysql in CentOS, Fedora, and RHEL. The same applies to all the following operations.

Then, start mysql in safe mode:

sudo mysqld_safe --skip-grant-tables --skip-networking &

This way we can log in directly as root without a password:

mysql -u root

In this way, we logged in to mysql with the root account.

Then, you can reset the root password:

mysql> use mysql; 
mysql> update user set password=PASSWORD("mynewpassword") where User='root'; 
mysql> flush privileges;

After the reset is complete, exit mysql. Then start the mysql service:

sudo service mysql restart

Next, log in with the root account:

mysql -u root -pmynewpassword

3. Modify the relevant permissions of account A

After logging in to mysql with the root account, let's take a look at the relevant information of account A:

mysql> use mysql;
Database changed
mysql> select User, Host from user where User='hive';
+------+--------------+
| User | Host |
+------+--------------+
| hive | 127.0.0.1 |
+------+--------------+

Now I understand. Damn, no wonder I can't log in to the navicat client. The host of the hive account is only 127.0.0.1, so you can only log in locally.

mysql> update user set Host='%' where User='hive';

Set the hive account to be accessible to all machines, and then refresh the permissions:

mysql> flush privileges;

Let’s take another look:

mysql> select User, Host from user where User='hive';
+------+------+
| User | Host |
+------+------+
| hive | % |
+------+------+

So far, you’re done!

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Does the % in the newly created MySQL user include localhost?
  • How to allow all hosts to access mysql
  • Perfect solution to the problem that MySQL cannot connect to the database through localhost
  • Solution to the problem that MySQL can connect using localhost but cannot connect using IP
  • A brief discussion on the matching rules of host and user when Mysql connects to the database

<<:  HTML table markup tutorial (28): cell border color attribute BORDERCOLOR

>>:  Vue implements the browser-side code scanning function

Recommend

Problems and solutions of using jsx syntax in React-vscode

Problem Description After installing the plugin E...

A brief discussion on how to customize the host file in Docker

Table of contents 1. Command 2. docker-compose.ym...

nuxt.js multiple environment variable configuration

Table of contents 1. Introduction 2. Scenario 3. ...

Detailed explanation of CSS3 Flex elastic layout example code

1. Basic Concepts //Any container can be specifie...

Detailed analysis of SQL execution steps

Detailed analysis of SQL execution steps Let'...

React implements dynamic pop-up window component

When we write some UI components, if we don't...

27 Linux document editing commands worth collecting

Linux col command The Linux col command is used t...

How to allow all hosts to access mysql

1. Change the Host field value of a record in the...

Chrome 4.0 supports GreaseMonkey scripts

GreaseMokey (Chinese people call it Grease Monkey...

How to enable the slow query log function in MySQL

The MySQL slow query log is very useful for track...

How to create a Django project + connect to MySQL

1: django-admin.py startproject project name 2: c...

The use of MySQL triggers and what to pay attention to

Table of contents About Triggers Use of triggers ...

Understanding of web design layout

<br />A contradiction arises. In small works...