Installing Mysql on a VM under Alpine - mysql

I'm trying to install and use mysql on Alpine. I'm using Docker to generate a VM under Alpine.
My Dockerfile is really simple :
FROM alpine:3.11.3
CMD sh
Once I've run the image created (using docker build // docker run image_id), I install Mysql :
apk add --update --upgrade mysql mysql-client
Then, I install the database :
mysql_install_db
The problem is that once I've done that and I try to "mysql", the machine returns this :
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
And when I try to do "mysqld -u root", it returns me this problem :
2020-08-26 10:07:55 0 [ERROR] Could not open mysql.plugin table. Some plugins may be not loaded
2020-08-26 10:07:55 0 [ERROR] Can't open and lock privilege tables: Table 'mysql.servers' doesn't exist
2020-08-26 10:07:55 0 [ERROR] Can't start server : Bind on unix socket: No such file or directory
2020-08-26 10:07:55 0 [ERROR] Do you already have another mysqld server running on socket: /run/mysqld/mysqld.sock ?
2020-08-26 10:07:55 0 [ERROR] Aborting
I understood that the file mysqld.sock serves the server to discuss with the client. I tried to create the directory mysqld and then to create the file mysqld.sock but it doesn't work. It returns me the same error but with a different return value.
(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (111)
At least, I don't want to use an existing image of mysql on Dockerhub.
Do anybody has an idea of what step is missing to make it work ?
Sorry if I'm not writing proper, I'm not a native english speaker.

I was able to successfully install and start mysql on an alpine container with these commands:
$ docker run -it --rm alpine:latest
/ # apk add mysql mysql-client
/ # mkdir /run/mysqld
/ # mysql_install_db
/ # mysqld -u root --data=./data &> /dev/null &
Testing connection:
/ # mysql -e "SELECT VERSION();"
+----------------+
| VERSION() |
+----------------+
| 10.6.4-MariaDB |
+----------------+

Related

MySql docker container not starting when using a network share for data directory

I'm running docker in Ubuntu and trying to create and run a MySql container. I want to use a mounted network share for the data directory. I am trying the following docker run command, but I'm having issues with permissions. How do I fix this?
root#jarvis:/mnt/wayne/mysql-data$ sudo docker run -it -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -v /mnt/wayne/mysql:/var/lib/mysql/ --name mysqlserver mysql/mysql-server
[Entrypoint] MySQL Docker Image 8.0.20-1.1.16
[Entrypoint] Initializing database
2020-06-08T21:43:25.253898Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 22
2020-06-08T21:43:25.281460Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-06-08T21:43:27.815075Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysqld: Cannot change permissions of the file 'ca.pem' (OS errno 1 - Operation not permitted)
2020-06-08T21:43:29.851875Z 0 [ERROR] [MY-010295] [Server] Could not set file permission for ca.pem
2020-06-08T21:43:29.852970Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
2020-06-08T21:43:29.854806Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-06-08T21:43:31.947298Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20) MySQL Community Server - GPL.
You use CIFs for network mount means the remote server is windows right? My answer is based on this assumption.
The latest mysql docker image has a user named mysql and its uid=27,gid=27
You verify this by mounting an empty folder as data_dir. You will see that the files created by mysql container has user and group is as 27.
Hence the mysql container expects files with uid/gid(owner userid and owner group id) as 27 in its data_dir. But the files that you mounted from the windows share has uid/gid which belongs to the user that executes mount command in ubuntu. This is the default behavior of mount command.
To solve this you need to pass "uid=27,gid=27" parameters to the Linux mount command.
For instance
sudo mount -t cifs -o
username=windows-username,uid=27,gid=27
//WIN_SHARE_IP/ /mnt/wayne
You can have look here for further details
I must say it is unlikely to run mysql over a network share. It won't perform well.
This is not exactly with MySQL but I hope it can give you an idea, I basically use this for testing against a MySQL database from my local environment, for this I use docker-compose and MariaDB, I configure the "data-dir" as a volume so that I can stop/start the docker container without the need to "seed" every time the database.
This is the content of the /your/path/docker-compose.yml file:
---
version: '3'
services:
mariadb:
image: mariadb:10.4.13
container_name: mariadb
restart: always
ports:
- 13306:3306
environment:
MYSQL_DATABASE: world
MYSQL_ROOT_PASSWORD: test
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
volumes:
- ${PWD}/mariadb/db/:/var/lib/mysql
In the same directory, I have the volume directory /your/path/mariadb/db
Then to bring up the container I use:
$ docker-compose up
From the docker-compose.yml has you can see I use port 13306 therefore for testing/connection I use:
$ mysql -h 127.0.0.1 -P13306 -uroot -p
All the data (databases) will be in /your/path/mariadb/db
If you run into the same "permissions" problem:
mysqld: Cannot change permissions of the file 'ca.pem' (OS errno 1 - Operation not permitted)
Try to change the permissions of your volume/mount point, for example:
chmod -R 777 /your/volume/mount_point
okay, I tried this and google also, what I found is
https://github.com/docker-library/mysql/issues/302#issuecomment-308745834
So basically if you are using mysql:5.7 then upgrade to mysql:5.7.16.
And if this doesn't help then I have one more solution.
Basically the problem is you are sharing dir to container -v /mnt/wayne/mysql:/var/lib/mysql/ but you ubuntu is not giving permission to access the /mnt/wayne/mysql dir. so give admin permission to this location or you can create a docker user chown and chmode.
Basically give permission to the host machine directory. so that docker container can access it.
and One more thing give permission to the docker container dir also, that is showing in your error
The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
Create a user in a docker container which have chown and chmod permissions to the dir /var/lib/mysql/.
if you are using dockerfile to create mysql container then use these following 2 lines in it
FROM mysql:5.7.16
WORKDIR /app
RUN chown -R admin:admin /app
RUN chmod 755 /app
USER admin
CMD ["Your command"]
To operate normally, MariaDB or MySQL needs to set some permissions on their own files. Some external file systems (such as FTP and many others) do not support these features. You need to use a file system which supports these features.
there is a permission issue to access the mounted volume. Please read the documentation about use volumes:
https://docs.docker.com/storage/volumes/#use-a-volume-driver
For NFSv3 Partition:
$ docker service create -d \
--name nfs-service \
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,volume-opt=o=addr=10.0.0.10' \
nginx:latest
Or check the CA.pem file permissions (use chmod 777 /path/to/ca.pem)
For NFSv4 Partition:
docker service create -d \
--name nfs-service \
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,"volume-opt=o=10.0.0.10,rw,nfsvers=4,async"' \
nginx:latest
Check https://docs.docker.com/storage/volumes/#use-a-volume-driver

