wordpress - How Can I Connect wp-cli and MariaDB in Docker - Stack Overflow

First of all i should have to say that i can't use images that prepared. I have created these file

First of all i should have to say that i can't use images that prepared. I have created these files;

docker-compose.yaml
srcs/
    |---> mariadb/
    |---> nginx/
    |---> wp-cli/

But after a few tries wp-cli and mariadb connection has a problem. Because when open the localhost it says "Error establishing a database connection". After wp-cli conteiner, i have created mariadb conteiner. Probably i made a mistake while creating MariaDB. So these are my dockerfile for MariaDB

FROM debian:buster

RUN apt-get update && apt-get upgrade -y && apt-get install -y \
    mariadb-server \
    dos2unix

EXPOSE 3306

STOPSIGNAL SIGTERM

COPY ./conf/50-serverf /etc/mysql/mariadb.conf.d/

COPY ./tools/script.sh /script.sh

RUN chmod +x /script.sh

RUN dos2unix script.sh

ENTRYPOINT [ "/script.sh" ]

Also here is my sh file;

#!/bin/bash

chown -R mysql:mysql /var/lib/mysql
chmod -R 755 /var/lib/mysql

mysqld_safe --datadir=/var/lib/mysql &

sleep 30

cat << EOF > db1.sql
CREATE DATABASE IF NOT EXISTS $database_name;
CREATE USER IF NOT EXISTS '$mysql_user'@'%' IDENTIFIED BY '$mysql_password';
GRANT ALL PRIVILEGES ON $database_name.* TO '$mysql_user'@'%';
FLUSH PRIVILEGES;
EOF

mysql -h $mysql_host -u root -p$mysql_password < db1.sql

cat << EOF > wp_setup.sql
USE $database_name;
INSERT INTO wp_users (user_login, user_pass, user_email, user_registered, user_status, display_name)
VALUES ('$wordpress_admin', MD5('$wordpress_admin_password'), '$wordpress_admin_email', NOW(), 0, '$brand')
ON DUPLICATE KEY UPDATE user_pass = VALUES(user_pass), user_email = VALUES(user_email), display_name = VALUES(display_name);

INSERT INTO wp_users (user_login, user_pass, user_email, user_registered, user_status, display_name)
VALUES ('$login', MD5('$wp_user_pwd'), '$wp_user_email', NOW(), 0, '$login')
ON DUPLICATE KEY UPDATE user_pass = VALUES(user_pass), user_email = VALUES(user_email), display_name = VALUES(display_name);
EOF

mysql -h $mysql_host -u root -p$mysql_password < wp_setup.sql

exec mysqld

How can i fix my problem?

First of all i should have to say that i can't use images that prepared. I have created these files;

docker-compose.yaml
srcs/
    |---> mariadb/
    |---> nginx/
    |---> wp-cli/

But after a few tries wp-cli and mariadb connection has a problem. Because when open the localhost it says "Error establishing a database connection". After wp-cli conteiner, i have created mariadb conteiner. Probably i made a mistake while creating MariaDB. So these are my dockerfile for MariaDB

FROM debian:buster

RUN apt-get update && apt-get upgrade -y && apt-get install -y \
    mariadb-server \
    dos2unix

EXPOSE 3306

STOPSIGNAL SIGTERM

COPY ./conf/50-server.cnf /etc/mysql/mariadb.conf.d/

COPY ./tools/script.sh /script.sh

RUN chmod +x /script.sh

RUN dos2unix script.sh

ENTRYPOINT [ "/script.sh" ]

Also here is my sh file;

#!/bin/bash

chown -R mysql:mysql /var/lib/mysql
chmod -R 755 /var/lib/mysql

mysqld_safe --datadir=/var/lib/mysql &

sleep 30

cat << EOF > db1.sql
CREATE DATABASE IF NOT EXISTS $database_name;
CREATE USER IF NOT EXISTS '$mysql_user'@'%' IDENTIFIED BY '$mysql_password';
GRANT ALL PRIVILEGES ON $database_name.* TO '$mysql_user'@'%';
FLUSH PRIVILEGES;
EOF

mysql -h $mysql_host -u root -p$mysql_password < db1.sql

cat << EOF > wp_setup.sql
USE $database_name;
INSERT INTO wp_users (user_login, user_pass, user_email, user_registered, user_status, display_name)
VALUES ('$wordpress_admin', MD5('$wordpress_admin_password'), '$wordpress_admin_email', NOW(), 0, '$brand')
ON DUPLICATE KEY UPDATE user_pass = VALUES(user_pass), user_email = VALUES(user_email), display_name = VALUES(display_name);

INSERT INTO wp_users (user_login, user_pass, user_email, user_registered, user_status, display_name)
VALUES ('$login', MD5('$wp_user_pwd'), '$wp_user_email', NOW(), 0, '$login')
ON DUPLICATE KEY UPDATE user_pass = VALUES(user_pass), user_email = VALUES(user_email), display_name = VALUES(display_name);
EOF

mysql -h $mysql_host -u root -p$mysql_password < wp_setup.sql

exec mysqld

How can i fix my problem?

Share Improve this question asked Mar 24 at 12:03 acbst0acbst0 114 bronze badges 1
  • 1 What exactly is the problem? Can you use the standard Docker Hub mariadb image instead of spinning your own? Can you run the database migrations from your application container instead of trying to tie application-specific setup to your database startup? – David Maze Commented Mar 24 at 12:36
Add a comment  | 

1 Answer 1

Reset to default 0

Simplifying with the existing MariaDB Docker Official Image:

wp_setup/ contains the wp_setup.sql file or a script.

version: "3"

services:
  mariadb:
    image: mariadb:lts
    environment:
      - MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
      - MARIADB_DATABASE=${MARIADB_DATABASE}
      - MARIADB_USER=${MARIADB_USER}
      - MARIADB_PASSWORD=${MARIADB_PASSWORD}
    volumes:
      - mariadbdata:/var/lib/mysql
      - ./wp_setup:/docker-entrypoint-initdb.d/
volumes:
  mariadbdata:

The database is created if it doesn't exist and the grants fro MARIADB_USER are applied to fully access the databse.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744253884a4565299.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信