LInux MySQL 端口验证

linux suse11在terminal可以正常登录进行各种操作,在tomcat运行jdbc web程序异常:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link
failure
The last packet sent successfully to the server was 0 milliseconds
ago. The driver has not received any packets from the server.

原因查找

正常可以连接msql suse 运行以下命令情况如下:

1. 运行命令netstat -talnp验证3306端口是否打开:

test:~ # netstat -talnp
Active Internet connections (servers and
established)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name 
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4620/mysqld

。。。。。。

2.运行命令 ps aux|grep mysql看mysql服务是否在运行

test:~ # ps aux|grep mysql
root 4254 0.0 0.0 2940 1332 ? S 06:05 0:00
/bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data
--pid-file=/usr/local/mysql/data/test.pid
mysql 4620 0.1 0.9 301476 37428 ?
Sl 06:05 0:13 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin
--user=mysql --log-error=/usr/local/mysql/data/test.err
--pid-file=/usr/local/mysql/data/test.pid --socket=/usr/local/mysql/mysql.sock
--port=3306
root 8002 0.0 0.0 2264 680 pts/2 S+ 09:21 0:00 grep mysql

3.查看3306端口运行的进程 lsof -i :3306
test:~ # lsof -i :3306
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 4620 mysql 12u IPv4 10559 0t0 TCP
*:mysql (LISTEN)

问题机器运行上面运行命令

1.netstat -talnp 看不到绑定到3306端口的tcp连接

2.ps aux|grep mysql 显示服务在运行

3. lsof -i :3306查看mysql是否运行在3306端口,未有显示mysql服务没有运行在3306端口

结论:mysql服务虽然在运行,可以通过teminal操作,但没有运行在3306端口,所以通过jdbc连接连接不上。

后查看网络资料mysql配置中skip-networking未注释,参数skip-networking起的作用是:mysql不再在TCP /
IP端口上进行监听,与mysqld的所有互动都必须通过Unix套接字或命名管道进行。

配置文件注释如下

# Don‘t listen on a TCP/IP port at all. This can be a security
enhancement,
# if all processes that need to connect to mysqld run on the
same host.
# All interaction with mysqld must be made via Unix sockets or
named pipes.
# Note that using this option without enabling named pipes on
Windows
# (via the "enable-named-pipe" option) will render mysqld
useless!

将参数skip-networking注释重启mysql服务,jdbc程序连接mysql正常。

网络遇见同样异常,可能的其它原因:

1.

tomcat程序连接服务器mysql数据库,报

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link
failure
The last packet sent successfully to the server was 0 milliseconds
ago. The driverhas not received any packets from the
server.
错误,后来用java程序直接连接数据库,还是一样的错误,数据库连接,用户名密码都没有问题。
在网上查了很多情况,什么数据库用户权限设置,还是数据库连接等待时间(wait_timeout)设置,都没用。
而连接本地的数据库就没问题,本地数据库版本:5.0.37,服务器数据库版本:5.0.19,JDBC驱动版本:
mysql-connector-java-5.1.9-bin.jar。
这个问题困扰了一天,后来想想换换5.0的驱动看看怎么样,结果在网上找了个mysql-connector-java-5.1.14-bin.jar的驱动,结果OK了。

2.

CommunicationsException: Communications link failure

If you get a SQLException: Connection refused or Connection timed out or a
MySQL specific CommunicationsException: Communications link failure, then it
means that the DB isn‘t reachable at all. This can have one or more of the
following causes:

IP address or hostname in JDBC URL is wrong.
Hostname in JDBC URL is not
recognized by local DNS server.
Port number is missing or wrong in JDBC
URL.
DB server is down.
DB server doesn‘t accept TCP/IP connections.
DB
server has run out of connections.
Something in between Java and DB is
blocking connections, e.g. a firewall or proxy.

To solve the one or the other, follow the following advices:

Verify and test them with ping.
Refresh DNS or use IP address in JDBC URL
instead.
Verify it based on my.cnf of MySQL DB.
Start the DB.
Verify
if mysqld
is started without the --skip-networking option.
Restart the DB and fix your
code accordingly that it closes connections in finally.
Disable firewall
and/or configure firewall/proxy to allow/forward the port.

