ZhangZhihui's Blog  
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

 

复制代码
zzh@ZZHPC:~$ docker run --name mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=aaa -e MYSQL_DATABASE=simple_bank -d mysql
33fc54b61bdc9aebe532fd5ecc1bc1a73c5cae1e8f12b5f568293c50364c0084
zzh@ZZHPC:~$ 
zzh@ZZHPC:~$ docker exec -it mysql8 mysql -uroot -paaa simple_bank
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 8
Server version: 8.2.0 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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>
复制代码

 

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".

posted on   ZhangZhihuiAAA  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示