Persistent setup of MariaDB in Docker container - mysql

I am trying to setup MariaDB in a Docker container for CI/CD testing.
However, the way I do it the DB does not exist when I log into the container.
Here's parts of my Dockerfile
FROM debian:stable-slim
RUN apt -y install mariadb-server mariadb-client
EXPOSE 3306
ADD ./DBShema.sql /db/DBShema.sql
RUN mysqld_safe \
mysqladmin --silent --wait=30 ping \
mysql < /db/DBShema.sql
Does not show my DB / tables.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
How should that go properly to make the piped DB info persistent?

I think you are overcomplicating things. I do not see the point of starting from a plain Debian installation that requires you to install the RDBMS manually.
There are MariaDB images already made that you can use as a starting point for your CI images. Take the following Dockerfile as an example:
FROM mariadb:10.6.4-focal
COPY schema.sql /docker-entrypoint-initdb.d
This effectively copies over an sql script into the directory that is used to initialize the database on the first container start. This is mentioned in this portion of the documentation.
Once this is done the spun off container will contain all the changes included in the related sql file. I hope this is what you want.

Related

How to debug why phpmyadmin doesn't allow login to mysql when it was just working

I'm not sure what is happening or why this is happening so hopefully someone can give me tips on how to debug or where to look.
I have 2 docker containers, 1 for phpmyadmin and another for mysql. The phpmyadmin container is linked with the mysql container. When everything is/was working, I am able to log into my database thru the phpmyadmin login page with credentials I had setup.
However, and this has happened a couple times in the past already; yesterday I can log into mysql thru phpmyadmin, today when I try to log into mysql thru phpmyadmin, I'm not able to and I got a "Cannot log in to the MySQL server. mysqli::real_connect(): (HY000/2002): No such file or directory. I don't do anything to my host server and it happens.
Also, there doesn't seem to be a set pattern of time either. One time I was able to log in for 3 months before this happened, another time, it was 3 wks, and yet another time it was 6 days before I couldn't log in.
The funny thing is I can log into mysql via the cli after I log into the mysql docker container so mysql wasn't broken or down:
$ docker exec -it mysql /bin/bash
root#3752a707feb7:/# mysql --u username -ppwd
...
...
Your MySQL connection id is 12489
Server version: 8.0.23 MySQL Community Server - GPL
...
...
mysql> show databases
-> ;
+----------------------+
| Database |
+----------------------+
| information_schema |
| mysql |
...
...
+----------------------+
8 rows in set (0.03 sec)
mysql>
When I check my host and docker, I don't see any evidence the host or docker containers restarted.
$ w
16:16:59 up 160 days, 22:27, 3 users, load average: 0.02, 0.01, 0.00
...
...
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eead5d490e89 phpmyadmin "/docker-entrypoint.…" 3 months ago Up 3 months 0.0.0.0:8080->80/tcp myadmin
3752a707feb7 mysql:latest "docker-entrypoint.s…" 18 months ago Up 5 months 33060/tcp, 0.0.0.0:33060->3306/tcp mysql
...
...
$ docker events --since '2022-05-01' | grep eead5d490e89
(Doesn't show any container restarts or container destroys. I can put the log lines here if needed but it was A LOT of log lines)
The "fix" I did before was to just restart the phpmyadmin container and tell it to reconnect to mysql. I'd like to avoid doing this if possible and figure out what is making it disconnect in the first place.
What else can I check to see why from time to time the phpmyadmin docker container doesn't allow me to log into the mysql docker container?
I've had this error, I changed my port to 8080 and it worked
Are you trying to initialise mysql-server with any database? I have observed that in case a heavy sql file is loaded to mysql-server, it requires time to complete the import process before it becomes accessible through remote login.

mysql fror mac, create databases ERROR 3680 (HY000) [duplicate]

I can't create a database after logging in mysql under my root account. Do I have to make an admin account to do so? Also, for some reason, my StartUp file didn't install (there was an error). I'm not sure if that will affect anything else since mySQL DOES start up when I type "mysql" into my terminal.
Also when I type in
mysql> SELECT Host, User FROM mysql.user;
+---------------------+------+
| Host | User |
+---------------------+------+
| 127.0.0.1 | root |
| ::1 | root |
| myname-mac.att.net | |
| myname-mac.att.net | root |
| localhost | |
| localhost | root |
+---------------------+------+
Which I don't get. I seem to have multiple root users and I don't know what ::1 means.
EDIT: My databases currently look like this.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.02 sec)
And it doesn't matter what I type in as my database name. I even tried calling it 'apple'.
It might be problem with space.
Follow this
Check .err logs at /var/lib/mysql
if the log says something like
"[ERROR] Can't start server: can't create PID file: No space left on device"
Check /var size by df -hk /var
if used is 100% , then u have to find the files which is geting filled.
find large file in /var by
find /var/ -type f -size +100000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
see which file you can delete and then restart the mysql process by
/etc/init.d/mysql restart
let me know if that worked :)
If you're on macosx and if you've system preference pane installed, that should show a message like
the following directory is not owned by _mysql user - "/usr/local/msyql/data"
Once you know that path you can do the following:
sudo chown -R mysql:mysql /usr/local/mysql/data
sudo chown -R mysql:mysql <path>
You have one root user for several domains. Meaning you can connect and run queries on that database FROM the specified domains.
If you want to only show one, give it '%' for the domain and remove all others, although that is not advised. Save the root user for run rights only from localhost, and create limited users for running queries from outside.
As for test database error, it happens on fresh installs. Just reboot the mysql server(stop/start process) or the computer.
Also, make sure you have full rights by doing
GRANT ALL PRIVILEGES ON *.* TO 'root'#'thedomainyourunfrom/localhost/%' WITH GRANT OPTION;
this will give your root user full rights across all databases in the server
osx manual http://dev.mysql.com/doc/mysql-macosx-excerpt/5.0/en/macosx-installation.html
As an additional resource, you can try two other things:
Find out the data folder for your MySQL and 'chown' it so that mysql can write properly to it. For example, if your MySQL's data folder is /usr/local/mysql/data/, you can 'chown' it by typing up the command chown -R mysql:mysql /usr/local/mysql/data/
If you have just installed your MySQL server, try restarting your computer. Sometimes the installer fails on giving proper file access to the program
I hope that helps!

