MySQLdb: how to connect with phpMyAdmin - python

I have a LAMP server and then I installed MySQLdb for my Python scripts. Now I can't access the MySQL (from LAMP) from Python scripts because it isn't connecting to the MySQLdb, and also I can't access the MySQLdb with phpMyAdmin with (root root). I got "#2002 Cannot log in to the MySQL server" error. Is it possible to connect to one db with Python and phpMyAdmin?
Here is my Python code, which can't connect to the LAMP MySQL, but can connect to the MySQLdb:
db = MySQLdb.connect(host="localhost", port=3303, user="root", passwd="rootroot", db="test")
cursor = db.cursor()
sql = "CREATE TABLE TT(ID int NOT NULL AUTO_INCREMENT PRIMARY KEY)"
cursor.execute(sql)
db.commit()
db.close()

If you're getting #2002 Cannot log in to the MySQL server when logging in to phpmyadmin, then edit phpmyadmin/config.inc.php file and change:
$cfg['Servers'][$i]['host'] = 'localhost';
to:
$cfg['Servers'][$i]['host'] = '127.0.0.1';
You might want to visit this link:
http://blog.ryantremaine.com/2011/03/2002-cannot-log-in-to-mysql-server.html
UPDATE:
you wud not have configured your php.ini well and so that it cannot connect to mysql server.
Wrong path for the mysql.sock
mysql.sock is the instance of the mysql, so first you have to find where does it place at.
You may find it at "/tmp/mysql.sock" or "/var/mysql/mysql.sock".
Go to your php.ini and make sure the value for "pdo_mysql.default_socket", "mysql.default_socket", "mysqli.default_socket" is the right path.
Then restart your web server and try again.
ELSE
Try this:
Go to config.inc.php and check for the following line:
$cfg['Servers'][$i]['user'] = 'YOUR USER NAME IS HERE';
$cfg['Servers'][$i]['password'] = 'AND YOU PASSWORD IS HERE';
Check whether the user name and password that you gave is present or not

Related

Python securely connect to MariaDB database

I am trying to read the database of my MariaDB server. I have set it up like this:
CREATE DATABASE database1;
CREATE USER RaspberryPi#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database1.* TO RaspberryPi#'%';
SELECT database1;
CREATE TABLE Users;
This is on my Raspberry Pi 4 with the IP: 192.168.0.92. Now I have This Python script on my Windows computer:
import mysql.connector
mydb = mysql.connector.connect(host="192.168.0.92", user="RaspberryPi", passwd="password", database="database1")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM Users")
print(mycursor)
Now I convert my Python script to an .exe file using pyinstaller. My problem is, that if I give this file to some other people, he can easily convert the .exe file to his original .py file and then he has my login credentials. Can I code it somehow, that the username and password isn't shown or the script can't be converted back?
Thanks.
you can use this snipet:
Module Imports
import mariadb
import sys
# Connect to MariaDB Platform
try:
conn = mariadb.connect(
user="db_user",
password="db_user_passwd",
host="192.0.2.1",
port=3306,
database="employees"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
# Get Cursor
cur = conn.cursor()
for more reference use this - https://mariadb.com/resources/blog/how-to-connect-python-programs-to-mariadb/
Update:
in order to make it more secure you can put the username and password with get input

Cannot connect to MySQL in Python program

I am trying to establish a connection with a MySQL database in a python program
import mysql.connector
cnx = mysql.connector.connect(
host='localhost',
user='admin',
password='admin1234',
database='meu_banco'
)
cnx.close()
print(cnx)
but keep getting the following error:
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'admin'#'localhost' (using password: YES)
Im am pretty sure that the username is admin and that I've setted 'admin1234' as the password. Anyone knows what might be happening?
Notes:
-I've also tried to use 'passwd' and 'db' instead of 'password' and 'database' keyword arguments.
-I'm using MySQL that comes with XAMPP version 8.0.2 / PHP 8.0.2 for Linux.
You are connecting to the database and afterwards you are closing the connection.
Only use cnx.close() when you want to close the database connection, it may cause an error.
If that's not the problem, you may have the user wrong, try to use the default username that MySQL Workbench has, it is root.

Python - Connect MySQL through localhost not working but 127.0.0.1 is working?

I just setup a VPS with a Mysql Database on it. I'm able to log into the database in the SSH terminal with mysql -uroot -p however, the python file shown below is unable to form a connection, and errors with Access denied for root#localhost (using password: YES). I'm confused as to why this is happening. I'm 100% sure my password is correct. I'm using mysql.connector
Python file:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="pwd",
database="db"
)
mycursor = mydb.cursor()
sql = "INSERT INTO test (tests) VALUES (%s)"
val = ("testing")
mycursor.execute(sql, val)
mydb.commit()
I'm a bit new to this DB and VPS stuff, so please bear with me if I don't understand on the first go.
Thanks!
In addition to #don's answer
I assume it has something to do with the dns not properly resolving localhost. I have experienced that before, but outside of just mapping localhost to 127.0.01 in the host's file,
This can also be fixed by changing a value in your /etc/mysql/my.cnf file.
It's likely your bind-address line is
bind-address = localhost
You can try changing it to
bind-address = 127.0.0.1
You can also set it to 0.0.0.0 but this will expose the connection to the outside world (inc. public IP), so this is highly not recommended but will solve the problem.

