前言
使用docker容器时,通过镜像安装mysql数据库,但是容器内的mysql密码没有自定义设置,需要重新设置密码
解决方案
1.更改配置文件,增加”忘记密码启动”模式
2.重新设置root密码
3.删除”忘记密码启动”模式
4.重启容器镜像
具体操作
1.进入容器
#查看docker容器ID [root@web01 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ec35e80c33f7 seafileltd/seafile:latest "/sbin/my_init -- ..." 3 days ago Up 14 hours 0.0.0.0:80->80/tcp seafile #进入容器 [root@web01 ~]# docker exec -it ec35e80c33f7 bash root@ec35e80c33f7:/opt/seafile#
2.编辑mysql配置文件,增加”忘记密码启动”模式:skip-grant-tables
#必须是docker.cnf,如果没有就直接编辑个 root@ec35e80c33f7:/opt/seafile# vi /etc/mysql/conf.d/docker.cnf root@ec35e80c33f7:/opt/seafile# cat /etc/mysql/conf.d/docker.cnf [mysqld] skip-host-cache skip-name-resolve skip-grant-tables
4.退出容器,重启docker容器
[root@web01 ~]# docker restart ec35e80c33f7 ec35e80c33f7
5.进入容器,输入mysql命令,免密登录MySQL
[root@web01 ~]# docker exec -it ec35e80c33f7 bash root@ec35e80c33f7:/opt/seafile# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 10.1.34-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
6.设置root用户密码,刷新权限,退出mysql
#进入mysql数据库 MariaDB [(none)]> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A #更改root密码 Database changed MariaDB [mysql]> UPDATE user SET Password = password ( '123456' ) WHERE User = 'root'; Query OK, 0 rows affected (0.00 sec) Rows matched: 4 Changed: 0 Warnings: 0 #刷新权限 MariaDB [mysql]> flush privileges; Query OK, 0 rows affected (0.00 sec) #退出mysql MariaDB [mysql]> quit Bye root@ec35e80c33f7:/opt/seafile#
7.重新编辑mysql配置文件,删除”忘记密码启动”模式,退出重启容器
root@ec35e80c33f7:/opt/seafile# vi /etc/mysql/conf.d/docker.cnf root@ec35e80c33f7:/opt/seafile# exit [root@web01 ~]# docker restart ec35e80c33f7 ec35e80c33f7
8.进入容器,使用新密码登录MySQL
[root@web01 ~]# docker exec -it ec35e80c33f7 bash root@ec35e80c33f7:/opt/seafile# mysql -uroot -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 10.1.34-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>