From f52aff4122fb73b65ea20bf57e0ee58d95e1e55e Mon Sep 17 00:00:00 2001 From: Marco Mambelli Date: Wed, 31 Jul 2024 18:17:07 -0500 Subject: [PATCH] Using Apptainer to run the MySQL server and client in the mysql image from Docker Hub --- _episodes/04-mysql-and-python.md | 3 +++ setup.md | 33 +++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/_episodes/04-mysql-and-python.md b/_episodes/04-mysql-and-python.md index 67de681..1279ecd 100644 --- a/_episodes/04-mysql-and-python.md +++ b/_episodes/04-mysql-and-python.md @@ -88,11 +88,14 @@ We need the following URL components: * Database Name: Name of the specific database to connect to. So, the URL structure is : `Dialect+driver://username:password@hostname:port/databaseName` +or, if you use Apptainer and a socket file is: `Dialect+driver://username:password@localhost/databaseName?unix_socket=filePath` And we create a engine using this db_url. ```python # Define the MySQL database connection URL db_url = "mysql+pymysql://root:mypassword@localhost:3306/metadata2" +# if using Apptainer uncomment the next line to define the URL using the socket file: +# db_url = "mysql+pymysql://root:mypassword@localhost/metadata2?unix_socket=/var/run/mysqld/mysql.sock" # Create an SQLAlchemy engine engine = create_engine(db_url) diff --git a/setup.md b/setup.md index 765a8f1..ea51d62 100644 --- a/setup.md +++ b/setup.md @@ -55,20 +55,35 @@ executing the following command: apptainer --version ``` -We will use the same image as in Option 1. Execute the following commands to run the MySQL server in an -Apptainer instance: +We will use the same image as in Option 1. +Appteiner images are readonly if overlayfs is not available, so we'll be mounting the socket and database directories. This allows also for some persistency. +Depending on the system settings unprivileged Apptainer may not be allowed to bind to ports, so we'll use a socket file to connect to the server. +First move to a local directory: the image is not small and disk access will be faster, e.g. `mkdir /scratch//mysql-apptainer; cd /scratch//mysql-apptainer`. +Then execute the following commands to run the MySQL server in an Apptainer instance (replace mypassword with your password): ```bash -apptainer instance start docker://mysql:latest myfirst-sqlserver --net --network-args "portmap=3306:3306/tcp" \ ---env="MYSQL_ROOT_PASSWORD=mypassword" +echo "SET PASSWORD FOR 'root'@'localhost' = 'mypassword';" > .mysqlrootpw +mkdir -p ./mysql/var/lib/mysql/ ./mysql/run/mysqld +apptainer pull --name mysql.sif docker://mysql +apptainer instance start --bind ${PWD} --bind ${PWD}/mysql/var/lib/mysql/:/var/lib/mysql --bind ${PWD}/mysql/run/mysqld:/run/mysqld ./mysql.sif mysql +apptainer instance list # just to make sure the instance started +apptainer exec instance://mysql mysqld --init-file=${PWD}/.mysqlrootpw & ``` - -> ## TODO: -> It is not working out of the box. Need to figure out why. -{: .caution} +If you don't run the last command in background the terminal will be used by the server console and you'll have to use another terminal for other commands. To test that if everything is up and running, execute the following command: ```bash -apptainer instance exec myfirst-sqlserver bash -c "mysql -uroot -pmypassword" +apptainer exec instance://mysql mysql -S /var/run/mysqld/mysql.sock -u root -pmypassword +``` +Remember to use it also throughout the tutorial instead of the docker command. + +You may want to use a different password and a safer way to run mysql id to avoid to put the password in the command line, e.g. save in mysqlclient.ini the following: +``` +[client] +password="mypassword" +``` +And then run with (--defaults-extra-file must be the first option): +```bash +apptainer exec instance://mysql mysql --defaults-extra-file=myconf -S /var/run/mysqld/mysql.sock -u root ``` If you are interested on learning more about Apptainer, take a look at the