k8s引用secret来设置容器中MySQL的密码报错

进入mysql容器后,登录mysql一直报错,报错如下:

mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)

用k8s使用secret加密密码来制作mysql容器的流程为:

加密:
在这里插入图片描述
编写secret.yaml文件
在这里插入图片描述

应用secret.yaml生成secret

[root@k8s-master secret]# kubectl apply -f secret.yaml

编写pod_mysql_secret.yaml文件,以环境变量的形式引用了secret,来定义MYSQL_ROOT_PASSWORD环境变量,值为secret对象user-and-passwd的password值
在这里插入图片描述

应用pod_mysql_secret.yaml文件生成pod,pod中的容器为mysql容器

[root@k8s-master secret]# kubectl apply -f pod_mysql_secret.yaml

进入容器
这里一直报错,不能登录mysql

[root@k8s-master secret]# kubectl exec -it mysql /bin/bash
root@mysql:/# mysql -uroot -p'ZhangYu@123'
在这里插入图片描述

搜阅了很多文章后,发现别人加密时,都用了echo -n参数,而我没有用-n参数

echo -n:不换行输出,如果不使用-n,就会把换行符也作为了字符当做密码使用,所以一直报错进不去mysql

解决措施:

加密:使用-n参数
在这里插入图片描述
编写secret.yaml文件
在这里插入图片描述

重新生成secret对象

[root@k8s-master secret]# kubectl apply -f secret.yaml

删除原来的pod,重新制作

[root@k8s-master secret]# kubectl dekete -f pod_mysql_secret.yaml
[root@k8s-master secret]# kubectl apply -f pod_mysql_secret.yaml

进入mysql容器

[root@k8s-master secret]# kubectl exec -it mysql /bin/bash
在这里插入图片描述
成功登录mysql,问题解决

猜你喜欢

转载自blog.csdn.net/weixin_44178770/article/details/125232889