docker 启动mysql命令:
docker run -it \
--name mysql-test \
-p 3306:3306 \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql
启动后:
1. docker exec 到 容器内部可以登录mysql;
2. 采用python 测试mysql 连接与读写成功:
import pymysql
conn=pymysql.connect(host = '127.0.0.1', user = 'root', passwd='123456', port= 3306,
#db='mysql',
charset='utf8')
def get_data():
cur = conn.cursor() # 生成游标对象
cur.execute("use `mysql`")
sql="select * from `db` " # SQL语句
cur.execute(sql) # 执行SQL语句
data = cur.fetchall() # 通过fetchall方法获得数据
col = cur.description
for i in data[:2]:
print (i)
print(col)
cur.close()
conn.close() # 关闭连接
不过, 我们采用jdbc 和 Dbeaver 、SQLpro 、Navicat等SQL界面工具连接却报错:
java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
查询发现解决方案:
https://blog.csdn.net/waterflying2015/article/details/81047128
进入docker mysql 控制台;
docker exec -it mysql bash
mysql -uroot -p
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
修改后即可。