mysql数据库报错,请问如何处理?com.mysql.jdbc.CommunicationsException: Communications link fail..

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据库报错,请问如何处理?com.mysql.jdbc.CommunicationsException: Communications link fail..相关的知识,希望对你有一定的参考价值。

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.io.EOFException
MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
STACKTRACE:
java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2411)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2916)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at com.kaniwu.common.db.c3p0.JdbcUtils.query(JdbcUtils.java:122)
at com.kaniwu.common.DAO.impl.CommonDAOImpl.queryLoadInfo(CommonDAOImpl.java:58)
at com.kaniwu.common.process.CacheReloadThread.run(CacheReloadThread.java:77)
at java.lang.Thread.run(Thread.java:714)
** END NESTED EXCEPTION **

参考技术A 丢包了?
Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

如何处理这个错误(1049,“未知数据库'/users/ohyunjun/work/astral/mysql'”)

【中文标题】如何处理这个错误(1049,“未知数据库\'/users/ohyunjun/work/astral/mysql\'”)【英文标题】:How to deal with this ERROR (1049, "Unknown database '/users/ohyunjun/work/astral/mysql'")如何处理这个错误(1049,“未知数据库'/users/ohyunjun/work/astral/mysql'”) 【发布时间】:2014-08-19 03:57:24 【问题描述】:

在 Django settings.py 中,我以这种方式设置数据库选项

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.path.join(BASE_DIR, 'mysql'),
        'USER': 'root',
        'PASSWORD': 'sp153426',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    

我执行了这个命令

python manage.py syncdb

但因此错误而失败

  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 160, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 132, in _cursor
    self.ensure_connection()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 127, in ensure_connection
    self.connect()
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 127, in ensure_connection
    self.connect()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 115, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 435, in get_new_connection
    conn = Database.connect(**conn_params)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database '/users/ohyunjun/work/astral/mysql'")

【问题讨论】:

name 是数据库名称。不是绝对路径。 【参考方案1】:

嘿伙计,它只是要求数据库应该已经在 MySQL 中创建。

【讨论】:

请考虑更新您的答案。如果您从问题的settings.py 文件中读取代码,您可以看到数据库名称是完整的文件系统路径。这显然是个问题。如果您想扩展到其他原因,您会看到 1049 错误,请写一个更好的解释。【参考方案2】:

'NAME' 是您的数据库的名称。使用 MySQL,您也需要手动创建数据库。比方说,如果你运行:

$ mysql -u root -p

mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.02 sec)

你的配置应该是:

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': 'sp153426',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    

【讨论】:

如果我已经有一个需要加载的数据库 (.sql) 怎么办?我不想创建一个新的空数据库。

以上是关于mysql数据库报错,请问如何处理?com.mysql.jdbc.CommunicationsException: Communications link fail..的主要内容,如果未能解决你的问题,请参考以下文章

case when语句报错该如何处理?

sql导入数据时总是报错,详见下图,请大神指导如何处理?文件格式改过多个版本,也没有空格或符号

请问当用户反馈网站访问慢,你会如何处理?

请问当用户反馈网站访问慢,你会如何处理?

请问如下javax.servlet.ServletException 异常是啥情况,如何处理?

IIS无法启动报错配置文件的XML格式不正确如何处理