Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

mysql - Grant Privileges to Root via Dockerfile

Is there some way to grant all privileges to root via Dockerfile ou docker-compose.yml? I'm trying to boot up a mysql:latest image like this:

docker-compose.yml

version: "3.9"
 services:
  mysql_database:
   container_name: mysql_test_server
   restart: always
   build: .
   image: mysql:latest
   command: --default-authentication-plugin=mysql_native_password
   ports:
     - 3306:3306
   volumes:
     - ./data:/var/lib/mysql
     - ./config:/etc/mysql/conf.d
  environment:
    MYSQL_ROOT_PASSWORD: root

dockerfile (the build: . on .yml)

FROM mysql:latest
RUN mysql -u root -proot -h localhost -p 3306
RUN grant all privileges on *.* to 'root'@'%' identified by 'root';
RUN flush all privileges;
RUN exit;
EXPOSE 3306

TLDR; I just want to grant privileges for remote connections (SQLYog, DBeaver, etc) automatically... if docker-compose up, run this command so I don't need to do manually, like docker exet -it mysql_test_server mysql -u root ...

I awalys get this:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
question from:https://stackoverflow.com/questions/65834162/grant-privileges-to-root-via-dockerfile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your MySQL server is not running in this docker file, so you are getting errors.

The default docker image has root user and root user has all privileges.

You have to use environment variables to achieve what you try to do:

Run this command:


docker run --name some-mysql 
    -v /my/own/datadir:/var/lib/mysql 
    -e MYSQL_ROOT_PASSWORD=rootPassword 
    -e MYSQL_ROOT_HOST="%" 
    -p 3306:3306 
    -d mysql:tag

You have to replace paths in the -v parameter.

Then you can login to MySQL with command:

mysql -uroot -prootPassword -h127.0.0.1

The parameter you should take a look is MYSQL_ROOT_HOST. That variable is used in the MySQL container entrypoint: https://github.com/docker-library/mysql/blob/master/8.0/docker-entrypoint.sh#L232-L239


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...