Centos7 Docker安装mysql8.0 并执行脚本

16 篇文章 1 订阅
9 篇文章 0 订阅

起因:之前安装过MariaDB10.2,开发时使用Mysql8,当脚本部署到MariaDB10.2一些语法已不支持,因此就直接在Centos7中安装Mysql8.0.29,但是一直没有成功,所以使用Docker安装尝试一下,成功啦,这样也很简单。

0、前提条件

(1)Docker已安装

若未安装可参见链接进行安装:Centos7 安装 Docker_ling1998的博客-CSDN博客

1、拉取Mysql8.0镜像

[root@Tracy src]# docker pull mysql:8.0
8.0: Pulling from library/mysql
c32ce6654453: Pull complete 
415d08ee031a: Pull complete 
7a38fec2542f: Pull complete 
352881ee8fe9: Pull complete 
b8e20da291b6: Pull complete 
66c2a8cc1999: Pull complete 
d3a3a8e49878: Pull complete 
e33a48832bec: Pull complete 
410b942b8b28: Pull complete 
d5323c9dd265: Pull complete 
3212737f31c0: Pull complete 
d0032d4b0dc5: Pull complete 
Digest: sha256:a0805d37d4d298bd61e0dfa61f0ddf6f4680b453fa25d7aad420485a62417eab
Status: Downloaded newer image for mysql:8.0
docker.io/library/mysql:8.0

查看本地镜像

[root@Tracy src]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
mysql        8.0       76152be68449   5 days ago   524MB

2、创建数据挂载目录

[root@Tracy src]# mkdir -p /usr/local/docker/mysql8.0/data
[root@Tracy src]# mkdir -p /usr/local/docker/mysql8.0/mysql-files
[root@Tracy src]# mkdir -p /usr/local/docker/mysql8.0/conf
[root@Tracy src]# mkdir -p /usr/local/docker/mysql8.0/logs

或是命令

mkdir -p /usr/local/docker/mysql8.0/{data,mysql-files,conf,logs} 

3、 启动Mysql容器

docker run --name mysql8 -p 3306:3306 -d  -v /usr/local/docker/mysql8.0/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/docker/mysql8.0/data:/var/lib/mysql -v /usr/local/docker/mysql8.0/logs:/logs -e MYSQL_ROOT_PASSWORD=Jeer20231220! --restart=always  mysql:8.0 --lower_case_table_names=1

[root@Tracy src]# docker run --name mysql8 -p 3306:3306 -d  -v /usr/local/docker/mysql8.0/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/docker/mysql8.0/data:/var/lib/mysql -v /usr/local/docker/mysql8.0/logs:/logs -e MYSQL_ROOT_PASSWORD=Jeer20231220! --restart=always  mysql:8.0 --lower_case_table_names=1
9617dc9a250a20a600ca2b95784532115e84f7c67f3544f714866533e9af571c

查看启动的容器

[root@Tracy src]# docker ps -a
CONTAINER ID   IMAGE       COMMAND                  CREATED              STATUS              PORTS                                                  NAMES
9617dc9a250a   mysql:8.0   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql8

4、设置容器开机自动启动

[root@Tracy src]# docker update mysql8 --restart=always
mysql8

注:确保Docker已设置开机自动启动

5、进入容器,使用Mysql进行验证

[root@Tracy src]# docker exec -it mysql8 bash
root@9617dc9a250a:/# mysql -uroot -p
Enter password:  #输入密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> exit;   #退出
Bye

6、执行sql脚本文件

(1)在挂载卷中放入脚本文件(容器外)

在容器外Centos中,将一个.sql脚本文件放至挂载卷中 /usr/local/docker/mysql8.0/mysql-files

[root@Tracy /]# ls /usr/local/docker/mysql8.0/mysql-files/
test.sql

(2)查看容器对应的目录文件(容器内)

进入容器查看挂载卷对应的目录/var/lib/mysql-files,发现已同步脚本文件

root@9617dc9a250a:/# ls /var/lib/mysql-files
test.sql

(3)执行脚本文件(容器内)

进入mysql,创建数据库并执行脚本文件

root@9617dc9a250a:/# mysql -uroot -p    #进入mysql
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.29 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> create database mytest;  #创建数据库
Query OK, 1 row affected (0.01 sec)

mysql> show databases;          #查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mytest             |          |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use mytest;              #切至数据库
Database changed
mysql> source /var/lib/mysql-files/test.sql; #执行脚本文件,创建数据表
Database changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected, 6 warnings (0.02 sec)

Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected, 4 warnings (0.01 sec)

Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected, 5 warnings (0.01 sec)

Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show tables;               #查看数据表
+-----------------------+
| Tables_in_mytest      |
+-----------------------+
| t_class               |
| t_student             |
| t_teacher             |
+-----------------------+
3 rows in set (0.01 sec)

7、修改时区

(1)查询系统时间

发现比当前时间晚8小时

date -R

root@9617dc9a250a:/# date -R
Mon, 12 Sep 2022 10:04:38 +0800

(2) 修改时区为上海

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@9617dc9a250a:/# ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime 

(3)再次查看系统时间

root@9617dc9a250a:/# date -R
Mon, 12 Sep 2022 18:07:29 +0800

8、保存用户账号

(1)问题

每次git操作都要操作用户名密码,非常麻烦

[root@iZj6ccrze8uto8idape1ozZ client-back]# git pull origin main
Username for 'https://github.com': xxx@xxx.com
Password for 'https://zhangyanling1998@126.com@github.com': 

(2)解决

将用户名密码保存起来 

git config credential.helper store

[root@iZj6ccrze8uto8idape1ozZ client-back]# git config credential.helper store
[root@iZj6ccrze8uto8idape1ozZ client-back]# git pull origin main
From https://github.com/Smooth-Slow-Bot/client-back
 * branch            main       -> FETCH_HEAD
Already up-to-date.
[root@iZj6ccrze8uto8

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值