zzh@ZZHPC:~$ docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=verysecretpass -e MYSQL_DATABASE=order mysql
In this case, our data source URL is
root:verysecretpass@tcp(127.0.0.1:3306)/order
docker run -d \
--name my-mysql \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-v /your/local/path/mysql-data:/var/lib/mysql \
mysql:8
zzh@ZZHPC:~$ docker run --name mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=aaa -e MYSQL_DATABASE=simple_bank -d mysql 33fc54b61bdc9aebe532fd5ecc1bc1a73c5cae1e8f12b5f568293c50364c0084 zzh@ZZHPC:~$ docker exec -it mysql8 mysql -uroot -paaa simple_bank mysql>
Run MySQL without creating a database:
zzh@ZZHPC:~$ docker run --name mysql -p 3306:3306 mysql
2024-09-03 02:58:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 9.0.1-1.el9 started.
2024-09-03 02:58:28+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2024-09-03 02:58:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 9.0.1-1.el9 started.
2024-09-03 02:58:28+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following as an environment variable:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
zzh@ZZHPC:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
948b46ed402b mysql "docker-entrypoint.s…" 11 seconds ago Exited (1) 10 seconds ago mysql
zzh@ZZHPC:~$ docker container rm mysql
mysql
zzh@ZZHPC:~$ docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=rootpwd mysql
2024-09-03 03:00:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 9.0.1-1.el9 started.
2024-09-03 03:00:02+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2024-09-03 03:00:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 9.0.1-1.el9 started.
2024-09-03 03:00:03+00:00 [Note] [Entrypoint]: Initializing database files
2024-09-03T03:00:03.052670Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
2024-09-03T03:00:03.054535Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 9.0.1) initializing of server in progress as process 80
......
2024-09-03T03:00:11.781864Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2024-09-03T03:00:11.806045Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2024-09-03T03:00:11.806392Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '9.0.1' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
zzh@ZZHPC:~$ docker exec -it mysql mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) zzh@ZZHPC:~$ docker exec -it mysql mysql -uroot -prootpwd mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 9.0.1 MySQL Community Server - GPL Copyright (c) 2000, 2024, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Executing below SQL statements:
CREATE DATABASE snippetbox CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; USE snippetbox; CREATE USER 'web'@'localhost'; GRANT SELECT, INSERT, UPDATE, DELETE ON snippetbox.* TO 'web'@'localhost'; ALTER USER 'web'@'localhost' IDENTIFIED BY 'webpwd';
Leave the mysql prompt.
Connect to the snippetbox database with the web user:
zzh@ZZHPC:~$ docker exec -it mysql mysql -D snippetbox -u web -p Enter password: mysql>
Got below error with DSN "web:webpwd@tcp(localhost:3306)/snippetbox?parseTime=true":
Error 1045 (28000): Access denied for user 'web'@'172.17.0.1' (using password: YES)
Create another user web as below:
CREATE USER zeb; GRANT SELECT, INSERT, UPDATE, DELETE ON snippetbox.* TO zeb; ALTER USER zeb IDENTIFIED BY 'zebpwd';
Successfully connect to the database with DSN "zeb:zebpwd@tcp(localhost:3306)/snippetbox?parseTime=true".

浙公网安备 33010602011771号