Connect MariaDB on raspberrypi from remote over same network [duplicate] - python

This question already has answers here:
MySQL: How to allow remote connection to mysql
(19 answers)
Closed 4 months ago.
I have installed mariaDb (Mysql) on my raspberry pi .I am trying to connect my db using python from another machine over the same network but I receive the below error .
self.sock.connect(sockaddr)
ConnectionRefusedError: [Errno 61] Connection refused
The above exception was the direct cause of the following exception:
Both my machine and raspberry pi are over the same network . I can connect when I run the code on raspberry pi using localhost but running the same code on another machine gives above error .
import mysql.connector
if __name__ == '__main__':
db = mysql.connector.connect(host='192.168.0.108',user='admin123',password='abcd',database='cdr_mapping')
print(db)
Is there any config for mariadb which I need to set ?

Please open the config-file of mariadb under:
/etc/mysql/my.cnf
Change the bind-address from 127.0.0.1 to 0.0.0.0
bind-address = 0.0.0.0
And restart mariadb.
On my second DB the file looks like this:
[client-server]
# Port or socket location where to connect
#port = 3306
socket = /run/mysqld/mysqld.sock
# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
I think I used following blog to solve the issue:
https://webdock.io/en/docs/how-guides/database-guides/how-enable-remote-access-your-mariadbmysql-database
That worked for me :D

Related

Can not connect to Postgres from Python [duplicate]

This question already has answers here:
Connecting to Postgresql in a docker container from outside
(17 answers)
Closed 1 year ago.
I created a docker image with Postgres via Dockerfile:
FROM postgres:9.6-alpine
After I started this docker container, I'm checking that it's up and running using a connection from the different docker container that has pre-installed psql:
docker run -it --rm --link ml-postgres:postgres postgres:12.2-alpine
psql --dbname mlpython -h postgres -U postgres
The result is that I'm able to connect to the first container with postgres and perform all regular operations with the postgres DB.
Troubles begin when I want to connect to the container with postgres DB from a Python script that I created locally:
import psycopg2
conn = psycopg2.connect(
host="127.0.0.1",
database="mlpython",
user="postgres",
password="test",
port="5431"
)
cur = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM mytable LIMIT 10')
cur.close()
Here is an error which I get:
> psycopg2.OperationalError: server closed the connection unexpectedly
> This probably means the server terminated abnormally before or while
> processing the request.
What do I miss while trying to bootstrap this simple code sample where Python interacts with Postgres?
Please read through the README.md of a docker image you use. It should answer your questions.
I'm not sure, if you did, because:
I see, you started psql - the client. Why, if you're going to connect from python? And have you started the server?
I can't see if you exposed any container port to a host machine

sqlalchemy.exc.OperationalError : Can't connect to mysql in docker

I am working with sqlalchemy and mysql, the process is working fine for mysql installed locally but I am not able to connect it with a mysql docker image. I am using pymysql as a driver. Here is the line of commands that I run and I am getting an error shown below.
Following are the portions of /docker-compose.yml and the python file. Also I have a script that creates a database named "sqlalchemy" in docker mysql which is not shown below.
/docker-compose.yml
services:
db:
build: ./database
restart: always
ports:
- "3309:3306"
environment:
- MYSQL_USER=root
- MYSQL_ROOT_PASSWORD=password
- MYSQL_HOST=db
/sqlalchemy.py
msqldb_uri = 'mysql+pymysql://root:password#db:3309/sqlalchemy'
engine = create_engine(msqldb_uri)
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")
If the script is running inside container then you do not use the publish port in the connection but you should use 3306 within localhost or same network. The publish port is for outer world.
msqldb_uri = 'mysql+pymysql://root:password#localhost:3306/sqlalchemy'
engine = create_engine(msqldb_uri)
If the script is runnin in other container that is in the same docker-compose file then you also you do not need the publish port.
msqldb_uri = 'mysql+pymysql://root:password#db:3306/sqlalchemy'
engine = create_engine(msqldb_uri)
if the script in running on host and DB is running inside container then you need connect with the publish port but you should not use container name as a IP.
msqldb_uri = 'mysql+pymysql://root:password#localhost:3309/sqlalchemy'
engine = create_engine(msqldb_uri)

