Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to container based db instance #223

Closed
psteinb opened this issue Mar 10, 2025 · 1 comment
Closed

Unable to connect to container based db instance #223

psteinb opened this issue Mar 10, 2025 · 1 comment

Comments

@psteinb
Copy link

psteinb commented Mar 10, 2025

Hi,

thanks for providing access to mysql for julia hackers. I am a bit of a newbie and I am struggling to connect to a local mariadb instance. My julia code is pretty simple:

using MySQL, DBInterface, DataFrames

@testset "MySQL conenction" begin

    host = "localhost"
    user = "root"
    @test isfile("mysql.root_pass")

    password = read("mysql.root_pass", String)
    port = 3306
    dbname = "juliatests"
    usocket = "/foo/bar/julia_sandbox/database_storage/mariadb/run_mysqld/mysqld.sock"

    conn = DBInterface.connect(MySQL.Connection,
                               host,
                               user,
                               password;
                               db=dbname,
                               unix_socket=usocket)

    DBInterface.close!(conn)

    end;

But I always get:

MySQL conenction: Error During Test at /foo/bar/julia_sandbox/database_storage/mysql/test/test_float_storage.jl:3
  Got exception outside of a @test
  (1045): Access denied for user 'root'@'localhost' (using password: YES)
  Stacktrace:
    [1] connect
      @ ~/.julia/packages/MySQL/9WCDb/src/api/capi.jl:12 [inlined]
    [2] MySQL.Connection(host::String, user::String, passwd::String, db::String, port::Int64, unix_socket::String; kw::@Kwargs{})

Even though I am unable to connect to the DB alright from a local shell.

Here is what I do without root priviledges:

  1. singularity pull mariadb.simg docker://mariadb:10
  2. mkdir -p ./mariadb_ ./run_mysqld
  3. create database instance
singularity run \
--env MARIADB_DATABASE=juliatests \
--env MARIADB_ROOT_PASSWORD=`cat run_mysqld/mysql.root_pass` \
-B./mariadb_:/var/lib/mysql \
-B./run_mysqld:/run/mysqld \                              
./mariadb.simg 
  1. different shell, test setup
singularity exec ./mariadb.simg mariadb -uroot -p`cat run_mysqld/mysql.root_pass`  -e "CREATE DATABASE juliatests;" --socket=run_mysqld/mysqld.sock
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
  1. create database
singularity exec ./mariadb.simg mariadb -uroot -p`cat run_mysqld/mysql.root_pass`  -e "CREATE DATABASE juliatests;" --socket=run_mysqld/mysqld.sock
  1. create table
singularity exec ./mariadb.simg mariadb -uroot -p`cat run_mysqld/mysql.root_pass`  -e "CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), age INT);" --socket=run_mysqld/mysqld.sock
  1. singularity exec ./mariadb.simg mariadb -uroot -p\cat run_mysqld/mysql.root_pass` -e "SHOW DATABASES;" --socket=run_mysqld/mysqld.sock`
+--------------------+
| Database           |
+--------------------+
| information_schema |
| juliatests         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
  1. insert payload
singularity exec ./mariadb.simg mariadb -uroot -p`cat run_mysqld/mysql.root_pass` -D juliatests -e "INSERT INTO users (name, age) VALUES ('Alice', 25), ('Bob', 30);" --socket=run_mysqld/mysqld.sock
  1. check payload
singularity exec ./mariadb.simg mariadb -uroot -p`cat run_mysqld/mysql.root_pass` -D juliatests -e "SELECT * FROM users;" --socket=run_mysqld/mysqld.sock
+----|-------|------+
| id | name  | age  |
+----|-------|------+
|  1 | Alice |   25 |
|  2 | Bob   |   30 |
+----|-------|------+
@psteinb
Copy link
Author

psteinb commented Mar 14, 2025

It was as simple as to add rstrip(read("mysql.root_pass", String)). ;-)

@psteinb psteinb closed this as completed Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant