Golang not connecting to Postgres thought gorm and hosted by Docker

blov · · 464 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi everyone!</p> <p>Golang container isn&#39;t finding Postgres db&#39;s. I have one idea but can&#39;t implement this logic. Is it possible to retry to connect to database, if connection is refused? for example...</p> <p><a href="https://forum.golangbridge.org/t/golang-not-connecting-to-postgres-thought-gorm-and-hosted-by-docker/6571" rel="nofollow">And this is a link to my topic in golang forum, reddit code markdown works bad.</a></p> <pre><code>func Init() { authConnect := fmt.Sprintf( &#34;host=%s port=%s user=%s dbname=%s sslmode=disable password=%s&#34;, os.Getenv( &#34;AUTH_DB_HOST&#34; ), os.Getenv( &#34;AUTH_DB_PORT&#34; ), os.Getenv( &#34;AUTH_DB_USER&#34; ), os.Getenv( &#34;AUTH_DB_NAME&#34; ), os.Getenv( &#34;AUTH_DB_PASSWORD&#34; ) ) AuthConnect: AuthDB, err = gorm.Open( &#34;postgres&#34;, authConnect ) if err != nil { log.Printf(&#34;Got error when connect database `auth`, the error is &#39;%v&#39;&#34;, err) goto AuthConnect } userConnect := fmt.Sprintf( &#34;host=%s port=%s user=%s dbname=%s sslmode=disable password=%s&#34;, os.Getenv( &#34;USER_DB_HOST&#34; ), os.Getenv( &#34;USER_DB_PORT&#34; ), os.Getenv( &#34;USER_DB_USER&#34; ), os.Getenv( &#34;USER_DB_NAME&#34; ), os.Getenv( &#34;USER_DB_PASSWORD&#34; ) ) UserConnect: UserDB, err = gorm.Open(&#34;postgres&#34;, userConnect ) if err != nil { log.Fatalf(&#34;Got error when connect database `user`, the error is &#39;%v&#39;&#34;, err) goto UserConnect } } </code></pre> <p>If you have any suggestions please help me.</p> <p>Here are my code, logs and other details.</p> <pre><code>func Init() { authConnect := fmt.Sprintf( &#34;host=%s port=%s user=%s dbname=%s sslmode=disable password=%s&#34;, os.Getenv( &#34;AUTH_DB_HOST&#34; ), os.Getenv( &#34;AUTH_DB_PORT&#34; ), os.Getenv( &#34;AUTH_DB_USER&#34; ), os.Getenv( &#34;AUTH_DB_NAME&#34; ), os.Getenv( &#34;AUTH_DB_PASSWORD&#34; ) ) AuthDB, err = gorm.Open( &#34;postgres&#34;, authConnect ) if err != nil { log.Fatalf(&#34;Got error when connect database `auth`, the error is &#39;%v&#39;&#34;, err) } userConnect := fmt.Sprintf( &#34;host=%s port=%s user=%s dbname=%s sslmode=disable password=%s&#34;, os.Getenv( &#34;USER_DB_HOST&#34; ), os.Getenv( &#34;USER_DB_PORT&#34; ), os.Getenv( &#34;USER_DB_USER&#34; ), os.Getenv( &#34;USER_DB_NAME&#34; ), os.Getenv( &#34;USER_DB_PASSWORD&#34; ) ) UserDB, err = gorm.Open(&#34;postgres&#34;, userConnect ) if err != nil { log.Fatalf(&#34;Got error when connect database `user`, the error is &#39;%v&#39;&#34;, err) } } </code></pre> <p>After starting the container I get this message.</p> <pre><code>auth_1 | 2017/09/07 12:04:32 Got error when connect database `auth`, the error is &#39;dial tcp 127.0.0.1:5437: getsockopt: connection refused&#39; </code></pre> <p>My docker-compose.yml</p> <pre><code>version: &#39;3&#39; </code></pre> <p>services: user_db: image: postgres environment: - POSTGRES_USER=postgres - POSTGRESS_PASSWORD=postgres - POSTGRES_DB=user ports: - &#34;5432:5432&#34; auth_db: image: postgres environment: - POSTGRES_USER=postgres - POSTGRESS_PASSWORD=postgres - POSTGRES_DB=auth ports: - &#34;5437:5432&#34; auth: build: context: . dockerfile: ./auth/Dockerfile environment: - AUTOMIGRATE=true - AUTH_DB_HOST=localhost - USER_DB_HOST=localhost - AUTH_DB_PORT=5437 - USER_DB_PORT=5432 - AUTH_DB_USER=postgres - USER_DB_USER=postgres - AUTH_DB_NAME=auth - USER_DB_NAME=user - AUTH_DB_PASSWORD=postgres - USER_DB_PASSWORD=postgres ports: - &#34;8070:8070&#34; depends_on: - user_db - auth_db user: build: context: . dockerfile: ./user/Dockerfile environment: - AUTOMIGRATE=true ports: - &#34;8090:8090&#34; depends_on: - user_db</p> <p>My docker-compose logs</p> <pre><code> Attaching to grsuser_auth_db_1, grsuser_user_db_1, grsuser_user_1, grsuser_auth_1 </code></pre> <p>auth_db_1 | The files belonging to this database system will be owned by user &#34;postgres&#34;. auth_db_1 | This user must also own the server process. auth_db_1 | auth_db_1 | The database cluster will be initialized with locale &#34;en_US.utf8&#34;. auth_db_1 | The default database encoding has accordingly been set to &#34;UTF8&#34;. auth_db_1 | The default text search configuration will be set to &#34;english&#34;. auth_1 | 2017/09/07 12:04:32 Got error when connect database <code>auth</code>, the error is &#39;dial tcp 127.0.0.1:5437: getsockopt: connection refused&#39; auth_db_1 | auth_db_1 | Data page checksums are disabled. user_db_1 | The files belonging to this database system will be owned by user &#34;postgres&#34;. auth_db_1 | user_db_1 | This user must also own the server process. user_db_1 | user_db_1 | The database cluster will be initialized with locale &#34;en_US.utf8&#34;. auth_db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok auth_db_1 | creating subdirectories ... ok user_db_1 | The default database encoding has accordingly been set to &#34;UTF8&#34;. auth_db_1 | selecting default max_connections ... 100 user_db_1 | The default text search configuration will be set to &#34;english&#34;. auth_db_1 | selecting default shared_buffers ... 128MB user_db_1 | user_db_1 | Data page checksums are disabled. auth_db_1 | selecting dynamic shared memory implementation ... posix auth_db_1 | creating configuration files ... ok user_db_1 | auth_db_1 | running bootstrap script ... ok user_db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok user_1 | Now listening on: http://localhost:8090 auth_db_1 | performing post-bootstrap initialization ... ok user_db_1 | creating subdirectories ... ok user_db_1 | selecting default max_connections ... 100 auth_db_1 | user_1 | Application started. Press CTRL+C to shut down. auth_db_1 | WARNING: enabling &#34;trust&#34; authentication for local connections user_db_1 | selecting default shared_buffers ... 128MB auth_db_1 | You can change this by editing pg_hba.conf or using the option -A, or user_1 | Now listening on: http://localhost:8090 user_db_1 | selecting dynamic shared memory implementation ... posix auth_db_1 | --auth-local and --auth-host, the next time you run initdb. user_1 | Application started. Press CTRL+C to shut down. user_db_1 | creating configuration files ... ok auth_db_1 | syncing data to disk ... ok user_db_1 | running bootstrap script ... ok auth_db_1 | auth_db_1 | Success. You can now start the database server using: user_db_1 | performing post-bootstrap initialization ... ok user_db_1 | syncing data to disk ... ok user_db_1 | user_db_1 | Success. You can now start the database server using: user_db_1 | user_db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start auth_db_1 | user_db_1 | auth_db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start user_db_1 | user_db_1 | WARNING: enabling &#34;trust&#34; authentication for local connections auth_db_1 | user_db_1 | You can change this by editing pg_hba.conf or using the option -A, or auth_db_1 | **************************************************** user_db_1 | --auth-local and --auth-host, the next time you run initdb. auth_db_1 | WARNING: No password has been set for the database. auth_db_1 | This will allow anyone with access to the user_db_1 | **************************************************** auth_db_1 | Postgres port to access your database. In user_db_1 | WARNING: No password has been set for the database. auth_db_1 | Docker&#39;s default configuration, this is user_db_1 | This will allow anyone with access to the auth_db_1 | effectively any other container on the same user_db_1 | Postgres port to access your database. In auth_db_1 | system. auth_db_1 | auth_db_1 | Use &#34;-e POSTGRES_PASSWORD=password&#34; to set user_db_1 | Docker&#39;s default configuration, this is auth_db_1 | it in &#34;docker run&#34;. user_db_1 | effectively any other container on the same auth_db_1 | **************************************************** user_db_1 | system. auth_db_1 | waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address auth_db_1 | HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. auth_db_1 | LOG: database system was shut down at 2017-09-07 11:55:29 UTC auth_db_1 | LOG: MultiXact member wraparound protections are now enabled auth_db_1 | LOG: database system is ready to accept connections auth_db_1 | LOG: autovacuum launcher started auth_db_1 | done auth_db_1 | server started auth_db_1 | CREATE DATABASE auth_db_1 | auth_db_1 | ALTER ROLE auth_db_1 | auth_db_1 | user_db_1 | user_db_1 | Use &#34;-e POSTGRES_PASSWORD=password&#34; to set user_db_1 | it in &#34;docker run&#34;. user_db_1 | **************************************************** user_db_1 | waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address user_db_1 | HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. user_db_1 | LOG: database system was shut down at 2017-09-07 11:55:29 UTC user_db_1 | LOG: MultiXact member wraparound protections are now enabled user_db_1 | LOG: database system is ready to accept connections user_db_1 | LOG: autovacuum launcher started user_db_1 | done auth_db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* auth_db_1 | auth_db_1 | LOG: received fast shutdown request user_db_1 | server started user_db_1 | CREATE DATABASE user_db_1 | user_db_1 | ALTER ROLE user_db_1 | user_db_1 | user_db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* user_db_1 | user_db_1 | LOG: received fast shutdown request auth_db_1 | waiting for server to shut down....LOG: aborting any active transactions auth_db_1 | LOG: autovacuum launcher shutting down auth_db_1 | LOG: shutting down auth_db_1 | LOG: database system is shut down auth_db_1 | done auth_db_1 | server stopped auth_db_1 | auth_db_1 | PostgreSQL init process complete; ready for start up. auth_db_1 | auth_db_1 | LOG: database system was shut down at 2017-09-07 11:55:34 UTC auth_db_1 | LOG: MultiXact member wraparound protections are now enabled auth_db_1 | LOG: database system is ready to accept connections auth_db_1 | LOG: autovacuum launcher started auth_db_1 | LOG: received smart shutdown request auth_db_1 | LOG: autovacuum launcher shutting down user_db_1 | waiting for server to shut down....LOG: aborting any active transactions user_db_1 | LOG: autovacuum launcher shutting down user_db_1 | LOG: shutting down user_db_1 | LOG: database system is shut down user_db_1 | done user_db_1 | server stopped user_db_1 | user_db_1 | PostgreSQL init process complete; ready for start up. user_db_1 | user_db_1 | LOG: database system was shut down at 2017-09-07 11:55:34 UTC user_db_1 | LOG: MultiXact member wraparound protections are now enabled user_db_1 | LOG: autovacuum launcher started user_db_1 | LOG: database system is ready to accept connections auth_db_1 | LOG: shutting down auth_db_1 | LOG: database system is shut down auth_db_1 | LOG: database system was shut down at 2017-09-07 12:00:31 UTC auth_db_1 | LOG: MultiXact member wraparound protections are now enabled auth_db_1 | LOG: database system is ready to accept connections auth_db_1 | LOG: autovacuum launcher started grsuser_auth_1 exited with code 1</p> <p>Regards, Khachatur!</p> <hr/>**评论:**<br/><br/>michele: <pre><p>Formatting is a bit of a mess, but it looks to me like you got networking wrong: you don&#39;t need the <code>ports</code> directives for the two dbs. The <code>auth</code> container will be able to connect to the dbs because of the <code>depends_on</code> directive. Plus, you don&#39;t connect to <code>localhost</code>, but use the containers&#39; names instead.</p> <p>So remove <code>ports</code> from the dbs and change <code>AUTH_DB_PORT</code> to <code>5432</code>, <code>AUTH_DB_HOST</code> to <code>auth_db</code> and <code>USER_DB_HOST</code> to <code>user_db</code>.</p> <p>Should work, let me know if it doesn&#39;t.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

464 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传