Connecting to PostgreSQL database through Python code

I have a Ubuntu virtual machine using Vagrant, I set it up and run it using vagrant up and vagrant ssh and I create the database psql -d dbname -f somesqlqueries.sql.
Whenever I try to connect to this database through python by doing conn = psycopg2.connect("dbname=dbname") I get this error:
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
Looking at solutions for this issue I changed postgresql.conf to have listen_address = '*' and still the same error occurs.
Also tried to turn off the firewall and run sudo ufw allow 5432/tcp and the error still persistent.

Connect to MySQL running on raspberry pi in my own laptop

I have been having problems connecting to the database. I have tested the username and password. It works fine and can login to the database. The database has been running.
This is the code for python in my own laptop not the virtual machine.
import mysql.connector
try:
connection = mysql.connector.connect(
user='myname', password='mypw', host='169.254.X.X', database='GarageParking')
print('success')
except mysql.connector.Error as e:
print("Database not successful")
The host is the ip addr of the raspberrypi. I had edited the mysql config file to 0.0.0.0. I just have problems connecting to the database only not the login.
The error is
2003: Can't connect to MySQL server on '169.254.X.X:3306' (10061 No connection could be made because the target machine actively refused it)
phpMyadmin
I appreciate your help.
I have solved alr. For those who wants to access the mysql running on raspberry pi,
import mysql.connector
try:
connection = mysql.connector.connect(
user='x', password='x', host='169.254.X.X', database='xx',
port=3306) // The host is the ip addr of the raspberry pi which also runs mysql.
print('success')
except mysql.connector.Error as e:
print(e)
Remember to comment out the bind-addr in the my.cnf.
The command is
sudo nano /etc/mysql/my.cnf
Please check if there are anything u have added accidentally in the my.cnf file and remove them appropriately.
After you have modified, restart your server
sudo service mysql restart
Please create a user which can be from any host. In this case, it is a #. Please specify choose any host in the phpmyadmin web.

Connection Error while connecting to PostgreSQL as postgres user?

I am not able connect to PostgreSQL remotely using python and psycopg2:
Here is my code.
>>> import psycopg2
>>> conn_string = "host='localhost' dbname='mydb' user='postgres'"
>>> print "Connecting to database\n ->%s" % (conn_string)
Connecting to database
->host='localhost' dbname='mydb' user='postgres'
>>> conn = psycopg2.connect(conn_string)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tools/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
The password is not set for postgres user.
Generally, I can connect to database by running following method on host.
1. SSH to box
2. su - postgres
3. psql
4. \c mydb
The server runs PostgreSQL 9.1.
You're trying to connect to PostgreSQL on localhost using a script running on your computer, but there's no PostgreSQL server running there.
For this to work, you'd have to ssh to the remote server, then run your Python script there, where the PostgreSQL server is "local" relative to the Python script.
(That's why running psql works - because you're running it on the remote server, where PostgreSQL is "local" relative to psql).
Alternately, you could:
Use an SSH tunnel to forward the PostgreSQL port from the local computer to the remote one; or
Connect directly over TCP/IP to the remote PostgreSQL server using its host name or IP address, after enabling remote connections on the server.
Note that just putting the server's IP address or host name into the connection string instead of localhost will not work unless you also configure the server to accept remote connections. You must set listen_addresses to listen for non-local connections, add any required firewall rules, set pg_hba.conf to permit connections from remote machines, and preferably set up SSL. All this is covered in the Client Authentication chapter of the PostgreSQL user manual.
You'll probably find an SSH tunnel simpler and easier to understand.

Categories