mysql安装 ( docker )
南江烂柯人 JAVA劝退师

Mysql5.X安装

拉取镜像

docker pull  mysql:5.7.31

创建挂载文件

mkdir /data/mysql/{cnf,logs,mysql}

创建配置文件

vi /data/mysql/cnf/my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#写入下面配置
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log

default-time_zone='+8:00'

skip-name-resolve

log-bin=mysql-bin
binlog-format=ROW
server_id=2

symbolic-links=0
expire_logs_days=7

max_connections = 2000
max_user_connections = 1900
max_connect_errors = 100000
max_allowed_packet = 50M
lower_case_table_names=0
sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

[client]
default-character-set=utf8

[mysql]
character-set-server=utf8

编写docker-compose文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
version: '3.5'
services:
web:
image: mysql:5.7.31
container_name: mysql-5.7.31
restart: always
ports:
- 3306:3306
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=root
#目录挂载
volumes:
- /data/mysql/conf:/etc/mysql/conf.d
- /data/mysql/logs:/logs
- /data/mysql/mysql:/var/lib/mysql

启动

docker-compose up -d
#查看日志
docker-compose logs -f 

配置远程访问

#进入mysql容器
docker exec -it mysql-5.7.31 bash
#登录mysql
mysql -uroot -proot
#创建用户并添加权限
grant all privileges on 库名.* to 登录名@'%' identified by '123456';
#刷新mysql权限
FLUSH PRIVILEGES;
#退出
exit;

Mysql8.X安装

拉取镜像

docker pull mysql:8.0.31

创建挂载文件

mkdir /data/mysql/{cnf,logs,mysql}

创建配置文件

vi /data/mysql/cnf/my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#写入下面配置
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql

default-time_zone='+8:00'
skip-name-resolve

log-bin=mysql-bin
binlog-format=ROW
server_id=1

symbolic-links=0
expire_logs_days=7

max_connections = 2000
max_user_connections = 1900
max_connect_errors = 100000
max_allowed_packet = 50M
lower_case_table_names=0
sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

编写docker-compose文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
version: '3.5'
services:
mysql-3310:
image: mysql:8.0.31
container_name: mysql-3310
privileged: true
restart: always
ports:
- 3310:3306
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=root
- character-set-server=utf8mb4
- collation-server=utf8mb4_general_ci
#目录挂载
volumes:
- /data/mysql/cnf:/etc/mysql/conf.d
- /data/mysql/logs:/logs
- /data/mysql/mysql:/var/lib/mysql

启动

docker-compose up -d
# 查看日志
docker-compose logs -f 

配置远程访问

# 进入mysql容器
docker exec -it mysql-5.7.31 bash
# 登录mysql
mysql -uroot -proot

# 在mysql8.0创建用户和授权和之前不太一样了,其实严格上来讲,也不能说是不一样,只能说是更严格,mysql8.0需要先创建用户和设置密码,然后才能授权. 
# 先创建一个用户
create user 'test'@'%' identified by '123123';   
# 再进行授权
grant all privileges on *.* to 'test'@'%' with grant option;

# 修改远程加密规则:
ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123123';

# 刷新权限:
FLUSH PRIVILEGES;
#OK,设置完成,再次使用 Navicat 连接数据库
  • 本文标题:mysql安装 ( docker )
  • 本文作者:南江烂柯人
  • 创建时间:2022-11-08 11:52:53
  • 本文链接:https://www.mattjia.com/2022/11/08/mysql/mysql安装 ( docker ) /
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论