mysql容器拒绝连接,Docker容器(Kubernetes):拒绝MySQL用户访问

Hi I have followed some k8s tutorials on how to get going with setting up a local db + WordPress installation, but user can't connect to mysql within my cluster.

(everything else seems ok - in Kubernetes Dashboard Web UI)

Error: [15:40:55][~]#kubectl logs -f website-56677747c7-c7lb6

[21-Nov-2019

11:07:17 UTC] PHP Warning: mysqli::__construct():

php_network_getaddresses: getaddrinfo failed: Name or service not

known in Standard input code on line 22 [21-Nov-2019 11:07:17 UTC] PHP

Warning: mysqli::__construct(): (HY000/2002):

php_network_getaddresses: getaddrinfo failed: Name or service not

known in Standard input code on line 22

MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo

failed: Name or service not known [21-Nov-2019 11:07:20 UTC] PHP

Warning: mysqli::__construct(): (HY000/1045): Access denied for user

'websiteu5er'@'10.1.0.35' (using password: YES) in Standard input code

on line 22

MySQL Connection Error: (1045) Access denied for user

'websiteu5er'@'10.1.0.35' (using password: YES)

MySQL Connection Error: (1045) Access denied for user

'websiteu5er'@'10.1.0.35' (using password: YES)

MySQL Connection Error: (1045) Access denied for user

'websiteu5er'@'10.1.0.35' (using password: YES)

My Dockerfile (which I used to create the image pushed to docker hub then pulled into k8s service + deployment):

FROM mysql:5.7

# This should create the following default root + user?

ENV MYSQL_ROOT_PASSWORD=hello123

ENV MYSQL_DATABASE=website

ENV MYSQL_USER=websiteu5er

ENV MYSQL_PASSWORD=hello123

RUN /etc/init.d/mysql start \

&& mysql -u root --password='hello123' -e "GRANT ALL PRIVILEGES ON *.* TO 'websiteu5er'@'%' IDENTIFIED BY 'hello123';"

FROM wordpress:5.2.4-php7.3-apache

# Copy wp-config file over

COPY configs/wp-config.php .

RUN chown -R www-data:www-data *

COPY ./src/wp-content/themes/bam /var/www/html/wp-content/themes/bam

解决方案

The standard Docker Hub mysql image has the ability to run arbitrary SQL scripts on the very first startup of the database only. It can also set up an initial database user with a known password, again on the first startup only. Details are in the linked Docker Hub page.

In a Kubernetes context I’d use just the environment variables, and specify them in my pod spec.

containers:

- name: mysql

image: mysql:5.7 # not a custom image

env:

- name: MYSQL_USER

value: websiteu5er

- name: MYSQL_PASSWORD

value: hello123

If you did need more involved setup, I’d create a ConfigMap that contained SQL scripts, and then mount that into the container in /docker-entrypoint-initdb.d.

There’s two things going on in your Dockerfile. One is that, when you have multiple FROM lines, you’re actually executing a multi-stage build; the image you get out at the end is only the Wordpress image, and the MySQL parts before it get skipped. The second is that you can’t actually create an image FROM mysql that contains any database-level configuration or content, so the image that comes out of the first stage has the environment variables set but won’t actually have executed your GRANT PRIVILEGES statement.

I’d just delete everything before the last FROM line and not try to build a derived MySQL image; use the /docker-entrypoint-initdb.d mechanism at startup time instead.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值