>>> from sqlalchemy import create_engine >>> engine = create_engine('mysql+mysqldb://root:123456@localhost/beta_monitor') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/bqi/venv/beta-monitor/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 424, in create_engine return strategy.create(*args, **kwargs) File "/Users/bqi/venv/beta-monitor/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 81, in create dbapi = dialect_cls.dbapi(**dbapi_args) File "/Users/bqi/venv/beta-monitor/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 102, in dbapi return __import__('MySQLdb') ImportError: No module named MySQLdb
错误信息 Authentication plugin 'caching_sha2_password' cannot be loaded
1 2 3 4 5 6 7 8 9 10 11 12 13 14
>>> engine = create_engine('mysql+mysqldb://root:123456@127.0.0.1/beta_monitor') >>> connection = engine.connect() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/bqi/venv/beta-monitor/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2102, in connect return self._connection_cls(self, **kwargs)
...
File "/Users/bqi/venv/beta-monitor/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "/Users/bqi/venv/beta-monitor/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__ super(Connection, self).__init__(*args, **kwargs2) sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/Cellar/mysql/5.7.22/lib/plugin/caching_sha2_password.so, 2): image not found") (Background on this error at: http://sqlalche.me/e/e3q8)
~|⇒ docker run -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=beta_monitor -e MYSQL_USER=test -e MYSQL_PASSWORD=123456 mysql --default-authentication-plugin=mysql_native_password d8f5e623dc595df19b9d6cce52780381b625c1565622f5867f2ad3aeafdca499
如果安装在服务器上,则在my.cnf中修改相关配置
测试
本地连接测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
~|⇒ mysql -utest -p123456 -h 127.0.0.1 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 ~|⇒ mysql -utest -p -h 127.0.0.1 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.12 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.