Linux允许MySQL数据库远程访问方法
远程连接MySQL出于安全考虑,一般都关闭了远程访问,但有时候需要提供远程访问数据库的服务,下面我们介绍两种开启mysql远程连接的方法。
Mysql默认root用户只能本地访问,不能远程连接管理mysql数据库,Linux如何开启mysql远程连接?设置步骤如下:
1、GRANT命令创建远程连接mysql授权用户itlogger
代码如下 | 复制代码 |
mysql -u root -p |
第一句增加itlogger用户授权通过本地机(localhost)访问,密码“www.itlogger.com”。第二句则是授与itlogger用户从任何其它主机发起的访问(通配符%)。
2、设置防火墙允许3306端口
vi /etc/sysconfig/IPtables
添加-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
(注意添加在-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited之前,否则可能导致规则不生效)
重启防火墙service iptables restart
3、附:Mysql无法远程连接的常见问题
1)查看Mysql的端口是否正确,通过netstat -ntlp查看端口占用情况,一般情况下端口是3306。
2)报错:ERROR 2003 (HY000): Can’t connect to MySQL server on ’192.168.51.112′ (111)
查看/etc/my.cnf中,skip-networking 是否已被注掉,需要注掉。
3)报错:ERROR 2003 (HY000): Can’t connect to MySQL server on ’192.168.51.112′ (113)
查看是否iptables没有允许mysql连接,通过:service iptables stop临时关闭测试是否可以正常远程访问,如果可以,按上面方面设置iptable允许3306端口
4)远程访问mysql速度很慢的解决方法
修改/etc/my.cnf或my.ini
[mysqld]下添加
代码如下 | 复制代码 |
skip-name-resolve skip-grant-tables |
方法二 修改my.ini
第一步:修改my.cnf文件
使用文本编辑器去编辑MySQL服务器的配置文件my.cnf
如果你使用Debian Linux,文件位置在: /etc/mysql/my.cnf
如果你使用Red Hat Linux/Fedora/Centos Linux,文件位置在: /etc/my.cnf
如果你使用FreeBSD,文件位置在: /var/db/mysql/my.cnf
如果使用VI编辑,直接使用命令
代码如下 | 复制代码 |
# vi /etc/my.cnf |
第二步:如果文件打开,按照下面内容进行
[mysqld]
确保skip-networking是被注释的,或者被删除,然后添加下面一行内容
bind-address=你的服务器IP
例如,你的服务器IP是65.55.55.2,然后需要配置例如如下的内容:
代码如下 | 复制代码 |
[mysqld] 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 bind-address = 65.55.55.2 # skip-networking .... .. ....
|
这里面
bind-address : 你需要绑定的IP地址.
skip-networking : 开启 skip-networking 选项可以彻底关闭MySQL的TCP/IP连接方式,在一些文档中也提到在单机运行的 MySQL 推荐开启该选项,现在看,不太靠谱。
第三步:保存并且关闭文件
重启你的MySQL服务器,在命令行输出
# /etc/init.d/mysql restart
第四步:绑定远程IP地址的管理权限
连接MySQL服务器:
$ mysql -u root -p mysql
绑定权限到新的数据表(这一步可以借助phpmyadmin这类的工具简单完成,这里只是个例子)
如果我们需要绑定一个远程ip 202.54.10.20到新建的foo数据库下的bar用户中,在命令行中输入:
代码如下 | 复制代码 |
mysql> CREATE DATABASE foo; mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';
|
如何绑定一个已经存在的数据库呢?
代码如下 | 复制代码 |
mysql> update db set Host='202.54.10.20' where Db='webdb'; mysql> update user set Host='202.54.10.20' where user='webadmin'; |
第四步:推出MySQL
输入下面的命令:
代码如下 | 复制代码 |
mysql> quit; 第五步:打开3306 |
端口
需要将TCP端口3306开启,使用iptables或者BSD的pf 防火墙
Linux下iptables的例子
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT
或者如果你只需要允许特定的服务器,ip为10.5.1.3,可以这样:
/sbin/iptables -A INPUT -i eth0 -s 10.5.1.3 -p tcp --destination-port 3306 -j ACCEPT
或者仅仅允许自己子网内的远程连接范围192.168.1.0/24
/sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT
最后保存所有规则
# service iptables save
FreeBSD / OpenBSD pf 的规则( /etc/pf.conf)
pass in on $ext_if proto tcp from any to any port 3306
或者允许允许ip:10.5.1.3
pass in on $ext_if proto tcp from 10.5.1.3 to any port 3306 flags S/SA synproxy state
第六步:测试
你远程主机上面打开cmd,输入:
mysql -u webadmin –h 65.55.55.2 –p
在这里
-u webadmin: webadmin 是MySQL服务器的用户
-h IP or 服务器名称: 65.55.55.2 is MySQL 服务器IP地址
-p : 密码
你同样可以使用telnet来连接到3306端口
$ telnet 65.55.55.2 3306

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Device Manager can be defined as an extension of the management console provided by Microsoft. It provides users with a centralized and organized view of the hardware devices connected to the computer. Accessing Device Manager is easy, but how do you connect to a remote Device Manager? What is a remote device? Before connecting to Device Manager remotely, do you know what a remote device is? A remote device can be defined as any device that you do not have physical access to but can be accessed remotely via a network link or using remote control software. What are some examples of remote access? You can access many devices remotely. Let's assume you work in an open plan office. The office has a printer shared by all employees. From your desk, you can access your printer remotely. Other remote access examples include remote access computing

