我遇到了如下所述的docker / mysql问题:
我有docker-compose.yml
运行一个简单的mysql服务:
version: '3.5'
services:
db:
image: "mysql:8"
restart: always
environment:
MYSQL_DATABASE: 'test'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'test'
MYSQL_ROOT_PASSWORD: 'test'
MYSQL_PORT: 3306
ports:
- "3399:3306"
expose:
- "3306"
volumes:
- container-volume:/var/lib/mysql
- ./station.sql:/docker-entrypoint-initdb.d/station.sql
volumes:
container-volume:
当我执行docker-compose up
时,显示以下错误:
ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
但是,容器仍在运行。
我尝试通过运行连接到在docker容器中运行的mysql服务
mysql -s -h localhost --port=3399 -uroot -ptest
在主机上。然后我得到了这个错误:
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
我搜索了许多解决ERROR 1698 (28000)
的解决方案,但没有一个起作用。我已经完成了:
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'test';
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';
flush privileges;
,我已经exit
并重新启动了Docker容器。我想在容器内执行sudo service restart mysql
或sudo systemctl restart mysql
,但是对于最小的mysql容器来说,这些是不可能的。 docker restart [containerid]
基本上是一样的。
我可以确认插件已更改:
mysql> select user, host, plugin from mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| root | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)
mysql>
但是,当我尝试通过主机上的端口3399
连接到docker sql时,我仍然无法获得
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
这与我正在运行的python项目的错误日志记录一致:
192.168.1.124 - - [06/Dec/2019 15:07:44] "GET /statistics HTTP/1.1" 200 -
192.168.1.124 - - [06/Dec/2019 15:07:44] "GET /static/image/favicon.ico HTTP/1.1" 200 -
192.168.1.124 - - [06/Dec/2019 15:07:44] "GET /user/check_login HTTP/1.1" 200 -
192.168.1.124 - - [06/Dec/2019 15:07:44] "GET /tenants/get HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
return fn()
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 363, in connect
return _ConnectionFairy._checkout(self)
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
rec = pool._do_get()
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
self._dec_overflow()
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
raise value
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
return self._create_connection()
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
connection = pool._invoke_creator(self)
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 453, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/connections.py", line 599, in connect
self._request_authentication()
File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/connections.py", line 861, in _request_authentication
auth_packet = self._read_packet()
File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1698, "Access denied for user 'root'@'localhost'")
有什么建议吗?