Python connect to MySQL database on web server

I wanted to know the process of connecting to a MySQL database that is hosted on a web server.
I have a basic free webserver for testing on 000webhost on which I created a MySQL database.
I have the credentials for the database which I will pretend are
host - mysql.webhost000.com
user - dummy_user
password - dummy_password
database - dummy_database
and I have a python script executing from my local computer with internet access
import MySQLdb
db = MySQLdb.connect(host="mysql.webhost000.com",
port=3306,
user="dummy_user",
passwd="dummy_password",
db="dummy_database")
I was hoping it would connect as long as I have the right credentials but when I execute the script it just hangs and once I quit it I see the error
Can't connect to MySQL server on 'mysql.webhost000.com' (4)
Am I missing some steps?
There are two possible problems and im not able to recreate the first one. One is the
host="mysql.webhost000.com"
is incorrect and throwing an error. The connection could be listed as another way. The other I noticed is this is usually how I set up my connection script.
import MySQLdb
def connect():
db = MySQLdb.connect(host="mysql.webhost000.com",
port=3306,
user="dummy_user",
passwd="dummy_password",
db="dummy_database")
c = conn.cursor()
return c, db

Django + PyMSQL + WSGI

I have an application accessing a distant mysql base using PySQL and the following fonction
def db_query(username, password, host, db, query):
try:
db = pymysql.connect(host=host, user=username, passwd=password, db=db)
except pymysql.err.OperationalError as e:
errorLog = e
return [0, errorLog]
# you must create a Cursor object. It will let you execute all the query you need
cur = db.cursor()
cur.execute(query)
result = cur.fetchall()
cur.close()
db.close()
return result
I have recently update Django from 1.7.1 to 1.7.3. Before update code worked. Now, when I'm in production using WSGI and only then (not on local dev server, nor on server dev server) I get a (2003, "Can't connect to MySQL server on [server.address] ([Errno 13] Permission denied)")
I have updated pymysql to latest version available on pip.
I see that 1.7.3 correcting some security issue in WSGI, i tried to downgrade to 1.7.2 (only by doing pip install django==1.7.2 (not sure this is the good way)) and the issue is still present.
Any idea on what i could try to check ?
Thank you in advance for your help.
I think this is related to apache permission to access MySQL database. Can you try command:
setsebool -P httpd_can_network_connect_db 1
Using -P option makes the change persistent. Without this option, the boolean value would be reset to 0 at reboot.

Categories

Resources