I'm using python and I'm trying to make a connection to a MySQL db. The problem is that when I run this code:
import mysql.connector
print('Attempting connection to database...')
con = mysql.connector.connect(
host="localhost",
user="root",
password="root"
)
print(con)
The terminal doesn't shows me the print. I've been debugging and I noticed that the program executes till the line 7 then jumps directly to the line 4 and finish the execution. I don't know if this behavior is normal but I'd like to know how to solve this, please :D
I'm also using MySQL Workbench to see if the connection is ok and it actually works.
I'm using python and I'm trying to make a connection to a MySQL db. The problem is that when I run this code:
import mysql.connector
print('Attempting connection to database...')
con = mysql.connector.connect(
host="localhost",
user="root",
password="root"
)
print(con)
The terminal doesn't shows me the print. I've been debugging and I noticed that the program executes till the line 7 then jumps directly to the line 4 and finish the execution. I don't know if this behavior is normal but I'd like to know how to solve this, please :D
I'm also using MySQL Workbench to see if the connection is ok and it actually works.
Share Improve this question edited Feb 2 at 18:17 Shadow 34.3k10 gold badges65 silver badges75 bronze badges asked Feb 2 at 18:04 VictorM03VictorM03 33 bronze badges 4 |1 Answer
Reset to default 0If you're new to database connections, perhaps my library might help you with some of this. https://github/hotnsoursoup/elixirdb
pip install elixirdb[mysql]
Then you can define a yaml config, or a dictionary.
from elixirdb import ElixirDB
config = {
"dialect": "mysql",
"url_params": {
"host": "localhost",
"port": 3306,
"database": "elixirdb",
"user": "test_user",
"password": "StrongPassword!123",
},
"engine_options": {
"pool_size": 20,
"pool_recycle": 3600,
"max_overflow": 10,
"echo": False,
},
}
db = ElixirDB(config)
db.connect()
if db.has_connection():
print(db.connection)
Output
<sqlalchemy.engine.base.Connection object at 0x000001C7FFA64AA0>
print the dict for class Connection
from pprint import pprint
pprint(db.connection.__dict__)
{'_Connection__can_reconnect': True,
'_Connection__in_begin': False,
'_Connection__savepoint_seq': 0,
'_allow_autobegin': True,
'_dbapi_connection': <sqlalchemy.pool.base._ConnectionFairy object at 0x00000205EF2551F0>,
'_echo': False,
'_execution_options': immutabledict({}),
'_has_events': False,
'_nested_transaction': None,
'_transaction': None,
'dialect': <sqlalchemy.dialects.mysql.pymysql.MySQLDialect_pymysql object at 0x00000205EF175040>,
'dispatch': <sqlalchemy.event.base.JoinedConnectionEventsDispatch object at 0x00000205EF22B100>,
'engine': Engine(mysql+pymysql://test_user:***@localhost:3306/elixirdb)}
It's a new library, so any stars on it would be appreciated =)
Attempting connection to database... <mysql.connector.connection_cext.CMySQLConnection object at 0x00000241E0DE6510>
. – chickity china chinese chicken Commented Feb 2 at 19:32cmd.exe
,bash
, etc. depending on your OS). If successful, then implement the connection in Python. – chickity china chinese chicken Commented Feb 2 at 19:33mysql
connection from a terminal ... if your MySQL server is running, and your MySQL credentials for user isroot
, password isroot
, thenC:\> mysql -u root -p
should prompt you to enter your password, then drop you into amysql>
terminal with a successful connection. If not, then there are other issues to debug. – chickity china chinese chicken Commented Feb 2 at 19:36