# Problem with MySQL

**URL:** https://discourse.julialang.org/t/problem-with-mysql/29313
**Category:** New to Julia
**Tags:** mysql
**Created:** [September 30, 2019, 9:04am UTC](https://discourse.julialang.org/t/problem-with-mysql/29313 "2019-09-30T09:04:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Andrea\_Pagnani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_pagnani/32/3136_2.png) [@Andrea\_Pagnani](https://discourse.julialang.org/u/Andrea_Pagnani)
#### Post date: [September 30, 2019, 9:04am UTC](https://discourse.julialang.org/t/problem-with-mysql/29313/1 "2019-09-30T09:04:29Z")

</div>

Dearests,

I have a `connect` problem with `MySQL.jl`. I’m writing a module that do some custom queries to a local mysql database. It looks like:

```julia
module PfamDatabaseTools

global connection = MySQL.connect("127.0.0.1","myname","mypasswd")

include("file1.jl")
...
include("fileN.jl")

println(connection)
end #end module

```

Now I activate the package (`]activate .` ) and after precompilation I see correctly printed on screen  
a regular connection

* * *

Host: 127.0.0.1  
Port: 3306  
User: myname  
DB:

Now the problem is that if I now try to see what happens to my connection I get a

```julia
julia> PfamDatabaseTools.connection
Null MySQL Connection

```

I tryed to change `global` into `const`, but same result. Note that if I do the same outside the module by squeezing all `file*jl` into a single file, and opening the connection at the beginning of the file, everything works perfectly.

Shooting in the dark, I also tried to `GC.@preserve` the connection variable, with no success either. It’s like if at the end of the module precompilation, the connection disappears.

Any ideas?

---

<div class="post-metadata">

### Author: ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)
#### Post date: [September 30, 2019, 9:59am UTC](https://discourse.julialang.org/t/problem-with-mysql/29313/2 "2019-09-30T09:59:21Z")

</div>

Precompilation is done only once, if you want to connect to MySQL every time module is loaded, try putting the connection code into the special ` __init__ ()` method. Even better approach would be to create connection only when you need it to be able to handle disconnections and similar things.

---

<div class="post-metadata">

### Author: ![Andrea\_Pagnani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_pagnani/32/3136_2.png) [@Andrea\_Pagnani](https://discourse.julialang.org/u/Andrea_Pagnani)
#### Post date: [September 30, 2019, 11:17am UTC](https://discourse.julialang.org/t/problem-with-mysql/29313/3 "2019-09-30T11:17:04Z")

</div>

Thank @dfdx ! I added in the main module file a

```julia
function __init__ ()
    global connection = MySQL.connect("127.0.0.1","myname","mypasswd")
    nothing
end 

```

And everything works like a charm. I see that this solution is brittle as it is not able to deal with random disconnection. I’m thinking about your “even better approach”. Marking as solved!
