0

So I have set-up a mySQL database and am trying to connect to it from Python. Unfortunately the Jupyter kernel keeps dieing if I try to connect. I don't have much to go on given I don't get an error message either, but this is the code I use (although I have tried SQLalchemy and pyodbc as well):

import mysql.connector

cnx = mysql.connector.connect(user='root', password='test',
                              host='localhost',
                              database='testdb')
cnx.close()

I am assuming it is something in my set-up/access but I have limited experience with SQL. I tried to do the troubleshooting suggested in some other questions but to no avail. I checked the session details in MySQL workbench and they are the same. I have also added the disable SSL statement (given it says in the session details SSL is not used) but again no change.

Hope you can help with the limited info I supplied/have.

5
  • Does it work if you run the same code as a normal python script (as opposed to Jupyter)? Does it raise any errors? Have you tried wrapping it in a try except? Commented Mar 20 at 10:19
  • If you're using Jupyter on a remote machine, you'll need to allow root to connect from % (any host), but avoid this on production systems for security reasons. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'test'; FLUSH PRIVILEGES; Commented Mar 20 at 12:03
  • Tried these suggestions, but unfortunately it didn't fix the issue. Even with a try statement the kernel still dies. Running it in anaconda prompt kicks me out of Python. @SzymonRoziewski, I got a syntax error in MySQL command line, but based on stackoverflow.com/questions/5016505/… I am assuming that has to do with the version of MySQL I am using. Changing the syntax based on the answers supplied there made me get rid of the syntax error, but it didn't fix the problem in Python. Commented Mar 20 at 13:41
  • Not sure if it helps but have you tried running mysql -u root -p test testdb from the command line? This would show if you can connect to server. Commented Mar 20 at 19:03
  • 1
    @YannisP. I had initially missed this comment. I am able to connect to the database via this way (after adding to my pathvariable), however this still doesn't solve the issue in Python (just tested). Commented Mar 26 at 12:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.