---
# System prepended metadata

title: mysql build on docker
tags: [Database]

---

---
title: mysql build on docker
---
# mysql build on docker
###### tags: `Database`

m1 mac
---
m1 直接docker pull mysql 报：

no matching manifest for linux/arm64/v8 in the manifest list entries

Docker Hub上没有适用于arm64架构的mysql镜像。

M1芯片是arm64架构，也称作aarch64架构，只能运行arm64/aarch64架构的程序。

使用：
```
docker pull mysql/mysql-server:latest
```
创建并启动MySQL服务容器

run
---
```
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql/mysql-server --lower-case-table-names=1
```
这时使用宿主机连接没有授权访问，需要进入mysql修改mysql访问权限。
```
docker exec -it mysql bash

mysql -u root -p 
```
#授权
```
CREATE USER 'root'@'%' IDENTIFIED BY 'root';

GRANT ALL ON *.* TO 'root'@'%';
```

#刷新权限
```
flush privileges;
```
#修改root用户密码
```
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
```
#刷新权限
```
mysql> flush privileges;
```
mysql数据库创建带横杠的数据库名
---
```
create database `d-d`;    //加反引号
```
dbeaver連接時 報錯處理
---
Public Key Retrieval is not allowed:
https://blog.csdn.net/qq_42816401/article/details/91303134

Access denied for user 'root'@'172.17.0.1' (using password: NO):
https://www.programminghunter.com/article/8019884516/

版本確認
---
SELECT VERSION();

dml
---
查詢使用者
---
```
mysql> SELECT User,Host FROM mysql.user;
```

查帳號權限
---
```
mysql> SHOW GRANTS FOR backuper;
```

restore
---
```
bash-4.4# mysql -u root -p tg-dev < /opt/tg-dev.sql
```

dump
---
mysqldump -u root -p tg_dev > /opt/tg_dev.sql
提示後再輸入密碼
https://dev.to/lanandra/backup-mysql-database-that-running-on-docker-container-1k8h

restore
---
mysql -u [uname] -p [db_to_restore] < [backupfile.sql]
提示後再輸入密碼

密碼特殊字符
---
mysql創建的密码中有特殊字符例如&，在命令行下需加入 "\"

大小寫敏感設置
---
https://www.cnblogs.com/czwbig/p/9961069.html
重點：如果已經運行才設置會報錯。  需砍掉重建