【问题标题】:Error connecting to MariaDB using Docker-Compose, "Connection Lost: The server closed the connection"使用 Docker-Compose 连接到 MariaDB 时出错,“连接丢失:服务器关闭了连接”
【发布时间】:2018-12-11 06:11:57
【问题描述】:

我正在尝试使用命令 docker-compose up 将 官方 MariaDB docker 映像 连接到我的客户端应用程序的映像;它成功连接到服务器,但是在尝试连接到 MariaDB 数据库时,大约一分钟后记录了 “错误:连接丢失:服务器关闭了连接。” 我在本地连接到数据库没有问题在启动服务器时,但在尝试链接相同代码的 docker 图像时会引发错误。

我的 docker-compose.yml:

version: '3.1'
services:
  webapp:

    image: client
    ports:
      - "3306:3306"
    links: 
      - db
    depends_on:
      - db
    environment:
      DATABASE_URL: "mysql://root:root@db/yelp"
db:
  image: mariadb:latest
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: root
    MYSQL_DATABASE: yelp

adminer:
  image: adminer
  restart: always
  ports:
    - 3001:3001

Dockerfile:

FROM node:10

WORKDIR /cadenza/documents/SDC/leaveReview

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3306

CMD ["npm", "start"]

mysql/MariaDB index.js 连接文件:

var mysql = require('mysql')
var connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: 'root',
    database: 'yelp'
});

connection.connect((err) => {
    if (err) {
        console.log('error when connecting to db', err);
    } else {
        console.log('connected to db')
    }
});

module.exports = connection;

运行 docker-compose up 时的日志:

    docker-compose up
