Solution to the problem that mysql cannot connect remotely

  The reasons why the MySQL database cannot be connected remotely may be as follows:


MySQL does not open the network connection.

MySQL only listens to local connections by default. You need to set bind-address to the server IP in the configuration file my.ini or /etc/my.cnf to enable network connections.


MySQL does not have remote access permission enabled.

MySQL only allows root users to log in locally by default, and remote access permissions need to be authorized.

You can execute the GRANT ALL ON.TO 'root'@'%' IDENTIFIED BY 'password'; command to grant the root user remote access rights.


The server firewall blocks the MySQL network connection.

If the server has a firewall enabled, you need to set the firewall to open MySQL's default port 3306 to allow inbound connections.


MySQL users do not have remote access rights.

Except for the root user, other users do not have remote access permissions and need to be authorized separately.

grant all on dbname.* to 'username'@'%' identified by 'password';


The network between server and client is unreachable.

If there is a problem with the network between the server and the remote client, it will also result in the inability to connect to the MySQL database. Need to check the network connection of server and client.


MySQL version is lower.

Lower versions of MySQL may have problems with network connection support. Upgrading to a newer version can solve some network connection problems.


Suggested resolution steps:


Set the my.ini configuration file, add bind-address = 0.0.0.0, and restart the MySQL service.


执行GRANT ALL ON.TO 'root'@'%' WITH GRANT OPTION;

Grant remote access permissions to the root user.


Check the server firewall settings and open MySQL port 3306. Or temporarily turn off the firewall for testing


Grant remote access permissions to remote connection users respectively.

grant all on dbname.* to 'username'@'%' identified by 'password';

首先登陆mysql控制台,登陆的命令也很简单,命令如下所示:
mysql -uroot -p666

为需要远程登录的用户赋予权限:

mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "666";
mysql> flush privileges;

第一行代码的含义是 所有的ip均可以访问root用户的数据库 密码是666
第二行的含义是刷新权限信息,必须刷新,要不然还要重启服务。

 MySQL user and permission management

(1) User creation and precautions

1. User naming

username  should not exceed  characters, and should not exceed 16  characters.

host supporting code % *

[email protected].%

[email protected]

Username matching principle: most accurate match(Explained in detail below)

Use username  and host  to control database access< /span>

下迎 192.168.222.10 上连连 DB 则Demand usage [email protected]

2. Creation of username

grant all privileges on *.* to 'username'@'192.168.222.%';

---There is no password when logging in with this authorization.

grant all privileges on *.* to 'username'@'%' identified by '123456';

--- Authorizes IP  users from all places, without limiting login sources, and Encrypt the password.

with grant option

---The created user can authorize other usersdelete from mysql.user where user!=root or host!=localhost;

---After creating a new database, user processing

flush privileges;

---Synchronize permission data in memory to disk.

select user,host,password from mysql.user;

---Query user information

3. Deletion of username:

drop user 'scott'@'192.168.222.10';

flush privileges;

(2) Examples of user connections

Create 2 users with the same username, but different hosts and different passwords

grant all privileges on *.* to 'scott'@'192.168.222.10' identified by 'tiger';

grant all privileges on *.* to 'scott'@'%' identified by '123456;

Create  users, then use scott  and 123456 Password, can I connect?

Cannot connect, must use password tiger  to connect.

The principle is: the most accurate match for users

(3) Forgot administrator user password and precautions

Stop MySQL Service

service mysql stop

Specify parameters skip-grant-tables Start database service addition

mysqld_safe --defaults-file=/data/mysql/mysql3306/my.cnf --datadir=/data/mysql/mysql3306/data

--basedir=/usr/local/mysql --user=mysql --port=3306 --skip-grant-tables &Change user password

mysql

select user,host,password from mysql.user;

update user set password=password('root12345') where user='root' and host=’localhost’;

select user,host,password from mysql.user;

filush privileges;

Caution:update Language constant addition where Restrictions

Check whether the network between the client and the server is connected, and troubleshoot related network problems.


Depending on the actual situation, consider upgrading the MySQL version to a newer version.


If the problem is still not resolved, you need to further troubleshoot the cause based on the MySQL error log.

Guess you like

Origin blog.csdn.net/qq_24768591/article/details/133611561