use vagrant to provision simple mysql setup (with shell provisioner)

It's a breeze to provision Vagrant to echo "hello world". Heck, even I can handle that!
But when it comes to doing things a user would actually like to do when provisioning a box, such as installing mysql, creating a database, and creating a mysql user, well... those things just don't seem to work. ARRG.
I'm working with a virtual CentOS server and installing MySQL goes off without a hitch, but that's about where I meet the proverbial brick wall. Bear in mind, I can do all these things easily by SSH'ing into the box (but of course that defeats the purpose of provisioning):
echo "hello world"; <-- good
sudo yum install mysql-server -y; <-- good
sudo /sbin/service mysqld start; <-- good
sudo mysql create user 'mycooluser'#'%' identified by 'mypassword'; <-- BAD
I've been Googling all over the place and the closest I've come is this article, but since it is based on Ubuntu rather than CentOS, I got stuck right away since apparently yum doesn't know what debconf-utils is, while apt does.
Can anyone shed some light on this for me? I don't think mine is an outrageous use-case, all I want to do is have the shell provisioner
install mysql
create database
create user and grant him all privileges
run db_setup.sql to add table structure and data to new database
Please don't tell me to "use chef or puppet". I've already tried and they had their own sets of issues and honestly feel like overkill for these simple requirements. Surely these items can be accomplished with a shell provisioner?
Thanks a lot for any insight. You may be able to tell I'm at my wit's end.
Here is my Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
$script = <<SCRIPT
set -x
echo I am provisioning...
date > /etc/vagrant_provisioned_at
echo "hello world"
yum install mysql-server -y
/sbin/service mysqld start
mysql -e "create user 'mycooluser'#'%' identified by 'mypassword'"
SCRIPT
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "chef/centos-6.5"
config.vm.provision :shell, :inline => $script
end
vagrant up should get you a centos-6.5 OS with mysql and the user 'mycooluser'.
To verify:
vagrant ssh to the box after it has finished provisioning and perform the following within the centos guest OS.
mysql -uroot
mysql> SELECT User FROM mysql.user;
+------------+
| User |
+------------+
| mycooluser |
| root |
| |
| root |
| |
| root |
+------------+
6 rows in set (0.00 sec)
Note: You will probably want to grant privileges for 'mycooluser' in order to login using the mysql shell etc.
More details can be found here.

creating a default database in mysql using dockerfile

