我试图用dockercompose-up命令将正式的mariadb docker映像连接到客户端应用程序的映像;它成功地连接到服务器,但在尝试连接到mariadb数据库时,大约一分钟后记录“error:connection lost:the server closed the connection.”启动服务器时,在本地连接到数据库没有问题,但在尝试链接相同代码的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.
它应该记录“连接到数据库”。我也尝试过人们建议的解决方案,用于处理由于空闲等原因导致的服务器断开连接,但它甚至一开始就没有连接到数据库。
2条答案
按热度按时间ryhaxcpt1#
当开发人员接近我说他的应用程序无法连接到容器时,我通常会这样做。我给他们以下的设置。
docker-compose.yml码:
dockerfile文件:
入口.sh:
当你跑的时候,这就行了
docker-compose up
,输出如下:这证明了容器之间的连接性是有效的,并且它们所遇到的任何问题都是特定于应用程序的。可能是以错误的格式传递连接字符串,或者使用错误的服务器名称,或者在mysql尚未启动时尝试提前连接。
已经证明了这一点,他们现在可以“弥合差距”,对自己或我的设置做一些小的改变,试图拉近距离,直到有东西打破。最后一步暴露了罪魁祸首。例如,如果
mysql
命令行连接到他们的应用程序容器,它确实连接,但他们的应用程序没有,这意味着他们需要在他们的应用程序代码中寻找原因。根据我的经验,这是解决此类问题的最快方法,因为原因还不清楚。
ovfsdjhp2#
因此,在我将database connection index.js文件中的主机从“127.0.0.1”更改为:
其中“db”指的是docker-compose.yml文件中的“db”图像。