Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to MySQL without using a password? (PyMySQL)

Tags:

python

mysql

Here's the relevant part of my code:

        try:
            if self.pass_entry.get():
                self.connection = pymysql.connect(
                    host=self.host_entry.get(),
                    user=self.user_entry.get(),
                    password=self.pass_entry.get(),
                    db=self.db_entry.get(),
                    charset="utf8mb4",
                    cursorclass=pymysql.cursors.DictCursor
                )
            else:
                self.connection = pymysql.connect(
                    host=self.host_entry.get(),
                    user=self.user_entry.get(),
                    db=self.db_entry.get(),
                    charset="utf8mb4",
                    cursorclass=pymysql.cursors.DictCursor
                )
        except Exception as e:
            self.console.insert(tk.END, "Error: {err}\n".format(err=str(e)))

Here's the error:

Connecting to 127.0.0.1...
Error: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

There's no password on "root" here, and PyMySQL is supposing that it has one. How can I connect without using a password? (I've tried to omit the password option when the password field / string is empty, but PyMySQL doesn't take it into account).

like image 612
Ericson Willians Avatar asked Mar 09 '16 01:03

Ericson Willians


People also ask

What is difference between PyMySQL and MySQL Connector?

PyMySQL and mysql-connector can be categorized as "PyPI Packages" tools. PyMySQL and mysql-connector are both open source tools. PyMySQL with 6.45K GitHub stars and 1.29K forks on GitHub appears to be more popular than mysql-connector with 37 GitHub stars and 8 GitHub forks.

What is MySQL PyMySQL?

PyMySQL is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and contains a pure-Python MySQL client library. The goal of PyMySQL is to be a drop-in replacement for MySQLdb.

How do you connect to a MySQL database using MySQL?

To Connect to a MySQL Database Expand the Drivers node from the Database Explorer. Right-click the MySQL (Connector/J driver) and choose Connect Using.... The New Database Connection dialog box is displayed. In the Basic Setting tab, enter the Database's URL <HOST>:<PORT>/<DB> in the corresponding text field.


1 Answers

When there is no password for the database you need to pass "" to the function as password.

Try this in your else:

        else:
            self.connection = pymysql.connect(
                host=self.host_entry.get(),
                user=self.user_entry.get(),
                password="",
                db=self.db_entry.get(),
                charset="utf8mb4",
                cursorclass=pymysql.cursors.DictCursor
            )
like image 129
hhaefliger Avatar answered Oct 02 '22 00:10

hhaefliger