-1

I did a python -m pip install mysql-connector and able to successfully run import mysql.connector through python. But when I am trying to run the below code.

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword"
)

print(mydb)

It is failing with InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)

Since I did pip install for mysql.connector I am not sure of user and passwd.

Sushant
  • 1
  • 1
  • 4

2 Answers2

0

I connected my database instance from Amazon RDS to mySql workbench, created a python file that looks like his and got the exact error. Everyone says you could have a system firewall problem but nothing straight forward.

Vin...C
  • 1
  • 6
-1

The connector is just a means of communicating with a mysql database programically.

You need this, or access to a mysql server to use the connector. https://dev.mysql.com/downloads/mysql/

The host is obviously localhost when you want to access it locally on your machine. This only works if the server is running on your machine. You can connect a remote server by changing the host to a valid IP address and providing valid credentials. If you use it remotely make sure the server has access through the firewall and that you properly forward the TCP port you decide to use. You may not have to forward, but I would as a general rule of thumb to make it one less thing to check when troubleshooting.

Good luck

Keith Cronin
  • 373
  • 2
  • 9
  • Since I already have done `python -m pip install mysql-connector` and I can run `import mysql.connector` I am not sure why i separately have to install https://dev.mysql.com/downloads/mysql/. – Sushant Mar 16 '19 at 15:28
  • I still tried to download and install mysql locally but it is failing with below error `C:\Users\susha\Downloads\mysql-8.0.15-winx64>python setup.py install (null): can't open file 'setup.py': [Errno 2] No such file or directory` – Sushant Mar 16 '19 at 15:35
  • @Sushant the connector isnt a mysql database. You are attempting to connect to something that doesnt exist which is why the machine refuses the connection. You run the setup file you got from the website and carefully go through the installation and configuration process. Also you need to make a database / schema after you set up the server and add columns and indexes to store information into them in which you can read or manipulate with SQL queries in python USING the connector. The connector isnt the database or the server it's just a library to communicate with a mysql database. – Keith Cronin Mar 16 '19 at 15:51
  • @Sushant additionally I recommend Mysql workbench for working with the mysql server. It's not the most intuitive software in the world but once you figure it out it's a great tool. – Keith Cronin Mar 16 '19 at 15:53
  • @Sushant `import mysql.connector` just imports the library into python for you to use it. The connector comes with the database. So unless you obtained the connector separately you might already have the server you just need to use mysql workbench to build the database and turn on the server. – Keith Cronin Mar 16 '19 at 16:28
  • After installing mysql database, when I running the same above code I am getting error as `mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported` I have followed this page caching sha2 password is not supported mysql and did install, but it does not solve my issue. `python -m pip install mysql-connector-python` I have python 3.7 in my system at location `C:\Users\susha\AppData\Roaming\Python\Python37\Scripts` – Sushant Mar 16 '19 at 16:53
  • @Sushant This is because the connector must not support the method of authentication that you chose for your server. Configure the server to use the classic method of authentication. I think you can do this by relaunching the setup program you downloaded and configuring your installation. – Keith Cronin Mar 16 '19 at 16:59
  • Can the configuration be changed post installation or I need to uninstall and re-install the software ? – Sushant Mar 16 '19 at 20:05
  • I think I can be changed post install but since you dont have anything worth saving just reinstall. – Keith Cronin Mar 16 '19 at 20:33
  • @Sushant I'd reinstall – Keith Cronin Mar 16 '19 at 21:01