Starting leavereview_db_1      ... done
Starting leavereview_adminer_1 ... done
Starting leavereview_webapp_1  ... done
Attaching to leavereview_adminer_1, leavereview_db_1, leavereview_webapp_1
adminer_1  | PHP 7.2.7 Development Server started at Tue Jul  3 02:34:03 2018
webapp_1   |
webapp_1   | > yelp-reviews@1.0.0 start /cadenza/documents/SDC/leaveReview
webapp_1   | > nodemon server/index.js
webapp_1   |
db_1       | 2018-07-03  2:34:04 0 [Note] mysqld (mysqld 10.3.7-MariaDB-1:10.3.7+maria~jessie) starting as process 1 ...
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Using Linux native AIO
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Uses event mutexes
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Compressed tables use zlib 1.2.8
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Number of pools: 1
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Completed initialization of buffer pool
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=1630896
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Waiting for purge to start
webapp_1   | [nodemon] 1.17.5
webapp_1   | [nodemon] to restart at any time, enter `rs`
webapp_1   | [nodemon] watching: *.*
webapp_1   | [nodemon] starting `node server/index.js`
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: 10.3.7 started; log sequence number 1630905; transaction id 21
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1       | 2018-07-03  2:34:04 0 [Note] Plugin 'FEEDBACK' is disabled.
db_1       | 2018-07-03  2:34:04 0 [Note] Recovering after a crash using tc.log
db_1       | 2018-07-03  2:34:04 0 [Note] Starting crash recovery...
db_1       | 2018-07-03  2:34:04 0 [Note] Crash recovery finished.
db_1       | 2018-07-03  2:34:04 0 [Note] Server socket created on IP: '::'.
db_1       | 2018-07-03  2:34:04 0 [Note] InnoDB: Buffer pool(s) load completed at 180703  2:34:04
db_1       | 2018-07-03  2:34:04 0 [Warning] 'proxies_priv' entry '@% root@a1a244ac54cf' ignored in --skip-name-resolve mode.
db_1       | 2018-07-03  2:34:04 0 [Note] Reading of all Master_info entries succeded
db_1       | 2018-07-03  2:34:04 0 [Note] Added new Master_info '' to hash table
db_1       | 2018-07-03  2:34:04 0 [Note] mysqld: ready for connections.
db_1       | Version: '10.3.7-MariaDB-1:10.3.7+maria~jessie'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
webapp_1   | Listening on port  3306
webapp_1   | error when connecting to db { Error: Connection lost: The server closed the connection.
webapp_1   |     at Protocol.end (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:113:13)
webapp_1   |     at Socket.<anonymous> (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/Connection.js:109:28)
webapp_1   |     at Socket.emit (events.js:187:15)
webapp_1   |     at endReadableNT (_stream_readable.js:1081:12)
webapp_1   |     at process._tickCallback (internal/process/next_tick.js:63:19)
webapp_1   |     --------------------
webapp_1   |     at Protocol._enqueue (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:145:48)
webapp_1   |     at Protocol.handshake (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/protocol/Protocol.js:52:23)
webapp_1   |     at Connection.connect (/cadenza/documents/SDC/leaveReview/node_modules/mysql/lib/Connection.js:130:18)
webapp_1   |     at Object.<anonymous> (/cadenza/documents/SDC/leaveReview/database/index.js:9:12)
webapp_1   |     at Module._compile (internal/modules/cjs/loader.js:702:30)
webapp_1   |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
webapp_1   |     at Module.load (internal/modules/cjs/loader.js:612:32)
webapp_1   |     at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
webapp_1   |     at Function.Module._load (internal/modules/cjs/loader.js:543:3)
webapp_1   |     at Module.require (internal/modules/cjs/loader.js:650:17) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
webapp_1   | (node:29) [DEP0096] DeprecationWarning: timers.unenroll() is deprecated. Please use clearTimeout instead.

它应该记录 "connected to db"。我还尝试了人们建议的解决方案,用于处理由于空闲等原因导致的服务器断开连接,但它甚至一开始都没有连接到数据库。

【问题讨论】:

    标签: mysql node.js docker docker-compose mariadb


    【解决方案1】:

    当开发人员找到我说他的应用程序无法连接到容器时,我通常会这样做。我给他们以下设置。

    docker-compose.yml:

    version: '3.1'
    services:
      webapp:
        build: .
      db:
        image: mariadb:latest
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: yelp
    

    Dockerfile:

    FROM alpine
    RUN apk add -U mysql-client
    COPY entry.sh .
    CMD ["/bin/sh", "entry.sh"]
    

    entry.sh:

    #!/bin/sh
    
    echo "Waiting for mysql..."
    until mysqladmin ping -h"db" -P"3306" --silent
    do
      echo "mysql is not ready will retry in 5..."
      sleep 5
    done
    
    echo -e "\nmysql is ready"
    
    mysql -h"db" -P"3306" -p"root" -u"root" -e "SHOW DATABASES"
    

    这很有效,当您运行 docker-compose up 时,输出如下所示:

    Starting 51145943_db_1     ... done
    Starting 51145943_webapp_1 ... done
    Attaching to 51145943_webapp_1, 51145943_db_1
    webapp_1  | Waiting for mysql...
    webapp_1  | mysql is not ready will retry in 5...
    db_1      | 2018-07-03  3:17:10 0 [Note] mysqld (mysqld 10.3.7-MariaDB-1:10.3.7+maria~jessie) starting as process 1 ...
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: Using Linux native AIO
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: Uses event mutexes
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: Compressed tables use zlib 1.2.8
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: Number of pools: 1
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: Using SSE2 crc32 instructions
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: Completed initialization of buffer pool
    db_1      | 2018-07-03  3:17:10 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
    db_1      | 2018-07-03  3:17:11 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
    db_1      | 2018-07-03  3:17:11 0 [Note] InnoDB: Creating shared tablespace for temporary tables
    db_1      | 2018-07-03  3:17:11 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
    db_1      | 2018-07-03  3:17:11 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
    db_1      | 2018-07-03  3:17:11 0 [Note] InnoDB: 10.3.7 started; log sequence number 1630896; transaction id 21
    db_1      | 2018-07-03  3:17:11 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
    db_1      | 2018-07-03  3:17:11 0 [Note] Plugin 'FEEDBACK' is disabled.
    db_1      | 2018-07-03  3:17:11 0 [Note] Server socket created on IP: '::'.
    db_1      | 2018-07-03  3:17:11 0 [Warning] 'proxies_priv' entry '@% root@4a91686036a2' ignored in --skip-name-resolve mode.
    db_1      | 2018-07-03  3:17:11 0 [Note] InnoDB: Buffer pool(s) load completed at 180703  3:17:11
    db_1      | 2018-07-03  3:17:11 0 [Note] Reading of all Master_info entries succeded
    db_1      | 2018-07-03  3:17:11 0 [Note] Added new Master_info '' to hash table
    db_1      | 2018-07-03  3:17:11 0 [Note] mysqld: ready for connections.
    db_1      | Version: '10.3.7-MariaDB-1:10.3.7+maria~jessie'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
    db_1      | 2018-07-03  3:17:15 8 [Warning] Access denied for user 'root'@'172.22.0.3' (using password: NO)
    webapp_1  |
    webapp_1  | mysql is ready
    webapp_1  | Database
    webapp_1  | information_schema
    webapp_1  | mysql
    webapp_1  | performance_schema
    webapp_1  | yelp
    51145943_webapp_1 exited with code 0
    

    这证明容器之间的连接是有效的,并且它们遇到的任何问题都是特定于应用程序的。可能是传递的连接字符串格式错误,或者使用了错误的服务器名称,或者在 mysql 尚未启动时尝试提前连接。

    证明了这一点后,他们现在可以“缩小差距”,对他们自己的或我的设置进行微小的更改,试图拉近它们直到出现问题。最后一步揭示了罪魁祸首。例如,如果在他们的应用容器中添加mysql 命令行连接并且它确实连接,但他们的应用没有,这意味着他们需要在他们的应用代码中查找原因。

    根据我的经验,当原因不是很明显时,这是解决此类问题的最快方法。

    【讨论】:

      【解决方案2】:

      所以在我将数据库连接 index.js 文件中的主机从“127.0.0.1”更改为:

      var connection = mysql.createConnection({
      host: 'db',
      user: 'root',
      password: 'root',
      database: 'yelp'
      });
      

      “db”指的是我的 docker-compose.yml 文件中的“db”图像。

      【讨论】:

        猜你喜欢
        • 2013-12-11
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        • 2013-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-30
        相关资源
        最近更新 更多