Im using this tutum-docker-mysql dockerfile, to get up and running with a docker image with mysql installed in it,the docker file creates a user with the name root and no password, what i need is to add something like this to the dockerfile:
RUN mysql -uroot -p"" && mysql create database test;
So when I build an image from the docker file, the database should already be there.
I was able to accomplish the goal of adding a database to the tutum-docker-mysql image by doing the following.
git clone https://github.com/tutumcloud/tutum-docker-mysql.git
cd tutum-docker-mysql
vi create_mysql_admin_user.sh
Inside of that .sh file I added a line, right below the two "mysql -uroot" lines that are already there. I simply added:
mysql -uroot -e "create database test;"
After that change to that .sh file, I simply built the docker image.
docker build -t tutum/mysql .
After the docker image was built, I could run it with something like:
docker run -d -p 3307:3306 tutum/mysql
Once the container is running, you need to know the password to use and the ip address of the container. To get the password you simply do
docker logs 2976a81f1a9b19787d9bde893c831b7e6586d7c8391ccd222ad29b02c282d896
But of course use the container id, that was returned from the "docker run" command above. Now that you have the password, you need the ip address. I get that by doing.
docker inspect 2976a81f1a9b19787d9bde893c831b7e6586d7c8391ccd222ad29b02c282d896
And looking at the "Gateway" address. With the password and the ip address I was then able to do this mysql command from OUTSIDE of the container.
mysql -uadmin -pJcM5FCphMOp4 -h172.17.42.1 -P3307
Where the ip address and the password are the values I got from the previous two docker commands. After that command has run, I can then issue a "show databases" command with the following results.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
I fought for a while trying it by modifying the Dockerfile. I think it might be possible, but after a while, I found the above solution a lot quicker and simpler.
You can pass ON_CREATE_DB env parameter on container startup.
Example from tutum README.md
docker run -d -p 3306:3306 -e ON_CREATE_DB="newdatabase" tutum/mysql

mysql> create database test; ERROR 1006 (HY000): Can't create database 'test' (errno: 2)

I can't create a database after logging in mysql under my root account. Do I have to make an admin account to do so? Also, for some reason, my StartUp file didn't install (there was an error). I'm not sure if that will affect anything else since mySQL DOES start up when I type "mysql" into my terminal.
Also when I type in
mysql> SELECT Host, User FROM mysql.user;
+---------------------+------+
| Host | User |
+---------------------+------+
| 127.0.0.1 | root |
| ::1 | root |
| myname-mac.att.net | |
| myname-mac.att.net | root |
| localhost | |
| localhost | root |
+---------------------+------+
Which I don't get. I seem to have multiple root users and I don't know what ::1 means.
EDIT: My databases currently look like this.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.02 sec)
And it doesn't matter what I type in as my database name. I even tried calling it 'apple'.
It might be problem with space.
Follow this
Check .err logs at /var/lib/mysql
if the log says something like
"[ERROR] Can't start server: can't create PID file: No space left on device"
Check /var size by df -hk /var
if used is 100% , then u have to find the files which is geting filled.
find large file in /var by
find /var/ -type f -size +100000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
see which file you can delete and then restart the mysql process by
/etc/init.d/mysql restart
let me know if that worked :)
If you're on macosx and if you've system preference pane installed, that should show a message like
the following directory is not owned by _mysql user - "/usr/local/msyql/data"
Once you know that path you can do the following:
sudo chown -R mysql:mysql /usr/local/mysql/data
sudo chown -R mysql:mysql <path>
You have one root user for several domains. Meaning you can connect and run queries on that database FROM the specified domains.
If you want to only show one, give it '%' for the domain and remove all others, although that is not advised. Save the root user for run rights only from localhost, and create limited users for running queries from outside.
As for test database error, it happens on fresh installs. Just reboot the mysql server(stop/start process) or the computer.
Also, make sure you have full rights by doing
GRANT ALL PRIVILEGES ON *.* TO 'root'#'thedomainyourunfrom/localhost/%' WITH GRANT OPTION;
this will give your root user full rights across all databases in the server
osx manual http://dev.mysql.com/doc/mysql-macosx-excerpt/5.0/en/macosx-installation.html
As an additional resource, you can try two other things:
Find out the data folder for your MySQL and 'chown' it so that mysql can write properly to it. For example, if your MySQL's data folder is /usr/local/mysql/data/, you can 'chown' it by typing up the command chown -R mysql:mysql /usr/local/mysql/data/
If you have just installed your MySQL server, try restarting your computer. Sometimes the installer fails on giving proper file access to the program
I hope that helps!