Connect to MySQL running on raspberry pi in my own laptop - python

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.

Related

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

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

Python psycopg2, NO postgresql etc service running on mashine... Where is the postgress driver? where is pg_hba.conf? (FATAL: no pg_hba.conf entry)

I am using -in Linux- python 3 and psycopg2 to connect to some postres databases:
import psycopg2 as pg
connection = None
try:
connection = pg.connect(
user = "username",
password = "...",
host = "host_ip",
port = "5432",
database = "db_name",
)
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print ( connection.get_dsn_parameters(),"\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
except (Exception, pg.Error) as error :
print ("Error while connecting to PostgreSQL", error)
For one of the DBs this works, but for the other I am getting:
Error while connecting to PostgreSQL FATAL: no pg_hba.conf entry for host "XXX.XXX.XXX.XXX", user "YYYY", database "ZZZZ", SSL off
I have checked the web and stackoverflow, and there are a lot of similar questions,
e.g. Psycopg2 reporting pg_hba.conf error
However, I am not root on the machine where I used pip/anaconda, and there seems to be no
sql service or anything similar running:
$ sudo systemctl status postgres*
$ sudo systemctl status postgres
Unit postgres.service could not be found.
$ sudo systemctl status postgresql
Unit postgresql.service could not be found.
$ sudo systemctl status post*
So none of the answers seem to be relevant, because this question seems to be based on the
postgress service running, or on the existence of pg_hba.conf, either of which do not in my system. Though note that a sample is included in my envs/py3/share/ (where py3 the name of my environment):
$ locate pg_hba.conf
/home/nick/anaconda3/envs/py3/share/pg_hba.conf.sample
My question here aims to -apart to find a way to solve my immediate problem- understand what psycopg2 is / how it ends up using pg_hba.conf, seemingly used in a postgresql service that does not seem to exist in my system:
Does psycopg2 is/uses a driver? Why does it seem to include pg_hba.conf.sample and what is one supposed to do with it? where to place pg_hba.conf(???) it to make psycopg2 read it?
Notes / Info based on comments:
The DB is not locally hosted. It is running on a different server.
I am able to access that DB using DBeaver and my local Ubuntu python, but a container (same psycopg2 version is not), so I speculate it is not a DB server issue.
It seems pg_hba.conf is a file that should only be on the server? (If so, that actually is part of the answer I am looking for...)

Cannot connect to MySQL with MySQLdb in Python, can connect from command line

I'm having trouble connecting to my local MySQL database in python, but have no problem connecting from the command line.
When I try this:
db = MySQLdb.connect(host="localhost",
user="username",
passwd="password",
db="dbname",
port=3306,
unix_socket="/var/run/mysqld/mysqld.sock")
I get the following error:
OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
I get the same error when I don't explicitly pass in the socket path. I get a similar error when I use 127.0.0.1 instead of localhost as the host (also removing the unix socket argument):
OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
I know that MySQL is running, as when I can connect to the database from the command line no problem with mysql -u username -p dbname. I also know MySQL is listening on 127.0.0.1, when I run mysql -h 127.0.0.1 -p I can connect as well. I'm also sure it's listening on port 3306.

Connecting to Amazon rds with Python

Using MySQL Workbench, I can connect mysql running on amazon rds. I supply SSH Hostname, SSH Username, SSH Password, SSH Key File, MySQL Hostname, MySQL Server Port, Username, and Password.
However, I have a python script with which I'd like to connect to this same database. I tried using ssh tunneling:
with SSHTunnelForwarder(
(SSH Hostname, 22),
ssh_password=SSH Password,
ssh_username=SSH Username,
ssh_pkey= SSH Key File path,
remote_bind_address=(MySQL Hostname, MySQL Port) as server:
conn = MySQLdb.connect(
host = MySQL Hostname,
port = MySQL Port,
user = Username,
passwd = Password)
When I run the script, it hangs for about a minute, then says
'Can't connect to MySQL server on ~MySQL Hostname~'
I am unsure about the remote_bind_address. Since I don't supply that info when successfully connecting from MySQLWorkbench, is there a way to connect from Python without specifying remote_bind_address?
Additionally, I tried using the scripting tool in MySQL Workbench. I am able to load my python script, but then an error occurs at one of my imports, specifically 'import module requests'. The error indicates there is no module named requests. However, this module has been installed for a while, and I have imported and successfully used it outside of MySQL Workbench.

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