search
HomeDatabaseMysql TutorialLinux允许MySQL数据库远程访问方法

远程连接MySQL出于安全考虑,一般都关闭了远程访问,但有时候需要提供远程访问数据库的服务,下面我们介绍两种开启mysql远程连接的方法。

Mysql默认root用户只能本地访问,不能远程连接管理mysql数据库,Linux如何开启mysql远程连接?设置步骤如下:
1、GRANT命令创建远程连接mysql授权用户itlogger

 代码如下 复制代码

mysql -u root -p
mysql>GRANT ALL PRIVILEGES ON *.* TO itlogger@localhost IDENTIFIED BY ‘www.itlogger.com’ WITH GRANT OPTION;
mysql>GRANT ALL PRIVILEGES ON *.* TO itlogger@”%” IDENTIFIED BY ‘www.itlogger.com’ WITH GRANT OPTION;

第一句增加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

Statement
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
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.