简体   繁体   English

在Kubernetes上使用Wordpress,MySQL,Nginx“建立数据库连接时出错”

[英]“Error establishing a database connection” using Wordpress, MySQL, Nginx on Kubernetes

I am deploying an app using Wordpress, php-fpm, nginx and mysql on Kubernetes. 我正在Kubernetes上使用Wordpress,php-fpm,nginx和mysql部署应用程序。 The steps I have taken are: 我采取的步骤是:

  1. deploy mysql using helm 使用头盔部署mysql
  2. expose the pod as a clusterIP service and dump my tables onto the database 将Pod公开为clusterIP服务并将我的表转储到数据库中
  3. deploy my wordpress/php app in the same pod with an nginx container 使用Nginx容器将我的wordpress / php应用程序部署在同一容器中
  4. expose clusterIP service and set up ingress / TLS 公开clusterIP服务并设置入口/ TLS

The database seems to be working, I can connect to it and see my tables using the following command: echo "mysql -pXXX" | kubectl exec -it <mysql-pod> 该数据库似乎正在运行,我可以使用以下命令连接到该数据库并查看我的表: echo "mysql -pXXX" | kubectl exec -it <mysql-pod> echo "mysql -pXXX" | kubectl exec -it <mysql-pod> . echo "mysql -pXXX" | kubectl exec -it <mysql-pod> Step 4 (ssl cert and ingress) is also working and no problem there. 第4步(ssl cert和ingress)也可以正常工作,并且在那里没有问题。 Creating the two pods (my app and mysql), and adding the config files result in this message when I try to access my domain: 当我尝试访问我的域时,创建两个Pod(我的应用程序和mysql)并添加配置文件会显示以下消息:

Error establishing a database connection 建立数据库连接时出错

I deploy mysql pod using helm through this command: 我通过以下命令使用头盔部署mysql pod:

helm install --name mysql --set \
mysqlRootPassword=xxx,mysqlUser=xxx,mysqlPassword=xxx, \
mysqlDatabase=xxx,persistence.size=50Gi \
stable/mysql

Once this is deployed, the pod runs and I can access my database successfully. 部署完成后,pod将运行,我可以成功访问数据库。 This is when I dump my tables data from a local .sql file onto the created database, and when I run show tables they all exist on the persistent storage. 这是当我将表数据从本地.sql文件转储到创建的数据库中时,以及当我运行show tables它们都存在于持久性存储中。 This part seems to be working properly. 这部分似乎工作正常。

I am deploying my wordpress app and nginx containers in one pod, for mutual persistent volume use. 我正在将我的wordpress应用程序和nginx容器部署在一个容器中,以便相互持久使用。 The deployment yaml looks like this: 部署yaml如下所示:

wordpress-deployment.yaml WordPress的-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - image: nginx:alpine
        name: nginx
        env:
        - name: WP_HOST
          value: wordpress
        - name: DB_HOST
          value: mysql:3306
        - name: DB_NAME
          value: xxx
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql
              key: password
        ports:
        - containerPort: 443
        - containerPort: 80
        volumeMounts:
          - name: wordpress-persistent-storage
            mountPath: /var/www/html
          - name: wp-config
            mountPath: "/etc/nginx/conf.d"
      - image: my-wordpress-php-app
        name: wordpress
        env:
        - name: MY_DB_HOST
          value: mysql:3306
        - name: MY_DB_NAME
          value: xxx
        - name: MY_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql
              key: password
        - name: MY_WP_HOME
          value: "https://example.com"
        - name: MY_WP_SITEURL
          value: "https://example.com"
        - name: WP_DEBUG_LOG
          value: "true"
        - name: WP_DEBUG
          value: "true"
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wordpress-volumeclaim
      - name: wp-config
        configMap:
          name: wp-config
          items:
          - key: wp.conf
            path: wp.conf
      imagePullSecrets:
      - name: regcred

