ghostrider513
Posts: 35
Joined: Tue Mar 28, 2017 6:03 am

How do I connect to my MySQL on my website DB?

Thu Jun 28, 2018 4:20 am

I have a sensor that is printing data. I need to have that data sent to my database on my website. None of the tutorials work. How do I connect my RPI to my Dbase?

I am not a programmer and have very little knowledge of coding in python. It would be great if someone code write the code, tell me where it goes on my code and what info I need to complete to make it work.

raspcoder
Posts: 2
Joined: Sat Jun 30, 2018 5:01 pm

Re: How do I connect to my MySQL on my website DB?

Sat Jun 30, 2018 5:26 pm

You can setup MySQL to allow remote logins. This has to be configured server side. There's an example here: https://www.cyberciti.biz/tips/how-do-i ... erver.html

After configuring MySQL remote access, you can do something like this:

Code: Select all

DB = 'gts'
DB_HOST = 'ps95074.dreamhost.com'
DB_USER = 'root'
DB_PASSWORD = 'dbadminpassword'
conn = MySQLdb.Connection(db=DB, host=DB_HOST, user=DB_USER,passwd=DB_PASSWORD)
cursor = conn.cursor()

User avatar
The Traveler
Posts: 360
Joined: Sat Oct 21, 2017 3:48 pm

Re: How do I connect to my MySQL on my website DB?

Mon Jul 02, 2018 1:38 pm

Where is your db located? Commercial server? Institutional, ie: school? Private? Being able to access your mySQL db usually requires some intervention on the part of the system administrator. There is a wide range of options for access and you'll need to know the particulars. It's hard to make recommendations otherwise.

Cheers.
Retired IT professional, C programmer and "beardie weirdie".
RPi interests: Developing an Infinite Improbability Drive
“Thinking outside of the box allows you to get rewards outside of your reach.” Matshona Dhliwayo

User avatar
DougieLawson
Posts: 35364
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How do I connect to my MySQL on my website DB?

Mon Jul 02, 2018 1:42 pm

If your database server isn't on your local LAN then you really want to run port 3306 traffic through a TLS secure tunnel.

https://dev.mysql.com/doc/refman/8.0/en ... tions.html
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Python”