用Podman搭建LAMP开发环境的容器(二) -- 让数据库保存在本地

在博文用Podman搭建LAMP开发环境的容器(一)中记录了我在之前搞的apache+php容器上又安装了mariadb。

昨天还留了一个问题,就是启动容器的时候界面卡在启动mysqld_safe上了。我直接把entrypoint.sh中的mysqld_safe行改成:service mysql start,问题就解决了。

今天我打算把maraidb的数据文件保存在项目的目录中,而不是保存在容器里面。我知道在ubuntu下mariadb的数据默认是保存在/var/lib/mysql目录下的,所以我想是不是做创建(运行)容器的时候把项目用于保存数据的目录挂载到容器的/var/lib/mysql就行了?马上试试看:

E:\test>md database

E:\test>podman run --rm -it -p8080:80 -v ./:/workspaces/myproject -v database/:/var/lib/mysql/ -e MY_DOCUMENT_ROOT=/work
spaces/myprojects/src/public  lamp:ubuntu20.04
AH00112: Warning: DocumentRoot [/workspaces/myprojects/src/public] does not exist
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.88.0.85. Set the 'ServerName' directive globally to suppress this message
 * Starting MariaDB database server mysqld                                                                       [fail]
root@e73c6526f7b7:/#

糟糕,我看到了红色的[fail]。MariaDB启动失败了!为什么呢?看看日志中有没有线索:

root@e73c6526f7b7:/# cat /var/log/mysql/error.log
root@e73c6526f7b7:/#

错误日志竟然是空的?

手动初始化一下数据看看:

root@e73c6526f7b7:/# mysql_install_db
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following command:

'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.

Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

经过一段时间的等等,我看到了"OK"的字样,再手动启动MariaDB服务试试:

root@e73c6526f7b7:/# service mysql start
 * Starting MariaDB database server mysqld                                                                       [ OK ]
root@e73c6526f7b7:/# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.3.39-MariaDB-0ubuntu0.20.04.2 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

这次成功了。退出容器,再运行一次试试:

root@e73c6526f7b7:/# exit
exit

E:\test>podman run --rm -it -p8080:80 -v ./:/workspaces/myproject -v database/:/var/lib/mysql/ -e MY_DOCUMENT_ROOT=/workspaces/myprojects/src/public  lamp:ubuntu20.04
AH00112: Warning: DocumentRoot [/workspaces/myprojects/src/public] does not exist
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.88.0.86. Set the 'ServerName' directive globally to suppress this message
 * Starting MariaDB database server mysqld                                                                       [ OK ]
root@2fee83f44144:/# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.3.39-MariaDB-0ubuntu0.20.04.2 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

可以了。所以看来是需要先初始化数据库目录。这样我在entrypoint.sh里启动MariaDB服务之前先初始化数据库目录是否就可以了?仔细想想应该不行,因为entrypoint.sh是容器每次启动时都会运行的,如果这样做不是每次启动容器都会初始化一次数据库?只能再多做一些事情了,在容器启动时先判断数据库是否已经初始化了,如果是就直接启动MariaDB服务,否则初始化数据库后再启动MariaDB服务。修改entrypoint.sh:

#!/bin/bash

source /etc/apache2/envvars
apache2

if [ -d /var/lib/mysql/mysql ]; then
	service mysql start
else
	mysql_install_db
	service mysql start
fi

exec "$@"

重新构建容器镜像后,再删除之前用过的数据目录,然后启动容器试试。希望这次能成功:

E:\test>rd /s/q database

E:\test>md database

E:\test>podman run --rm -it -p8080:80 -v ./:/workspaces/myproject -v database/:/var/lib/mysql/ -e MY_DOCUMENT_ROOT=/workspaces/myprojects/src/public  lamp:ubuntu20.04
AH00112: Warning: DocumentRoot [/workspaces/myprojects/src/public] does not exist
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.88.0.89. Set the 'ServerName' directive globally to suppress this message
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following command:

'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.

Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

 * Starting MariaDB database server mysqld                                                                       [ OK ]
root@cad0d8fa3f6a:/# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.3.39-MariaDB-0ubuntu0.20.04.2 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

看样子是成功了。今天时间差不多可,明天继续。😃

我在这个基础上增加了一个phpMyAdmin,方便数据库的操作,有兴趣请看:用Podman搭建LAMP开发环境的容器(三)–增加phpMyAdmin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值