I have confirmed that I can access my database through my wordpress pod by simply connecting to mysql:3306. 我已经确认,只需连接到mysql:3306,就可以通过wordpress pod访问数据库。 I checked and confirmed that my app works through docker compose on a server, and the code seems fine, so we can assume the wordpress app's docker image is also doing what it should. 我检查并确认我的应用程序可以通过docker compose在服务器上运行,并且代码看起来还不错,因此我们可以假定wordpress应用程序的docker映像也可以正常工作。

For reference, my config file is as follows: wp.conf 供参考,我的配置文件如下: wp.​​conf

    listen 80;
    listen 443 ssl;
    server_name $SITE_URL;

    root /var/www/html;
    index index.php;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip off;

    types {
        ...
    }

    location xxx {
        rewrite .* /index.php;
        ...
    }

    location ~ '\.php$' {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        ...
    }

    location / {
        autoindex off;
        ...
    }
}

Some extra info in case it helps: 一些额外的信息,以防万一:

  • Both of my services (mysql and wordpress/nginx) are ClusterIP type. 我的两个服务(mysql和wordpress / nginx)都是ClusterIP类型。 In my mysql service I have the following: 在我的mysql服务中,我有以下内容:
    - port: 3306
      targetPort: 3306

And in my wordpress service I have the following: 在我的wordpress服务中,我有以下内容:

  - name: wordpress
    port: 9000
    targetPort: 9000
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    targetPort: 443
    protocol: TCP
    name: https

I have added the mysql password as a secret with the proper yaml file and base64 value. 我已经添加了mysql密码作为密码,并带有正确的yaml文件和base64值。 I have also tried using the command line instead for creating the secret, and both don't change anything in the results. 我也尝试过使用命令行来创建机密,并且两者都不会改变结果。

