SOLVED: Error connecting to MySQL in another container

I have MariaDB running in a container and connected to both the host network and a network called mysql.

I have dockerised Adonis and expose port 3333. I can connect to Adonis via a web browser from another machine, so the server is running just fine.

I have also created a link to my MySQL container and given it a alias of db.

When I try to run ace migration:run I get the following error.

Error: connect ENOENT tcp://172.17.0.3:3306                                     
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)            
    at Protocol._enqueue (/. /app/node_modules/mysql/lib/protocol/Protocol.js:14
4:48)                                                                           
    at Protocol.handshake (/. /app/node_modules/mysql/lib/protocol/Protocol.js:5
1:23)                                                                           
    at Connection.connect (/. /app/node_modules/mysql/lib/Connection.js:116:18) 
    at /. /app/node_modules/knex/lib/dialects/mysql/index.js:95:18              
From previous event:                                                            
    at Client_MySQL.acquireRawConnection (/. /app/node_modules/knex/lib/dialects
/mysql/index.js:90:12)                                                          
    at create (/. /app/node_modules/knex/lib/client.js:280:23)                  
    at /. /app/node_modules/tarn/lib/Pool.js:315:34                             
    at Object.tryPromise (/. /app/node_modules/tarn/lib/utils.js:53:24)         
    at /. /app/node_modules/tarn/lib/Pool.js:315:17                             
    at new Promise (<anonymous>)                                                
    at callbackOrPromise (/. /app/node_modules/tarn/lib/Pool.js:306:12)         
    at Pool._create (/. /app/node_modules/tarn/lib/Pool.js:236:9)               
    at Pool._doCreate (/. /app/node_modules/tarn/lib/Pool.js:208:36)            
    at Pool._tryAcquireOrCreate (/. /app/node_modules/tarn/lib/Pool.js:159:18)  
    at Pool.acquire (/. /app/node_modules/tarn/lib/Pool.js:85:14)               
    at /. /app/node_modules/knex/lib/client.js:335:26                           
From previous event:                                                            
    at Client_MySQL.acquireConnection (/. /app/node_modules/knex/lib/client.js:3
34:34)                                                                          
    at Runner.ensureConnection (/. /app/node_modules/knex/lib/runner.js:228:24) 
    at Runner.run (/. /app/node_modules/knex/lib/runner.js:34:42)               
    at SchemaBuilder.Target.then (/. /app/node_modules/knex/lib/interface.js:20:
43) {                                                                           
  errno: 'ENOENT',                                                              
  code: 'ENOENT',                                                               
  syscall: 'connect',                                                           
  address: 'tcp://172.17.0.3:3306',                                             
  fatal: true                                                                   
}    

I can run ping db and I get back the address of my mysql container

Here is my .env file

HOST=0.0.0.0
PORT=3333
NODE_ENV=development
APP_URL=http://${HOST}:${PORT}
CACHE_VIEWS=false
APP_KEY=dUPYRyffrtJ2KGYWW2Wmm12Tgj1cG9fU
DB_CONNECTION=mysql
DB_HOST=172.17.0.3
DB_PORT=3306
DB_USER=simon
DB_PASSWORD=Pr35t0nTh30
DB_DATABASE=faceshields
SESSION_DRIVER=cookie
HASH_DRIVER=bcrypt

In DB_HOST I have tried 172.17.0.3, db and localhost

I can connect from my Mac across the network using the MySQL client just fine. i.e. This works ok

mysql -h 192.168.10.3 -uroot -p

I’m pulling my hair out, any ideas?

1 Like

After hours of trying I managed to solve it, 10 minutes after writing the post.

I removed the link to the MySQL container from the Adonis Container and I changed the DB_HOST in my .env file to the host public network port 192.168.10.3. I would rather not do that, but hey, it’s got me up and running.

1 Like

You can make both API and MySQL to run on host networking (docker networking is quite slow, it will give you 20-70% performance boost too)
Other than that it seems like problem is more in MySQL config. What is it listening on? Is it set to listen 0.0.0.0?