首页  >  问答  >  正文

如何从本地主机连接到在docker网络中运行的docker容器

<p>我有一个mysql的docker容器使用docker撰写像这样:</p> <pre class="brush:php;toolbar:false;">version: "3.3" services: db-server: container_name: database image: mysql:latest build: dockerfile: ./Dockerfile environment: MYSQL_ROOT_PASSWORD: rootTest MYSQL_USER: soumalya MYSQL_PASSWORD: userTest ports: - "3031:3306" networks: - internal volumes: - mysql:/var/lib/mysql - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro restart: unless-stopped networks: internal: driver: bridge volumes: mysql:</pre> <p>我正在用正确的密码连接到localhost:3031,但它给了我这个错误:</p> <pre class="lang-bash prettyprint-override"><code>Access denied for user 'root'@'172.19.0.1' (using password: YES) </code></pre> <p>我该如何修复这个错误?同一内部网络中的其他容器也无法连接到该数据库。</p><p>我在这里给出Dockerfile的内容:</p><p><code></code></p> <pre class="brush:php;toolbar:false;">ADD init.sql init.sql</pre> <p>我给出了init的内容。sql在这里:</p> <pre class="brush:php;toolbar:false;">create database if not exists reviews; create database if not exists users; create database if not exists vendors; GRANT ALL PRIVILEGES on reviews.* to 'soumalya'@'%'; GRANT ALL PRIVILEGES on users.* to 'soumalya'@'%'; GRANT ALL PRIVILEGES on vendors.* to 'soumalya'@'%';</pre> <p>从容器日志中,我可以看到数据库正在启动并等待端口3306中的传入连接。</p>
P粉418854048P粉418854048452 天前484

全部回复(1)我来回复

  • P粉803527801

    P粉8035278012023-07-26 10:43:28

    正如@HansKillan所说,当数据库已经存在时,我更改了密码,更改的密码被忽略了,它正在使用旧密码。我不得不手动修改我的用户的密码写sql命令使用docker exec <container_name>…使用SQL命令修改用户密码。然后一切都成功了。在您的情况下,如果您的数据库不包含太多数据,那么您可以直接删除卷并重新创建容器以缩短时间

    回复
    0
  • 取消回复