Here are some logs in case it can tell anything about the problem (I couldn't find much with that regards): 以下是一些日志,以防它可以告诉您有关该问题的任何信息(就此而言,我找不到太多):

Mysql pod logs MySQL Pod日志

MySQL init process in progress... MySQL初始化程序正在进行中...
Warning: Unable to load '/usr/share/zoneinfo/Factory' as time zone. 警告:无法加载“ / usr / share / zoneinfo / Factory”作为时区。 Skipping it. 跳过它。
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. 警告:无法将“ /usr/share/zoneinfo/iso3166.tab”加载为时区。 Skipping it. 跳过它。
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. 警告:无法加载“ /usr/share/zoneinfo/leap-seconds.list”作为时区。 Skipping it. 跳过它。
Warning: Unable to load '/usr/share/zoneinfo/posix/Factory' as time zone. 警告:无法将“ / usr / share / zoneinfo / posix / Factory”加载为时区。 Skipping it. 跳过它。
Warning: Unable to load '/usr/share/zoneinfo/right/Factory' as time zone. 警告:无法将“ / usr / share / zoneinfo / right / Factory”加载为时区。 Skipping it. 跳过它。
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. 警告:无法将“ /usr/share/zoneinfo/zone.tab”加载为时区。 Skipping it. 跳过它。
mysql: [Warning] Using a password on the command line interface can be insecure. mysql:[警告]在命令行界面上使用密码可能不安全。
MySQL init process done. MySQL的初始化过程完成了。 Ready for start up. 准备启动。

Nginx container logs Nginx容器日志

[11:15:03 +0000] "GET /robots.txt HTTP/1.1" 500 262 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; + http://www.google.com/bot.html )" [11:15:03 +0000]“ GET /robots.txt HTTP / 1.1” 500262“-”“ Mozilla / 5.0(兼容; Googlebot / 2.1; + http://www.google.com/bot.html ) “
10.20.0.128 - - [12:17:48 +0000] "GET / HTTP/1.1" 500 262 "-" "Python/3.6 aiohttp/3.4.4" 10.20.0.128--[12:17:48 +0000]“ GET / HTTP / 1.1” 500262“-”“ Python / 3.6 aiohttp / 3.4.4”
10.20.0.128 - - [16:04:42 +0000] "GET / HTTP/1.1" 500 262 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/x Safari/537.36" 10.20.0.128--[16:04:42 +0000]“ GET / HTTP / 1.1” 500262“-”“ Mozilla / 5.0(Macintosh; Intel Mac OS X)AppleWebKit / 537.36(KHTML,like Gecko)Chrome / x Safari浏览器/ 537.36"
10.20.0.128 - - [16:04:42 +0000] "GET /favicon.ico HTTP/1.1" 200 5 " https://example.com/ " "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/x Safari/537.36" 10.20.0.128--[16:04:42 +0000]“ GET /favicon.ico HTTP / 1.1” 200 5“ https://example.com/ ”“ Mozilla / 5.0(Macintosh; Intel Mac OS X)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / x Safari / 537.36“

Wordpress container logs WordPress的容器日志

127.0.0.1 - 16:04:42 +0000 "GET /index.php" 500 127.0.0.1-16:04:42 +0000“ GET /index.php” 500
127.0.0.1 - 16:04:42 +0000 "GET /index.php" 200 127.0.0.1-16:04:42 +0000“ GET /index.php” 200

I personally think there is something simple missing here, but I haven't been able to point it out in the past few days. 我个人认为这里缺少一些简单的东西,但是过去几天我一直无法指出。 Anyone know what I'm missing here? 有人知道我在这里想念的吗?

I solved this problem by replacing the secret I used to access my database from my wordpress pod. 我通过替换用于从Wordpress Pod访问数据库的秘密解决了这个问题。 To confirm that the secret was the issue, I created a secret and deployed mysql with a secret reference for MYSQL_ROOT_PASSWORD and giving the same secret reference as the db password in my wordpress pod. 为了确认这个秘密是问题所在,我创建了一个秘密,并部署了带有MYSQL_ROOT_PASSWORD秘密参考的mysql ,并在wordpress pod中提供了与db密码相同的秘密参考。 Since they were both using the same secret object this worked and fixed the db connection issue. 由于它们都使用相同的秘密对象,因此可以解决数据库连接问题。

FIX: Helm automatically creates a db secret that includes all of your password / used / db name entries. FIX:Helm自动创建一个数据库秘密,其中包括您的所有密码/使用过的/数据库名称条目。 Use the automatically created secret in the wordpress yaml file instead of the one you create: 在wordpress yaml文件中使用自动创建的密码,而不要使用您创建的密码:

helm install --name mysql-helm --set \
mysqlRootPassword=xxx,mysqlUser=xxx,mysqlPassword=xxx, \
mysqlDatabase=xxx,persistence.size=50Gi \
stable/mysql

Use the created password secret like this: 使用创建的密码机密,如下所示:

        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-helm
              key: password

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL Wordpress Nginx dnsmasq:错误:建立数据库连接时出错 - MySQL Wordpress Nginx dnsmasq: Error: Error establishing a database connection 建立数据库连接时出错-WordPress和MySQL - Error establishing a database connection - WordPress and mysql 使用Wordpress建立数据库连接时出错 - Error establishing a database connection using Wordpress AWS Kubernetes 集群 - WordPress 建立数据库连接时出错 - AWS Kubernetes Cluster - WordPress Error establishing a database connection 在 macOS 上配置 Wordpress + mysql 8.0x + nginx 时出现“建立数据库连接时出错” - "Error establishing a database connection" when config Wordpress + mysql 8.0x + nginx on macOS Running Wordpress on Docker, NGinx, php &amp; sql - Error establishing a database connection - Running Wordpress on Docker, NGinx, php & sql - Error establishing a database connection 在Wordpress上建立数据库连接时出错 - Error establishing a database connection on wordpress WordPress 建立数据库连接时出错 - WordPress Error establishing a database connection Wordpress - 建立数据库连接时出错 - Wordpress - Error establishing a database connection 如何在docker中运行的wordpress和mysql中解决“建立数据库连接时出错” - How to solve “Error establishing a database connection” with wordpress and mysql running in docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM