How to connect to a remote database server using Python? - mysql

I am trying to connect to a remote database server that is installed on CentOS 7 in VMware from windows using python, but I am unable to connect to it. I am using mysql.connector to access the database.Please help me solve the problem.
import mysql.connector
mydb = mysql.connector.connect(host="192.168.136.129", user="root", passwd="root", database="test", port=3306)
mycr = mydb.cursor()
mycr.execute("CREATE TABLE cat (name VARCHAR(25));")

Don't use root user there might be some permission issue.
Try creating a new user and access it.

Related

MySQL8 – 'caching_sha2_password requires SSL'

I have changed to Mysql8 on my server.
I connect to the server through ssh.
With (e.g.) VScode Database explorer I can use the on the server, but from Python3 with MySQL connector, I don't.
On sever side, 'ssl_disabled=True,' solved the problem, but on my computer, I can not connect to the server's db.
import mysql.connector
from mysql.connector import Error
def create_connection():
connection = None
try:
connection = mysql.connector.connect(
host='host',
port='port',
user='user',
passwd='passwd',
database='db',
ssl_disabled=True,
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"Error in create_connection -->\n '{e}'")
return connection
create_connection()
I try to run this and get:
'caching_sha2_password requires SSL'
I solved it!
If I use port 3307 instead of 3306, it works from the local machine, too!

R how to select the database I connect?

I have both MySQL and MariaDB on my computer, originally I only used MySQL, but recently I have a program using MariaDB. so I used the following script to extract the data
library(RMariaDB)
# export table from mySQL
database <- "movesdb20201105"
mydb = DBI::dbConnect(RMariaDB::MariaDB(), user='moves', password='moves', #actually from RMySQL
dbname = database, host= "localhost")
rs_tmp = DBI::dbSendQuery(mydb, "SELECT * FROM emissionratebyage")
but I look into the connection, it connects to MySQL not MraiaDB, how can I fix this?
Thanks
MariaDB is the successor of MySQL. Maybe just a wrong label?
Also try unloading the MySQL library.

Access a Remote SQL Server which on Colab

I am quite new to working on remote servers so apologize if this is a silly question, but I couldn't find any instructions anywhere.
I have access to a remote DataBase from University but I need to make a connection through Pulse Secure to access it. (And then I use mySQL workbench to do so). Now I need to get the data to work on Deep Learning, in Colab or anywhere else on the cloud. But I have no idea how to do this. I tried using mySQLdb but it didn't work to get a connection.
Any Help is appreciated.
Here's an example how to install MySQL in Colab and query it. You can change the connection to your university remote database.
# install, set connection
!apt-get install mysql-server > /dev/null
!service mysql start
!mysql -e "ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root'"
!pip -q install PyMySQL
%load_ext sql
%config SqlMagic.feedback=False
%config SqlMagic.autopandas=True
%sql mysql+pymysql://root:root#/
# query using %sql or %%sql
df = %sql SELECT Host, User, authentication_string FROM mysql.user
df

Export Data From Raspberry Pi to mysql database on a PC via wifi

I am new to raspberry pi, and mysql servers, I am hoping I could get some insight on a problem which has occurred. This is a part of a project which is due in a few weeks, and was hoping to have a solution soon.
I am attempting to send data, via wifi, from my raspberry pi to a mysql database located on a PC. The raspberry pi and the mysql database are on the same local wifi network.
On the raspberry pi side, I have used the commands:
"sudo apt-get install python3-mysql.connector" and
"sudo apt-get install -f" to install the mysql connector.
my code is as follows:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost", #localhost=ip address of PC
user="user",
passwd="1111",
database="location",
port="80"
)
mycursor = mydb.cursor()
sql = "INSERT INTO location (latitude, longitude) VALUES (%s, %s)"
val = ("26.111111", "-80.44444444")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
But the following error occurred:
self.sock.connect(sockaddr) ConnectionRefusedError:[Errno 111] Connection refused
I'd like to know, are there any other procedures that I am missing?
You are connecting to localhost instead of your PC, where I suspect your MySQL must be running.
I found the solution.
even though the httpd.conf file located on the PC says "Listen 0.0.0.0:80 Listen [::0]:80",
the port in the code should be 3306, instead of 80.

connecting Canopy to mysql

I am new to Python and using Enthought Canopy Express for learning purpose. As part of that I am looking for an option to connect Canopy to mysql. I did not find any materials. Please share if you know any method that I can use mysql in Canopy Express.
I am using Mac OS X version 10.9
Path for canopy is:
/Users/mz/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
Mysql is version 5.6 with the following path:
mysql is /usr/local/mysql/bin/mysql
You can use the Canopy Package Manager to install pymysql:
Once installed, access pymysql according to the Python PEP249 DB API (create a connection, get a cursor, etc.):
In [1]: import pymysql
In [2]: connection = pymysql.connect?
Signature: pymysql.connect(*args, **kwargs)
Docstring:
Establish a connection to the MySQL database. Accepts several
arguments:
host: Host where the database server is located
user: Username to log in as
password: Password to use.
database: Database to use, None to not use a particular one.
port: MySQL port to use, default is usually OK.
...