Solution MySQL cannot connect to the database method remotely

Source: Internet
Author: User
Tags bind mysql database

1, login MySQL Database

Mysql-u root-p

View User table

The code is as follows Copy Code

mysql> use MySQL;
Database changed
Mysql> select Host,user,password from user;
+--------------+------+-------------------------------------------+
| Host | user | password |
+--------------+------+-------------------------------------------+
| localhost | Root | *a731aebfb621e354cd41baf207d884a609e81f5e |
| 192.168.1.1 | Root | *a731aebfb621e354cd41baf207d884a609e81f5e |
+--------------+------+-------------------------------------------+
2 rows in Set (0.00 sec)

You can see the root user that has been created in the user table. The Host field indicates which hosts are logged on, their values can be IP, and host names are available.

(1) Sometimes want to use the local IP login, then you can change the host value of the above to their own IP can.

2. Realize remote connection (authorization law)

Changing the value of the host field to% indicates that the root user can log on to the MySQL server on any client machine, and it is recommended that you set it to% at development time.
Update user Set host = '% ' where user = ' root ';

Change permissions to all privileges

The code is as follows Copy Code

mysql> use MySQL;
Database changed
Mysql> grant all privileges in *.* to root@ '% ' identified by ' root ';
Query OK, 0 rows Affected (0.00 sec)

Mysql> select Host,user,password from user;
+--------------+------+-------------------------------------------+
| Host | user | password |
+--------------+------+-------------------------------------------+
| localhost | Root | *a731aebfb621e354cd41baf207d884a609e81f5e |
| 192.168.1.1 | Root | *a731aebfb621e354cd41baf207d884a609e81f5e |
| %            | Root | *a731aebfb621e354cd41baf207d884a609e81f5e |
+--------------+------+-------------------------------------------+
3 Rows in Set (0.00 sec)

This allows the machine to remotely access MySQL on the machine with the username root password root.

3. Realize remote connection (change table method)

The code is as follows Copy Code

Use MySQL;

Update user Set host = '% ' where user = ' root ';

This allows MySQL to be accessed remotely from the root user.


Another way

MySQL By default is not accessible via remote machines and can be accessed by using the following configuration.

My MySQL environment is UBUNTU+MYSQL5.

1. Modify the/etc/mysql/my.conf, modify the bind-address, designated as the actual IP address of the machine, your My.con modified is probably as follows

The code is as follows Copy Code

[Mysqld]
 

#
# * Basic Settings
#
Default-character-set=utf8
Default-storage-engine=innodb
Bind-address = 192.168.1.107
user = MySQL
Pid-file =/var/run/mysqld/mysqld.pid
Socket =/var/run/mysqld/mysqld.sock
Port = 3306
Basedir =/usr
DataDir =/var/lib/mysql
Tmpdir =/tmp
Language =/usr/share/mysql/english
Skip-external-locking
  

2. Login with root MySQL execute the following command

The code is as follows Copy Code
Grant all on sonardb.* to sonar@ '% ' identified by ' 123456 ';
Grant all on sonardb.* to sonar@localhost identified by ' 123456 ';

Sonardb Replace with the name of the database you want to visit, Sonar is the username you want to use, 123456 is replaced with your password, which opens the remote access function.


For more details please see: http://www.111cn.net/database/mysql/34989.htm


The above two methods is directly with the root user in the CMD mode operation, one is in the configuration file inside operation, Datong small righteousness.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.