Connecting a server to a MySQL database

agolangf · · 561 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So I&#39;m trying to connect a server written in Go to a MySQL database, both of which are running in docker containers. My Go server is mapped to port 8080 and my database is mapped to port 3306. The server will serve the webpage when I connect with chrome and I can connect with the database through MySQL workbench. The issue is that the Server will not connect to the database. I&#39;m using these lines of code to connect:</p> <pre><code>db, err = sql.Open(&#34;mysql&#34;, &#34;goadddb:goapppassword@/goappdb&#34;) if err!=nil{ log.Fatal(&#34;could not connect to server: &#34;, err.Error) } </code></pre> <p>The code executes without any error, but when I run</p> <pre><code>err := db.Ping() if err!=nil{ log.Fatal(&#34;ping failed: &#34;, err.Error) } </code></pre> <p>The ping fails and the server stops running. </p> <p>If you need me to post any other details let me know.</p> <hr/>**评论:**<br/><br/>Romeo3t: <pre><p>I might be mis-remembering but isn&#39;t the syntax of the mysql string: <code>user:password@hostname/dbname</code></p> <p>You&#39;re missing a hostname here aren&#39;t you?</p> <p>And I think if you leave the hostname blank it might default to trying to access localhost. Which if these are in separate containers then localhost on the app container would not be running a mysql instance.</p> <p>In the case that my theory is not correct make sure you&#39;re referencing and mapping the ports of the containers correctly. Remember that localhost in a docker container is different from your host machine. That&#39;s all we can tell without any error message information.</p></pre>Your__Butthole: <pre><p>This makes sense since the docker containers wont have the same localhost. I have a docker compose file set up so they can communicate through a virtual network with the host location being <a href="http://goappdb" rel="nofollow">http://goappdb</a>. </p> <p>would something like this work?</p> <pre><code>goappdb:goapppassword@http://goappdb/goappdb </code></pre> <p>Edit: removed port, don&#39;t think it matters</p></pre>Romeo3t: <pre><p>You wouldn&#39;t use http:// here as that would imply communication over the http which mysql doesn&#39;t use. Instead you would insert here whatever you named the database container in the docker container file.</p> <p>So in your case it would just be <code>goappdb:goapppassword@goappdb/goappdb</code></p> <p>Or for the example below it would be <code>romeo:password@myawesomedb/foobardb</code></p> <pre><code>version: &#34;3&#34; services: app: image: romeo/lgts:app env_file: - .env ports: - &#34;8080:8080&#34; volumes: - /tmp:/tmp myawesomedb: image: romeo/myawesomedb:postgres ports: - &#34;5432&#34;:&#34;5432&#34; depends_on: - app </code></pre></pre>tusharsingh: <pre><p>You need to indicate the host you are connecting to. You also should link from your go container to the mysql container on a specific port.</p></pre>jerf: <pre><p>If it is at all possible to get a shell in the container the Go executable is in, what happens if you use telnet to try to get to the MySQL server?</p> <p>If you can&#39;t do that, use the net package to just try opening a socket to the MySQL server and see what happens.</p> <p>I&#39;d guess you&#39;ll get a &#34;socket not open&#34; or &#34;connection refused&#34; message, which means that the mapping is not correctly done through Docker. Be sure the ports are indeed flowing correctly before adding the complication of the sql library, which tries to do some pooling and such that can complicate handling the simpler problems.</p> <p>Plus, given that you logged the error, you really should paste it in here. If it is &#34;connection refused&#34; or something similar it is almost certainly a docker configuration issue.</p></pre>Your__Butthole: <pre><p>the error says </p> <pre><code>dial tcp 127.0.0.1:3306 getsockopt: connection refused </code></pre> <p>I&#39;ve reconfigured docker using docker compose to set up a virtual network the two containers can communicate through, the host of the database should be http://goappdb, do I just plug that in as the host name in a string with this format?</p> <pre><code>user:password@hostname/dbname </code></pre></pre>aleks-me2digital: <pre><p>You will need to add &#34;(&#34; &#34;)&#34; to be able to connect to a IP or name.</p> <p>&#34;goadddb:goapppass@(127.0.0.)/goappdb&#34;</p> <p>Please take a look into the full description of the DSN in the readme <a href="https://github.com/go-sql-driver/mysql#dsn-data-source-name" rel="nofollow">https://github.com/go-sql-driver/mysql#dsn-data-source-name</a></p></pre>RenThraysk: <pre><p>Try using explicit IP address as the host name.</p> <p>&#34;goadddb:<a href="mailto:goapppassword@127.0.0.1" rel="nofollow">goapppassword@127.0.0.1</a>/goappdb&#34;</p> <p>Plus are you connecting using the same account details in MySQL Workbench? Or using root?</p></pre>aleks-me2digital: <pre><p>You will need to add &#34;(&#34; &#34;)&#34; to be able to connect to a IP or name.</p> <p>&#34;goadddb:goapppass@(127.0.0.)/goappdb&#34;</p> <p>Please take a look into the full description of the DSN in the readme <a href="https://github.com/go-sql-driver/mysql#dsn-data-source-name" rel="nofollow">https://github.com/go-sql-driver/mysql#dsn-data-source-name</a></p></pre>tsdtsdtsd: <pre><p><a href="https://golang.org/pkg/database/sql/#Open" rel="nofollow">sql.Open docs</a> say:</p> <blockquote> <p>Open may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call Ping.</p> </blockquote> <p>So, after <code>Open</code>, you don&#39;t have the connection yet, the returned err is not stating any facts about a connection, data source name or user credentials.</p></pre>

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

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