smithjames0
Posts: 38
Joined: Tue Oct 04, 2016 10:04 pm

mariadb connection

Mon Aug 13, 2018 4:51 pm

Hi,

I have a problem with my script starting on boot up. If I start my script from the command prompt it works fine, if I start it from a cron job on boot I get a mysql connection error.

Traceback (most recent call last):
File "/home/pi/CoopControl/server.py", line 53, in <module>
mariadb_connection = mariadb.connect(host="localhost",user='root', password='some password', database='Chicken')
File "/usr/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 94, in __init__
self.connect(**kwargs)
File "/usr/lib/python2.7/dist-packages/mysql/connector/abstracts.py", line 722, in connect
self._open_connection()
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 207, in _open_connection
self._socket.open_connection()
File "/usr/lib/python2.7/dist-packages/mysql/connector/network.py", line 512, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)

I have changed localhost to 127.0.0.1 and I get the same error, in my script I haven't specified a port number so I am not sure where 3306 has come from. Also why would my connection be refused only when the scripts starting on boot?

Can anyone help??

Regards

DirkS
Posts: 8841
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: mariadb connection

Mon Aug 13, 2018 5:03 pm

smithjames0 wrote:
Mon Aug 13, 2018 4:51 pm
I have changed localhost to 127.0.0.1 and I get the same error, in my script I haven't specified a port number so I am not sure where 3306 has come from. Also why would my connection be refused only when the scripts starting on boot?
Port 3306 is the default for Mysql / mariadb.

Most likely cause is that the DB engine is not yet ready when your script starts.
You could start with a pause / sleep to delay startup of your script

If your script runs in the background you could also make it a systemd service. It is then easy to wait until the DB engine is up and running.

User avatar
DougieLawson
Posts: 33608
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: mariadb connection

Tue Aug 14, 2018 8:57 am

As Dirk says error 111 has three possible causes.

1. Wrong IP, port may be right or wrong
2. Right IP, wrong port
3. Right IP, right port server not listening

You problem is clearly #3.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

smithjames0
Posts: 38
Joined: Tue Oct 04, 2016 10:04 pm

Re: mariadb connection

Wed Aug 15, 2018 5:52 am

Thanks for your help, I used After=mysql.service


Regards

James

Return to “Beginners”

Who is online

Users browsing this forum: Bing [Bot], douwedabbert and 29 guests