Skip to content

Commit 4cd7424

Browse files
authored
Ensure passwords can be read from options files (#180)
Fixes #166. Wowza what an issue. After spending way too much time googling various forms of "can't read password from mariadb options file", I started poking around the capi.jl file, and reading anything related to "options". I was trying to see if there was a way to have a debug print of what options were actually read from an options file. That's when I found the following in the docs for `mysql_real_connect`, which I'm now very thankful that past me decided to copy/paste all the api docs for every single api function. ``` For passwd, specify a value of NULL. (For the password, a value of the empty string in the mysql_real_connect() call cannot be overridden in an option file, because the empty string indicates explicitly that the MySQL account must have an empty password.) ``` So if you want a password to be read from an option file, you need to pass NULL to the `mysql_real_connect` function; but we were always passing an empty string! So it prevented reading any password from option files. Anyway, the fix is just that: by default we'll pass `nothing` as the password, which will pass NULL to the actual api call, allowing passwords to be read from option files.
1 parent ce1f9fc commit 4cd7424

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

src/MySQL.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mutable struct Connection <: DBInterface.Connection
2121
db::String
2222
lastexecute::Any
2323

24-
function Connection(host::String, user::String, passwd::String, db::String, port::Integer, unix_socket::String; kw...)
24+
function Connection(host::String, user::String, passwd::Union{String, Nothing}, db::String, port::Integer, unix_socket::String; kw...)
2525
mysql = API.init()
2626
API.setoption(mysql, API.MYSQL_PLUGIN_DIR, API.PLUGIN_DIR)
2727
API.setoption(mysql, API.MYSQL_SET_CHARSET_NAME, "utf8mb4")
@@ -285,7 +285,7 @@ Connect to a MySQL database with provided `host`, `user`, and `passwd` positiona
285285
* `read_default_group::Bool`: only the default option groups are read from specified option file(s)
286286
* `option_group::String`: it is interpreted as a custom option group, and that custom option group is read in addition to the default option groups.
287287
"""
288-
DBInterface.connect(::Type{Connection}, host::String, user::String, passwd::String; db::String="", port::Integer=3306, unix_socket::String=API.MYSQL_DEFAULT_SOCKET, kw...) =
288+
DBInterface.connect(::Type{Connection}, host::String, user::String, passwd::Union{String, Nothing}=nothing; db::String="", port::Integer=3306, unix_socket::String=API.MYSQL_DEFAULT_SOCKET, kw...) =
289289
Connection(host, user, passwd, db, port, unix_socket; kw...)
290290

291291
"""

src/api/capi.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1129,8 +1129,8 @@ If no value is found in an option file for a parameter, its default value is use
11291129
Return Values
11301130
A MYSQL* connection handler if the connection was successful, NULL if the connection was unsuccessful. For a successful connection, the return value is the same as the value of the first parameter.
11311131
"""=#
1132-
function connect(mysql::MYSQL, host::String, user::String, passwd::String, db::String, port::Integer, unix_socket::String, client_flag)
1133-
@checknull mysql mysql_real_connect(mysql.ptr, host, user, passwd, db, port, unix_socket, client_flag)
1132+
function connect(mysql::MYSQL, host::String, user::String, passwd::Union{String, Nothing}, db::String, port::Integer, unix_socket::String, client_flag)
1133+
@checknull mysql mysql_real_connect(mysql.ptr, host, user, passwd === nothing ? Ptr{UInt8}(C_NULL) : passwd, db, port, unix_socket, client_flag)
11341134
return mysql
11351135
end
11361136

test/my.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ host=127.0.0.1
33
user=root
44
port=3306
55
connect_timeout=30
6-
report-data-truncation=true
6+
report-data-truncation=true
7+
password = ""

test/runtests.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ conn = DBInterface.connect(MySQL.Connection, "127.0.0.1", "root", ""; port=3306)
44
DBInterface.close!(conn)
55

66
# https://github.com/JuliaDatabases/MySQL.jl/issues/170
7-
conn = DBInterface.connect(MySQL.Connection, "mysql://127.0.0.1", "root", ""; port=3306)
7+
conn = DBInterface.connect(MySQL.Connection, "mysql://127.0.0.1", "root"; port=3306)
88
DBInterface.close!(conn)
99

1010
# load host/user + options from file
11-
conn = DBInterface.connect(MySQL.Connection, "", "", ""; option_file="my.ini")
11+
conn = DBInterface.connect(MySQL.Connection, "", ""; option_file=joinpath(dirname(pathof(MySQL)), "../test/", "my.ini"))
1212
@test isopen(conn)
1313

1414
DBInterface.execute(conn, "DROP DATABASE if exists mysqltest")

0 commit comments

Comments
 (0)