So I just set-up an ubuntu server on amazon and installed mysql. It is working with:
mysql -u root -p
And then the mysql_password. I have added via command line a database and tables. But now I wonder how I can access this database with python locally (and eventually remotely).
If I try locally something like:
db = MySQLdb.connect(host="localhost", user="root", passwd="mysql_password", db="dbname")
I get the following error:
_mysql_exceptions.OperationalError: (1049, "Unknown database 'dbname'")
However, the database dbname does exist at /var/lib/mysql. So how to access this via python (first locally and eventually remotely)?
Solved it, it was a spelling mistake in dbname
Related
Good day,
I have a Synology NAS DS120J hosting a MariaDB 10 (10.3.32-1040) database, on the same local network, I have a Linux machine running Ubuntu 22.04.
I am trying to connect the Linux machine to the MariaDB database using Python 3.10 and the MariaDB connector. However, it seems that I am missing a step.
When I access this database using the command line, I SSH from the Linux machine to the NAS, mentioning the port where the database is located on the NAS.
Then I populate my credentials to login to the database, this works fine.
But when using Python 3.10 and the MariaDB connector, I only provide with my credentials to login to the database, so I feel like I am missing the SSH step to the NAS, at least this is my assumption.
How could I achieve that ? Can someone please help ?
Here is my script in Python:
import mariadb
import json
import sys
with open('config.json') as config_file:
config = json.load(config_file)['mariadb']
try:
conn = mariadb.connect(
user=config['raspi-svr']['user'],
password=config['raspi-svr']['password'],
host=config['raspi-svr']['host'],
port=config['raspi-svr']['port'],
database="test_db"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
cur = conn.cursor()
Here is the error message I am getting when running my script:
Error connecting to MariaDB Platform: Lost connection to server at 'handshake: reading initial communication packet', system error: 0
I tried running the MariaDB connector for Python.
I did a python -m pip install mysql-connector and able to successfully run import mysql.connector through python. But when I am trying to run the below code.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
print(mydb)
It is failing with InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)
Since I did pip install for mysql.connector I am not sure of user and passwd.
I connected my database instance from Amazon RDS to mySql workbench, created a python file that looks like his and got the exact error. Everyone says you could have a system firewall problem but nothing straight forward.
The connector is just a means of communicating with a mysql database programically.
You need this, or access to a mysql server to use the connector.
https://dev.mysql.com/downloads/mysql/
The host is obviously localhost when you want to access it locally on your machine. This only works if the server is running on your machine. You can connect a remote server by changing the host to a valid IP address and providing valid credentials. If you use it remotely make sure the server has access through the firewall and that you properly forward the TCP port you decide to use. You may not have to forward, but I would as a general rule of thumb to make it one less thing to check when troubleshooting.
Good luck
I´m trying to connect to a mysql database installed on a virtual machine with Ubuntu Server. I´m very new to server administration so I followed this tutorial LAMP on it. I also followed this tutorial to install phpMyAdmin. Apparently, everything is working fine. I can access to phpMyAdmin from the web browser on my host machine.
The network of the virtual machine is configured as bridge adapter.
The problem is that I cannot connect when using python (also from host machine). This is the code I'm using:
import mysql.connector
cnx = mysql.connector.connect(user='root', password='XXXXX',
host='192.168.1.138',
port=3306,
database='mysql')
cnx.close()
The code returns this error:
DatabaseError: 1130: Host 'desktop-p7v30jj.home' is not allowed to connect to this MySQL server
I looked for information and found that it could be due to the bind-address. So I edited the file /etc/mysql/mysql.cnf and included the following two lines at the end with no success:
[mysqld]
bind-address = 0.0.0.0
What else should I try? Any idea?
Thanks in advance!
I managed to find the solution by simply creating another user, as stated in this topic:
Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
Sorry for answering.
Regards.
I'm using Python 3.5 + Spyder 2 (from Anaconda) on Windows 10. I have an Ubuntu 16.04 desktop machine which is running a MySQL server on a LAN addressable IP. PhpMyAdmin works fine with this remote server. However, every time I attempt to connect to this server from my Windows 10 Spyder I get the following error:
OperationalError: (1045, "Access denied for user 'root'#'10.0.0.30' (using password: YES)")
The command I'm using is:
import os
from peewee import *
from playhouse.db_url import connect
db = MySQLDatabase('test', host="10.0.0.80", port=3306, user='root', passwd='*********')
db.connect()
The IP of the machine I'm using to make the call is 10.0.0.30 shown in the Error. I originally tried it before the "test" db existed. Then I created the "test" db using PhpMyAdmin. I then tested again. Same error. I have created a table in the "test" db via PhpMyAdmin to confirm that root has the appropriate privileges (which wasn't really a question but I wanted confirm).
I have downloaded and installed what I could find via several StackOverflow questions prior to posting this question. I have also rebooted to make sure that any new drivers and such that I installed were actually up and running.
Note my issue is not the same as this one:
Peewee - Can't connect to MySQL server on host
My connection error shows that despite using the host argument and setting it to "10.0.0.80" that peewee is still trying to connect to 10.0.0.30. IP 10.0.0.30 is the Windows machine I'm running peewee from while 10.0.0.80 is the machine I'm attempting to connect to.
TIA
I'm running MySQL 5.1 under Windows 7. If I start the MySQL Command Line Client and type:
show databases;
it returns:
information_schema
aircraft_taxiing
dvd_collection
eqndb
mydb
mysql
test
test_db
all of which are in the directory in my.ini:
datadir="C:/ProgramData/MySQL/MySQL Server 5.1/Data/"
If I open a command prompt window and type mysql it only returns:
information_schema
test
test_db.
What happened to the rest of the databases? I've been trying to connect Python to MySQL using MySQLdb and can open any of the three databases, but none of the missing ones.
My goal is to make the Python connection in the end, but I'd like to understand what's going on at the command prompt, too.
It depends on the user you are logging in as. If your user doesn't have any priviledges on those databases, you won't be able to see them.