1

I've recently installed the official MySQL extension for Python. However, when connecting to the server it asks me to select the database, but I have not made a database yet.

I don't really know what to do here. So I tried to connect using my information without the database but received errors with the following code:

import mysql.connector

cnx = mysql.connector.connect(user='ubuntulogin', password='ubuntupassword',
                          host='localhost')

cursor = cnx.cursor()

query = ("CREATE DATABASE database")

cursor.execute(query)

cursor.close()

cnx.close()

Please let me know any issues with my code or how to get MySQL information when I don't know mydatabase name.

Thanks

EDIT: My error message when running the code was:

File "/home/liam/sqltest.py", line 3, in <module>
cnx = mysql.connector.connect(user='ubuntulogin', password='ubuntupassword', host='localhost')
File "/usr/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 162, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 129, in __init__
self.connect(**kwargs)
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 454, in connect
self._open_connection()
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 417, in _open_connection
self._socket.open_connection()
File "/usr/lib/python2.7/dist-packages/mysql/connector/network.py", line 475, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
6
  • Please include the complete error message in your question. Commented Sep 18, 2017 at 1:32
  • Sorry I'll do that now Commented Sep 18, 2017 at 1:44
  • @DYZ I've added the error message, sorry about not including it earlier Commented Sep 18, 2017 at 1:49
  • Looks like you haven't started the MySQL server or you do not have an account with the credentials 'ubuntulogin' and 'ubuntupassword'. Commented Sep 18, 2017 at 1:51
  • @DYZ How do I start the MySQL server. Are the credentials different to what I use to login to my actual server? Commented Sep 18, 2017 at 3:34

2 Answers 2

2

Okay, so it turns out that I am a complete idiot. I was under the impression that MySQL was pre-installed and running since I am used to PHP. However, I just installed it and use my code above and everything seems to be working.

Thanks to @DYZ for pointing out that it looked like I hadn't started the MySQL server and my credentials were incorrect (which turned out to be both true.

Sign up to request clarification or add additional context in comments.

Comments

0

You forgot something)

cursor.execute("CREATE DATABASE database;")

1 Comment

The OP has this line in their code. The semicolon at the end is optional.

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.