How to implement Modbus TCP remote access through PHP Modbus is a communication protocol used to exchange data between controllers and devices in the field of industrial automation. ModbusTCP is a Modbus protocol based on TCP/IP communication on Ethernet. Using PHP language, we can easily implement remote access to ModbusTCP. This article will introduce how to implement ModbusTCP remote access through PHP and provide sample code. Step 1: Installation

PHP development practice: Use PHPMailer to send emails to users in the MySQL database Introduction: In the construction of the modern Internet, email is an important communication tool. Whether it is user registration, password reset, or order confirmation in e-commerce, sending emails is an essential function. This article will introduce how to use PHPMailer to send emails and save the email information to the user information table in the MySQL database. 1. Install the PHPMailer library PHPMailer is

How to Disable Remote Desktop on Windows 11 With Remote Desktop, anyone with the correct username and password can access your PC. This is not a common attack tactic, but it can happen. Once a user has access to your PC, bad actors can gain access to your files, applications, and other account credentials. RDP is generally a security feature, but if you don't use it, disabling it can help eliminate attacks from potentially malicious remote threats. So, there are several ways to disable RDP and we'll show you how. want

As the amount of data continues to increase, database performance has become an increasingly important issue. Hot and cold data separation processing is an effective solution that can separate hot data and cold data, thereby improving system performance and efficiency. This article will introduce how to use Go language and MySQL database to separate hot and cold data. 1. What is hot and cold data separation processing? Hot and cold data separation processing is a way of classifying hot data and cold data. Hot data refers to data with high access frequency and high performance requirements. Cold data

How to use MySQL database for time series analysis? Time series data refers to a collection of data arranged in time order, which has temporal continuity and correlation. Time series analysis is an important data analysis method that can be used to predict future trends, discover cyclical changes, detect outliers, etc. In this article, we will introduce how to use a MySQL database for time series analysis, along with code examples. Create a data table First, we need to create a data table to store time series data. Suppose we want to analyze the number

How to use MySQL database for image processing? MySQL is a powerful relational database management system. In addition to storing and managing data, it can also be used for image processing. This article will introduce how to use a MySQL database for image processing and provide some code examples. Before you begin, make sure you have installed a MySQL database and are familiar with basic SQL statements. Create a database table First, create a new database table to store the image data. The structure of the table can be as follows

To what extent can I develop MySQL database skills to be successfully employed? With the rapid development of the information age, database management systems have become an indispensable and important component in all walks of life. As a commonly used relational database management system, MySQL has a wide range of application fields and employment opportunities. So, to what extent do MySQL database skills need to be developed to be successfully employed? First of all, mastering the basic principles and basic knowledge of MySQL is the most basic requirement. MySQL is an open source relational database management