Mysql with docker: Can't connect to local MySQL server through socket

I cannot use MySQL anymore in my Docker container:
root#mysql-container:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
mysqld is running:
root#mysql-spirit-ssl:/etc/mysql/conf.d# /etc/init.d/mysql start
[info] A MySQL Server is already started.
Trying to stop mysqld timed out:
root#mysql-container:/# /etc/init.d/mysql stop
............................................................[info] Attempt to shutdown MySQL Community Server 5.7.17 timed out.
So I tried to start using the mysqladmin way:
root#mysql-container:/# /usr/bin/mysqladmin --port=8889 -u root shutdown
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)'
So I checked that MySQL daemon is running:
root#mysql-container:/# ps -eax
PID TTY STAT TIME COMMAND
1 ? Ssl 0:01 mysqld
And that socket exists:
root#mysql-container:/# ls -l /var/run/mysqld/mysqld.sock
-rwxrwxrwx. 1 mysql mysql 0 Jan 4 10:12 /var/run/mysqld/mysqld.sock
I already tried to:
restart my Docker container
comment bind address in my.cnf and restart my Docker container
kill mysqld process => does not work, process is still listed by ps -eax
recreate my Docker container
restart Docker
restart the server
delete pid and sock files, and /etc/init.d/mysql start
Result of cat /var/log/mysql/error.log:
2018-02-27T15:27:35.966028Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2018-02-27T15:27:35.966061Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
However I cannot kill that mysqld process, either with pkill mysqld, kill -9 1 or initctl --system stop mysql.
Could this be related to Docker?
Remark: The MySQL daemon could not be killed because it was owned by Docker user systemd+ and it was the entry point of the container. Indeed mysqld was process with PID 1. This means that MySQL daemon could be restarted by simply restarting the Docker container, and that MySQL configuration could be modified in between.
I noticed in MySQL logs tail -f /var/log/mysql/error.log that a data recovery was triggered on daemon start due to an anomaly detected during internal log scan: the database was not closed properly. However the recovery could not repair the data and an intentional crash was performed. As a consequence, the container was restarted and so on. This infinite loop prevented mysqld to start and the socket to be used by the client mysql.
1) This configuration of /etc/mysql/conf.d/my.cnf enabled to skip the recovery:
[mysqld]
innodb_force_recovery=4
and to use mysql client with socket to dump important schemas and/or delete corrupted schemas.
Do not forget to remove this line from my.cnf after you're done!
2) Perfoming a mysql upgrade and repair could also have been beneficial:
docker exec -it mysql-container mysql_upgrade -u root -p --force
mysqlcheck -u root -p --auto-repair --check --all-databases
Restarting the Docker container is necessary after this step.
3) Also, deleting MySQL internal logs (that were scanned and triggered the recovery) was necessary:
cd /var/lib/mysql/mysql/
rm ibdata1 ib_logfile0 ib_logfile1
Now I can use MySQL again, from inside and outside the container.

AWS EC2 mysql root password issue