参考:

http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai
http://blog.csdn.net/gucapg/article/details/17509969
http://4925054.blog.51cto.com/4915054/1154263

时间: 2024-08-02 17:02:35

LInux MySQL 端口验证的相关文章

Linux下mysql端口不能远程访问的问题防火墙设置

以下内容只解决iptables开放端口问题,不解决mysql数据库本身账号远程访问权限问题. Linux iptables 开放Mysql端口允许远程访问 修改防火墙配置文件: vi  /etc/sysconfig/iptables 增加下面一行: -A RH-Firewall-1-INPUT -m state –state NEW -m  tcp -p tcp –dport 3306 -j ACCEPT 或者使用命令 iptables -A RH-Firewall-1-INPUT -m sta

Linux MYSQL 数据库

Linux MYSQL  数据库 1.1.概述: DBMS datebase management system  数据库管理系统 DML:date manapulate lanaguage  数据操作语言 INSERT,REPLACE,UPDATE,DELETE DLL: Date Defination Lanauage 数据定义语言 CREATE,创建数据库 ALTER,修改数据库 DROP 删除数据库和表 DCL:Date Control Language 数据控制语言 常用数据库软件 O

linux 查看端口号命令

Linux下如果我们需要知道2809号端口的情况的话,我们可以这样,如下命令: $netstat -pan|grep 24800 tcp        0      0 0.0.0.0:24800           0.0.0.0:*               LISTEN      4496/synergys        tcp        0      0 192.168.1.104:24800     192.168.1.106:49479     ESTABLISHED 4496

linux mysql access denied for user ‘root’@’localhost'(using password:YES)

linux安装完mysql后,使用程序连接报以上错误 解决方法,重新设置密码,步骤如下 1.先停掉原来的服务 service mysqld stop 2.使用安全模式登陆,跳过密码验证 mysqld_safe --user=mysql --skip-grant-tables --skip-networking& 3.登陆 mysql -uroot mysql 4.修改密码 mysql > update user set authentication_string = password('12

linux 常用端口

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px "Helvetica Neue"; background-color: #ffffff } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "Helvetica Neue"; color: #8c8c8c; background-color: #ffffff } p.p3 { margin: 0.0px

linux开关端口问题

linux开关端口问题: 我们知道一些常用的端口,比如mysql的端口为3306,sql的端口为:1433,以及tomcat的端口为 8008等等一样! 当这些端口在linux下是没有开启时,我们是无法使用它们的,数据库无法使用,浏览器无 法使用.那么如何来解决这个问题了? 首先我们要找到iptables的配置文件 在设置模式时:我们需要了解一下linux的iptables的默认命令的含义: :INPUT ACCEPT [0:0]# 该规则表示INPUT表默认策略是ACCEPT :FORWARD

Linux通过端口转发来访问内网服务(端口转发访问阿里云Redis数据库等服务)

# 安装rinetd wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -xvf rinetd.tar.gz&&cd rinetd sed -i 's/65536/65535/g' rinetd.c (修改端口范围) mkdir /usr/man&&make&&make install 说明:IP的端口是双字节,也就是256*256-1, 256*256对计算机来说就是0,因

Linux mysql数据库搭建

设共享软件包地址192.168.80.10 setenforce  0 service  iptables  stop 1.共享软件包 mount.cifs   //192.168.80.10/r   /media/                 匿名访问共享文件夹 cd   /media/ ls     查看是否挂载成功了 tar   xzvf   mysql-5.5.24.tar.gz   -C   /opt/        解压至/opt/ cd   /opt/ mysql-5.5.24

修改mysql端口以及出现的问题

为了提高服务器的安全性,本节课主要讲的是Linux下如何修改Mysql端口.支持Linux环境下大部分系统的修改,如Centos和Ubuntu等. 一.编辑my.cnf vi /etc/my.cnf 二.在[mysqld]下添加port=3506 三.重启mysql服务 service  mysqld  restart 这里我之前出现了一个问题,修改端口后 导致mysql起不来 排查如下 这是更改端口后,进入/var/log/mysqld.log里摘取的启动失败相关日志: 130823 17:2