Mysql cannot be accessed remotely by default.

What! No way! Everyone full of praise of the east east, incredibly default is a stand-alone version, too toy bar.

In fact, this is only for mysql that has just been installed. Mysql has only one user account, root, which can only be accessed locally. .

So, how can you enable remote access, for example in the same LAN, to allow other machines to access its database?

If MYSQL is installed, its initial account is as follows:

mysql> select host,user from user;
Copy the code
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0. 01. |
| root |: :1       |
| root | localhost |
+------+-----------+
3 rows in set (0.14 sec)
Copy the code

You can change host to ‘%’, so it can be accessed by all machines.

However, I have a situation where a third party has developed a system and we have to read data from its database. Its account defaults to something like this and is only locally accessible. Should I change its account? It seems like it’s a small change, and there should be no risk, but it doesn’t feel right.

However, almost all tutorials on the Internet are the same argument as changing host to ‘%’.

I think the idea is this:

Create a user. 2. Grant remote access to the user

After testing, it was successful. Perform the following steps: 1. Create a user

create user 'test' identified by '123456'
Copy the code

This creates a user whose host is’ % ‘.

To restrict access to only one IP address, use:

create user 'test'@'192.168.0.164' identified by '123456'
Copy the code

Ok, so we’re done creating a user and allowing remote access

2, but also assign permissions

grant all on db1.* to test@The '%';
Copy the code

Here, all permissions for database DB1 are assigned to test.

So, you can use test to access the mysql database.

Mysql > alter system root; mysql > alter system root; So what do we do in the join string? As it turns out, there are three ways to write:

server=192.168128.130.; user id=test; password=; database=db1
server=192.168128.130.; user id=test; password=' '; database=db1
server=192.168128.130.; user id=test; database=db1
Copy the code

All of the above. Mysql -u test (enter)

So how do you clear your password? Can be:

set password for test@The '%'=password(' ');
Copy the code

Marvel at the brilliance of mysql. It looks like the account is actually determined by the account name + host, equivalent to a compound primary key. It’s a novel idea. It’s also practical.