Installed the below in amazon ec2 new instance.
yum install -y httpd24 php56 mysql55-server php56-mysqlnd
Then did a start of mysqld using service mysqld start . It gave a list of commands that needs to be executed.
Followed the instructions in the service output to change the root password. When the below command is run it gives an error
/usr/libexec/mysql55/mysqladmin: connect to server at '177.37.1.30' failed
error: 'Host 'ip-177-37-1-30.ap-southeast-1.compute.internal' is not allowed to connect to this MySQL server'
How do I fix this?
service mysqld start
Initializing MySQL database: Installing MySQL system tables...
161102 4:22:07 [Note] /usr/libexec/mysql55/mysqld (mysqld 5.5.52) starting as process 3137 ...
OK
Filling help tables...
161102 4:22:07 [Note] /usr/libexec/mysql55/mysqld (mysqld 5.5.52) starting as process 3144 ...
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 MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/libexec/mysql55/mysqladmin -u root password 'new-password'
/usr/libexec/mysql55/mysqladmin -u root -h ip-177-37-1-30 password 'new-password'
Alternatively you can run:
/usr/libexec/mysql55/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 manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/libexec/mysql55/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
Give Nat permissions in VPC where instance is running.
thank you.

Docker alpine image : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")

Hi there is a public alpine mysql image for docker.
https://hub.docker.com/r/wangxian/alpine-mysql/
I wanted to run mysql in it.
I am getting this error when I execute the mysql command after docker run command:
docker run -i -t wangxian/alpine-mysql sh
/app # mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")
Well then I installed everything from scratch.
I made a new image of alpine and then I installed these things:
apt add mysql mysql-client
And then I again typed mysql and got same error.
This looks like a common error, it happens on all alpine images because this was a fresh install of alpine.
Any solutions ?
Edit:
When I do docker run -i -t wangxian/alpine-mysql
2016-08-17 12:51:41 140215609339688 [Note] Plugin 'FEEDBACK' is disabled.
2016-08-17 12:51:41 140215609339688 [Note] Server socket created on IP: '::'.
2016-08-17 12:51:41 140215609339688 [Note] /usr/bin/mysqld: ready for connections.
Version: '10.1.11-MariaDB-log' socket: '/run/mysqld/mysqld.sock' port: 3306 MariaDB Server
And then I opened new tab in command line while keeping that tab open, I again get the same error:
docker run -i -t wangxian/alpine-mysql sh
/app # mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")
Thats not really how the image you used works. On the Docker Hub page you can see how it is supposed to be run. It has a startup script which is not called when you pass the "sh" command on run, so mysql is not running.
When you run the image without any commands it starts mysql and sets some initial values for username/password. Then you can enter the running container with docker exec or can link other container to it

MySQL 5.6 - Database is running, but no Socket file

I am able to connect to mysql database and query it. But, I am NOT able to find the socket file.
$ps -ef|grep mysql
mysql 31408 30874 0 18:46 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/mysql/admin/ofile/TEST1.cnf
mysql 31959 31408 0 18:46 pts/1 00:00:01 /usr/sbin/mysqld --defaults- file=/mysql/admin/ofile/TEST1.cnf --basedir=/usr -- datadir=/mysql01data/TEST1/data --plugin-dir=/usr/lib64/mysql/plugin --log- error=/mysql/admin/TEST1/errors/mysqld_safe.err --pid- file=/mysql/admin/TEST1/run/mysqld_safe.pid
Here is my socket file entry in TEST1.cnf:
$ cat /mysql/admin/ofile/TEST1.cnf|grep sock
socket = /mysql/admin/TEST1/run/TEST1.sock
The corresponding directory only contains pid file. There is no socket file.
-sh-4.1$ cd /mysql/admin/TEST1/run
-sh-4.1$ ls -lrt
total 4
-rw-rw---- 1 mysql mysql 6 Apr 29 18:46 mysqld_safe.pid
This is the MySQL 5.6 version I installed through RPM's on RHEL 6.5. I have my old custom scripts which uses socket file to connect to the database.
So, I am wondering how I can use the socket file to connect to the database? Why the socket file is not created by default?
The socket file for a running instance of MySQL Server should be something that can be found with this shell command:
sudo lsof -a -U -p $(pgrep -d, -f /path/to/your/running/mysqld)
One possible cause of being unable to find the socket file would be if it had been deleted after the server was started. In that case the above command should work, and show something like (deleted) after the path.
That was my original assumption on this question... but here the issue was a configuration oversight. The "defaults file," commonly called my.cnf contains multiple sections. The [client] section configures client utilities, like mysql and mysqldump, while the [mysqld] section configures the server daemon. If the socket directive isn't in the appropriate section, the server (and/or client utilities) will look in the location compiled in by default, with /tmp/mysql.sock or /var/lib/mysql/mysql.sock being a couple of examples of common default locations.