P
P
proger_102018-03-04 22:25:04
MySQL
proger_10, 2018-03-04 22:25:04

How to connect to a database on another server?

Hello!
Guys, I use raspberry pi, it collects data from the sensor from me.
There is also an ubuntu on which the site is configured (locally) and there is a database.
I want to send data from raspberry to the ubuntu database, I do it all in python.
But first I want to at least connect to the database by installing:
sudo apt-get install python-mysqldb
I do this:

#!/usr/bin/python
import MySQLdb
# подключаемся к базе данных (не забываем указать кодировку, а то в базу запишутся иероглифы)
db = MySQLdb.connect(host="192.168.1.8", user="root", passwd="root", db="contacts", charset='utf8')
# формируем курсор, с помощью которого можно исполнять SQL-запросы
cursor = db.cursor()
# закрываем соединение с базой данных
db.close()

In place of the hota, I write the address of ubuntu, the password and login are correct.
He writes to me:
Traceback (most recent call last):
File "connect.py", line 4, in
db = MySQLdb.connect(host="192.168.1.8", user="root", passwd="root", db ="contacts", charset="utf8")
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File " /usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can 't connect to MySQL server on '192.168.1.8' (111)")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kekoz, 2018-03-05
@kekoz

And on remote base it is allowed to connect from other hosts?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question