Home Database Mysql Tutorial 解析远程连接管理其他机器上的MYSQL数据库_MySQL

解析远程连接管理其他机器上的MYSQL数据库_MySQL

Jun 01, 2016 pm 01:25 PM
develop database

bitsCN.com

在开发过程中,有时候需要远程连接并管理别的机器上的MYSQL数据库,在实现的过程中会遇到一系列的问题,现在以远程访问我自己安装在Ubuntu上的MYSQL数据为例(端口为默认端口3306),说明一下配置步骤及每一步中遇到的问题及相应解决方法:
远程连接管理MYSQL,总体上来说有三步:(A为主操作机器,B为远程机器(MYSQL安装在B上,由A访问B)
1,在被连接的MYSQL中创建专门的远程连接用户wow;

2,修改被连接的MYSQL的配置文件my.cnf,使此MYSQL不仅仅支持本地IP127.0.0.1的监听,也支持其他IP的监听,并重启Mysql服务,使配置生效。

3,验证A中MYSQL的配置端口是否与B中的一致(都是3306?),如果一致,通过 mysql -h B的ip -u wow --port=3306 -p,来远程连接。

下面,就每一步详细说明一下:
一,在被连接的MYSQL中创建专门的远程连接用户wow;
之所以要创建专门的用户,是因为MYSQL在最初安装的时候,默认的root帐号以及其他的帐号是仅限于在localhost连接使用的。在远程机器上即使你用同样的用户名及密码登录,都无法正常连接。比如你在远程机器192.168.83.56上用root帐号登录192.168.11.12机器上的Mysql:
 mysql -h 192.168.11.12 -u root  -p,是无法正常登录的。此时,需要登录192.168.11.12机器,进入mysql中的mysql数据库查看user表,确认具体的root权限,特别是它的host是localhost还是你对应的IP,还是%。

解决方法:
A,(在192.168.11.12机器中的MYSQL中创建专门的远程用户root或者wow),创建用户与赋予权限有两种方法:
1)改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,把"localhost"改为"%"。“%”的意思就是所有host都可以访问。
mysql -u root -p vmware mysql>use mysql; mysql>update user set host = '%' where user = 'root';mysql>select host, user from user;
2)授权法。例如,你想wow使用mypassword从任何主机连接到mysql服务器的话。
GRANT ALL PRIVILEGES ON *.* TO 'wow'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
如果你想只允许用户wow从ip为192.168.83.56的主机连接到192.168.11.12的mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO 'wow'@'192.168.83.56' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES
第二句表示从mysql数据库的grant表中重新加载权限数据。因为MySQL把权限都放在了cache中,所以在做完更改后需要重新加载。
B,在用GRANT授权法创建完用户wow并赋予权限以后,是不是就可以用了?不可以吗?我们可以简单验证一下。先在远程机器192.168.83.56上 mysql -h 192.168.11.12 -u root  -p,是无法正常登录。报2003的错:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.11.12' (111)
什么原因?是账户没有建立成功?权限设置问题,还是网络配置防火墙等的问题?还是mysql的其他配置问题没有解决?
先排除帐号与权限的问题:在localhost(192.168.11.12)机器中,尝试用wow用户进入数据库。mysql -u wow -p mypassword,报1045的错:
ERROR 1045 (28000): Access denied for user 'wow'@'localhost' (using password: YES),
为啥呢?怎么创建的新用户在本机上都无法登录MYSQL,难道真是用户wow没有创建成功?再查看user表,wow确实已经有了啊。一顿纠结,最后找到原因,原来是安装配置mysql的过程中没有删除匿名账户,删除匿名账户:
 mysql -u root -p
 mysql>use mysql
 mysql>delete from user where User=' ';
 mysql>quit;
再尝试    mysql -u wow -p mypassword,wow用户可以在本机正常登录了,看来帐号wow是没有问题的,那再试一下远程连接:
mysql -h 192.168.11.12 -u root  -p,是无法正常登录。还报2003的错:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.11.12' (111),
查看一下errorCode来排查问题:
[mysql@vvmvcs0 ~]$ perror 111
OS error code 111: Connection refused
那难道是网络的问题?ping xxx.xxx.xxx.12,可以PING通,那机器是没问题的。看来要看看mysql的配置文件my.cnf中关于远程连接的配置了,这就进入了我们的第二步。。

2,修改被连接的MYSQL的配置文件my.cnf,使此MYSQL不仅仅支持本地IP127.0.0.1的监听,也支持其他IP的监听,并重启Mysql服务,使配置生效。
主要是两个配置项:skip_networking或者bind_address,为了取消本地监听,需要在my.cnf中注释掉关于这两项的配置:
正常情况下,mysql占用的3306端口只是在IP 127.0.0.1上监听,拒绝了其他IP的访问(通过netstat可以查看到)。取消本地监听需要修改 my.cnf 文件:
            sudo vim /etc/mysql/my.cnf
            //找到如下内容,并注释
            bind-address = 127.0.0.1
            然后需要重启 mysql (可最后再重启)。sudo mysql stop
现在我们再试一试,在远程机器192.168.83.56上用如下命令登录一下192.168.11.12的数据库:mysql -h 192.168.11.12 -u root  -p,还是无法正常登录。还是报2003的错。又是什么原因呢?哦,是mysql端口配置是否统一的问题!

3,验证A中MYSQL的配置端口是否与B中的一致(都是3306?),如果一致,通过 mysql -h B的ip -u wow --port=3306 -p,来远程连接。
192.168.83.56上的mysql监听端口配置的不是3306,是3308,而192.168.11.12的端口是默认的3306,二者不匹配,如果仅仅在192.168.83.56上用mysql -h 192.168.11.12 -u root  -p来登录的话,就是去访问192.168.11.12的3308端口啦,当然无法访问了。我们还需要指定正确的被访问mysql的端口号:mysql -h 192.168.11.12 -u wow --port=3306 -p。

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How do new features of PHP functions simplify the development process? How do new features of PHP functions simplify the development process? May 04, 2024 pm 09:45 PM

The new features of PHP functions greatly simplify the development process, including: Arrow function: Provides concise anonymous function syntax to reduce code redundancy. Property type declaration: Specify types for class properties, enhance code readability and reliability, and automatically perform type checking at runtime. null operator: concisely checks and handles null values, can be used to handle optional parameters.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

See all articles