MySQL 5.7 – 永夜 https://www.shuijingwanwq.com 没有不值得去解决的问题,也没有不值得去学习的技术! Mon, 01 Jun 2026 08:48:35 +0000 zh-Hans hourly 1 https://wordpress.org/?v=7.0 配置开发环境:使用 Docker 管理多版本 MySQL 与 Redis https://www.shuijingwanwq.com/2026/06/01/15331/ https://www.shuijingwanwq.com/2026/06/01/15331/#respond Mon, 01 Jun 2026 08:48:33 +0000 https://www.shuijingwanwq.com/?p=15331 Post Views: 31

本文是「从 Windows 到 Ubuntu:迁移与配置完全指南」系列的开发环境篇。
适合已经完成 Ubuntu 系统安装,希望搭建高效、隔离、多版本数据库环境的开发者。


为什么要用 Docker 管理数据库?

在 Windows 下我习惯用 WSL2 + Docker 跑独立的数据库容器,迁移到 Ubuntu 后自然延续这一方式。相比直接在宿主机安装:

  • 多版本共存:同一台机器同时运行 MySQL 5.7 和 8.0,Redis 任意版本,互不干扰。
  • 环境隔离:每个项目的数据库独立或按版本共享,配置不打架。
  • 轻量易清理:容器停止即释放资源,数据卷保留,删除无残留。
  • 版本锁死mysql:5.7 镜像永远对应 5.7 最新版,不会因系统升级自动变更。

本文以 MySQL 5.7、MySQL 8.0、Redis 7.2 为例,搭建三个独立容器,供后续 Go/PHP 项目共用。


一、安装 Docker 引擎(纯命令行,不用 Docker Desktop)

Ubuntu 的“软件”或“应用中心”无法搜到 Docker Desktop,这是 Linux 发行版软件分发的特点——专业工具通常需要手动添加官方软件源。官方源安装更轻量、稳定。

1. 卸载可能冲突的旧包(全新系统可跳过)

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
  sudo apt remove -y $pkg
done

如果你是全新安装的 Ubuntu 26.04,这些包本来就不存在,此步骤可跳过。

2. 添加 Docker 官方 GPG 密钥和软件源

sudo apt update
sudo apt install -y ca-certificates curl

sudo install -m 0755 -d /etc/apt/keyrings
sudo curl --http1.1 -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt update

踩坑记录:如果 curl 下载 GPG 时报 curl: (16) Error in the HTTP2 framing layer,加上参数 --http1.1 即可解决。

3. 安装 Docker 引擎及 Compose 插件

sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

4. 将当前用户加入 docker 组(免 sudo 执行)

sudo usermod -aG docker $USER

必须注销并重新登录(或重启),否则 docker 命令仍需要 sudo。

5. 验证安装

docker --version          # 输出如 Docker version 29.5.2
docker compose version    # 输出 Docker Compose version v2.x

二、网络与镜像拉取问题排查(真实踩坑实录)

在 VPN 环境下,拉取第一个测试镜像时就遇到了网络超时。以下是完整的排查和解决过程。

2.1 第一次尝试:直接运行 hello-world

docker run hello-world

输出:

Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: failed to resolve reference "docker.io/library/hello-world:latest": failed to do request: Head "https://registry-1.docker.io/v2/library/hello-world/manifests/latest": dial tcp 128.242.240.85:443: i/o timeout

2.2 重启 Docker 服务,再次尝试

sudo systemctl restart docker
docker run hello-world

仍然超时,这次 IP 变成了 31.13.69.245

dial tcp 31.13.69.245:443: i/o timeout

2.3 检查宿主机能否访问 Docker Hub

curl -I https://registry-1.docker.io/v2/

输出:

HTTP/1.1 200 Connection established

HTTP/2 401 
date: Sun, 31 May 2026 10:04:04 GMT
content-type: application/json
content-length: 87
...

这说明宿主机网络正常(返回 401 是因为没有认证,但连接已建立),问题出在 Docker 守护进程自身。

2.4 配置国内镜像加速器(第一次尝试)

创建 /etc/docker/daemon.json,使用中科大、网易、百度镜像:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}
EOF
sudo systemctl daemon-reload
docker run hello-world

结果依然超时(IP 变为 103.97.3.19):

dial tcp 103.97.3.19:443: i/o timeout

2.5 再次重启 Docker,问题依旧

sudo systemctl restart docker
docker run hello-world

这次错误变了:

dial tcp: lookup docker.mirrors.ustc.edu.cn on 127.0.0.53:53: no such host

说明镜像加速器的域名无法解析 —— VPN 影响了 DNS。

2.6 最终方案:使用 DaoCloud 镜像 + 指定公共 DNS

修改 /etc/docker/daemon.json,同时配置镜像和 DNS:

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://docker.m.daocloud.io"],
  "dns": ["8.8.8.8", "1.1.1.1"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
docker run hello-world

这次终于成功拉取并运行:

latest: Pulling from library/hello-world
4f55086f7dd0: Pull complete 
d5e71e642bf5: Download complete 
Digest: sha256:0e760fdfbc48ba8041e7c6db999bb40bfca508b4be580ac75d32c4e29d202ce1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
...

结论:在 VPN 环境下,Docker 守护进程可能无法正确使用系统 DNS,需要显式指定公共 DNS(如 8.8.8.8)并配合一个可用的镜像加速器。DaoCloud 镜像 docker.m.daocloud.io 在此环境中可用。


三、部署 MySQL 5.7 / 8.0 和 Redis 7.2 容器

3.1 创建项目目录

mkdir -p ~/docker/services
cd ~/docker/services

3.2 编写 docker-compose.yml

cat > docker-compose.yml << 'EOF'
version: "3.8"      # 新版 Docker Compose 可省略该行,会有警告但无影响

networks:
  dev-network:
    driver: bridge

services:
  mysql57:
    image: mysql:5.7
    container_name: mysql57
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: mystrongpwd57
    ports:
      - "3306:3306"
    volumes:
      - ./data/mysql57:/var/lib/mysql
    networks:
      - dev-network

  mysql80:
    image: mysql:8.0
    container_name: mysql80
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: mystrongpwd80
    ports:
      - "3307:3306"
    volumes:
      - ./data/mysql80:/var/lib/mysql
    networks:
      - dev-network
    command: --default-authentication-plugin=mysql_native_password

  redis:
    image: redis:7.2
    container_name: redis72
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - ./data/redis72:/data
    command: redis-server --requirepass myredispass --appendonly yes
    networks:
      - dev-network
EOF

避坑提醒:MySQL 8.0 需要添加 --default-authentication-plugin=mysql_native_password,否则某些旧客户端连接会报 caching_sha2_password 错误。

3.3 第一次启动(遇到网络波动)

docker compose up -d

输出显示:

WARN[0000] the attribute `version` is obsolete...
[+] up 19/24
 ✘ Image mysql:8.0  Error failed to resolve reference ... EOF

错误原因是拉取 MySQL 8.0 时认证失败(EOF),可能是镜像加速器暂时性问题。

3.4 直接重试,成功

docker compose up -d

这次所有镜像都拉取成功:

[+] up 25/34
 ✔ Image mysql:5.7   Pulled
 ✔ Image redis:7.2   Pulled
 ✔ Image mysql:8.0   Pulled
 ✔ Network services_dev-network Created
 ✔ Container mysql57 Started
 ✔ Container redis72 Started
 ✔ Container mysql80 Started

经验:Docker Hub 或镜像加速器偶发性网络问题,重试一次往往就能解决。


四、验证容器运行及连接测试

4.1 查看容器状态

docker ps

输出:

CONTAINER ID   IMAGE       COMMAND                   STATUS          PORTS                                                    NAMES
9ec38ca384b6   redis:7.2   "docker-entrypoint.s…"   45 seconds ago   Up 42 seconds   0.0.0.0:6379->6379/tcp, [::]:6379->6379/tcp              redis72
4e93fd2e2c3f   mysql:8.0   "docker-entrypoint.s…"   45 seconds ago   Up 42 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp, [::]:3307->3306/tcp   mysql80
29598dd51087   mysql:5.7   "docker-entrypoint.s…"   45 seconds ago   Up 42 seconds   0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp, 33060/tcp   mysql57

4.2 连接 MySQL 5.7

docker exec -it mysql57 mysql -uroot -pmystrongpwd57

进入 MySQL 命令行,执行 exit 退出。

4.3 连接 MySQL 8.0

docker exec -it mysql80 mysql -uroot -pmystrongpwd80

同样成功进入。

4.4 连接 Redis

docker exec -it redis72 redis-cli -a myredispass

输出提示 Warning: Using a password with '-a' ... may not be safe(仅安全警告,不影响使用),然后进入 127.0.0.1:6379> 提示符。

至此,三个数据库容器均已正常运行。

至此,三个数据库容器均已正常运行。

五、供项目容器连接(网络互通)

数据库容器位于自定义网络 services_dev-network(由 docker-compose 自动创建)。项目容器只需加入同一外部网络,即可通过 容器名 访问。

示例 docker-compose.yml 片段:

networks:
  default:
    external:
      name: services_dev-network

services:
  go-app:
    image: golang:1.26-alpine
    networks:
      - default
    environment:
      DB_HOST: mysql57         # 或 mysql80
      REDIS_HOST: redis72

代码中连接 mysql57:3306redis72:6379 即可。


六、版本选择说明与注意事项

服务版本主流程度生命周期提醒
MySQL 5.75.7.44占约 18.8%,属于遗留系统常用版本已于 2023 年 10 月 EOL,不再有官方安全更新,非必要不建议新项目使用
MySQL 8.08.0.46当前最主流(占比 58%)官方支持将于 2026 年 4 月 30 日 结束,后续需升级到 8.4 LTS
Redis 7.27.2.x稳定且成熟开源分支 Valkey 的基础版本,可放心使用

若为新项目,推荐直接使用 MySQL 8.4 LTS(支持至 2031 年)和 Redis 7.2


七、日常管理命令速查

操作命令
启动所有服务cd ~/docker/services && docker compose up -d
停止所有服务cd ~/docker/services && docker compose down
单独重启 MySQL 8.0docker restart mysql80
查看 MySQL 5.7 日志docker logs -f mysql57
进入 Redis 命令行docker exec -it redis72 redis-cli -a 密码
清理未使用的镜像/容器docker system prune -a
备份数据卷tar -czf mysql57-backup.tar.gz ~/docker/services/data/mysql57

八、总结

至此,我们完成了一套干净、可复用的数据库容器环境:

  • MySQL 5.7 → 端口 3306
  • MySQL 8.0 → 端口 3307
  • Redis 7.2 → 端口 6379

所有数据持久化在 ~/docker/services/data/,容器随系统启动自动运行。后续任何 Go、PHP、Node.js 项目,只需通过 Docker 网络连接这些容器名,即可快速获得多版本数据库支持。


本文命令均在 Ubuntu 26.04 下验证通过,Docker 版本 29.5.2。
如有疑问或交流,欢迎在评论区留言。

]]>
https://www.shuijingwanwq.com/2026/06/01/15331/feed/ 0
WordPress 代码块中 Emoji 显示异常?三步彻底解决 https://www.shuijingwanwq.com/2026/05/25/13575/ https://www.shuijingwanwq.com/2026/05/25/13575/#respond Mon, 25 May 2026 12:49:39 +0000 https://www.shuijingwanwq.com/?p=13575 Post Views: 44

在 WordPress 中展示包含 Emoji 的代码片段时,常常遇到 Emoji 被转成 &#x1f389;<img> 标签的问题。本文记录一次完整的排查与解决过程。

问题现象

在 WordPress 古腾堡编辑器的代码块中,写入如下 PHP 代码,如图1:

在 WordPress 古腾堡编辑器的代码块中,写入如下 PHP 代码,如图1
echo "\n🎉 全部处理完成!\n";
echo "📊 统计:\n";
echo "   已处理新标签: {$processed}\n";
echo "   已跳过已有翻译: {$skipped}\n";
echo "\n✅ 所有标签都已经处理完毕,和手动添加的完全一样!\n";

保存文章后,前端显示的却是:

echo "\n&#x1f389; 全部处理完成!\n";
echo "&#x1f4ca; 统计:\n";
...

或者更糟糕的情况,Emoji 变成了长长的 <img> 标签:

echo "\n<img draggable="false" ... src=".../1f389.svg"> 全部处理完成!\n";

如图2:编辑器内正常显示 Emoji

如图2:编辑器内正常显示 Emoji

如图3:前端代码块中 Emoji 变成 HTML 实体

如图3:前端代码块中 Emoji 变成 HTML 实体

如图8:前端代码块中 Emoji 变成 img 标签

如图8:前端代码块中 Emoji 变成 img 标签

原因分析

这个问题由两个独立机制叠加引起:

  1. 数据库字符集不支持 Emoji
    旧版 MySQL 的 utf8 字符集只支持 3 字节字符,而 Emoji 占用 4 字节。当表或字段是 utf8 时,WordPress 会在保存前自动将 Emoji 转成 HTML 实体(如 🎉&#x1f389;),避免数据库报错。 如图4:查询发现核心表字符集为 utf8_general_ci
  2. 前端输出时的 Emoji 图片替换
    即使数据库已支持 utf8mb4,WordPress 仍默认将 Emoji 字符替换为从 CDN 加载的 SVG 图片(通过 wp_staticize_emoji 过滤器),导致代码块内出现 <img> 标签。
如图4:查询发现核心表字符集为 utf8_general_ci

对于使用 SyntaxHighlighter Evolved 等代码高亮插件的用户,这两种转换都会彻底破坏代码的原样展示。

解决方案

第一步:检查并升级数据库字符集

确保你的 WordPress 数据库使用 utf8mb4 字符集。

  1. 通过 phpMyAdmin 或 SQL 查询当前表的字符集:
   SELECT TABLE_NAME, TABLE_COLLATION
   FROM information_schema.TABLES
   WHERE TABLE_SCHEMA = '你的数据库名';

如图4:查询结果示例(部分表为 utf8_general_ci)

如图4:查询结果示例(部分表为 utf8_general_ci)
  1. 如果 wp_postswp_optionswp_terms 等核心表显示 utf8_general_ci,则需要转换:
-- SELECT TABLE_NAME, TABLE_COLLATION FROM information_schema.TABLES WHERE TABLE_SCHEMA = '你的数据库名'; 的查询结果如下:
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cky_banners','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cky_cookie_categories','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cky_cookies','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cmplz_cookiebanners','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cmplz_cookies','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cmplz_services','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_commentmeta','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_comments','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_links','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_options','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_orgseriesicons','utf8mb4_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_post_views','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_postmeta','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_posts','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_term_relationships','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_term_taxonomy','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_termmeta','utf8mb4_unicode_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_terms','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_usermeta','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_users','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_indexable','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_indexable_hierarchy','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_migrations','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_primary_term','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_seo_links','utf8_general_ci');


-- 需要转换的 SQL 如下:

-- 文章相关表
ALTER TABLE wp_posts CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_postmeta CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 选项表(可能存有包含 Emoji 的选项值)
ALTER TABLE wp_options CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 分类/标签表
ALTER TABLE wp_terms CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_term_taxonomy CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_term_relationships CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;


-- 评论表(如果评论可能包含 Emoji)
ALTER TABLE wp_comments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_commentmeta CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 用户表(可选)
ALTER TABLE wp_users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_usermeta CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 友情链接表(如果有)
ALTER TABLE wp_links CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

ALTER TABLE wp_yoast_seo_links CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

如图5:执行 ALTER TABLE 转换

如图5:执行 ALTER TABLE 转换

如图6:执行 ALTER TABLE 转换后的结果
  1. wp-config.php 中强制指定字符集,如图7:
   define('DB_CHARSET', 'utf8mb4');
   define('DB_COLLATE', 'utf8mb4_unicode_ci');
在 wp-config.php 中强制指定字符集,如图7:

⚠️ 注意:操作前务必备份数据库!

第二步:禁用前端 Emoji 图片替换

在主题的 functions.php 中添加以下代码:

// 彻底禁用 WordPress 前端 Emoji 转换为图片
remove_filter( 'the_content', 'wp_staticize_emoji' );
remove_filter( 'the_excerpt', 'wp_staticize_emoji' );
remove_filter( 'comment_text', 'wp_staticize_emoji' );

// 同时移除 Emoji 的 JS/CSS(提升加载速度)
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );

如图10:在 functions.php 中添加禁用代码

如图10:在 functions.php 中添加禁用代码

第三步:清理已有文章中的异常内容

  • 新文章:按以上两步配置后,直接使用 Emoji 保存即可正常显示。
  • 旧文章:如果之前已被转换成 HTML 实体或 <img> 标签,需要手动重新编辑,将实体(如 &#x1f389;)替换回原始 Emoji 字符(🎉),然后更新文章。

如图9:在古腾堡编辑器中替换实体为 Emoji

如图9:在古腾堡编辑器中替换实体为 Emoji

如图11:最终前端显示效果(Emoji 原样展示)

如图11:最终前端显示效果(Emoji 原样展示)

总结

现象原因解决方法
保存后 Emoji 变成 &#x...;数据库字符集不是 utf8mb4升级表为 utf8mb4
前端显示 <img> 标签wp_staticize_emoji 过滤器在 functions.php 中移除该过滤器

通过以上三步,你的 WordPress 站点即可完美在代码块中展示包含 Emoji 的代码片段,不再被任何转换干扰。如果使用的是 SyntaxHighlighter Evolved 或其他代码高亮插件,同样适用本方案。


本文基于真实问题排查记录整理,所有操作已在 WordPress 7.x + PHP 8.x 环境下验证通过。

]]>
https://www.shuijingwanwq.com/2026/05/25/13575/feed/ 0
在 Windows 10 中更新 MySQL 版本,5.7 更新至 8.0 https://www.shuijingwanwq.com/2024/02/05/8436/ https://www.shuijingwanwq.com/2024/02/05/8436/#respond Mon, 05 Feb 2024 01:56:18 +0000 https://www.shuijingwanwq.com/?p=8436 Post Views: 234

1、下载 MySQL 8.0 的安装包后,开始安装,过程中提示:Select Products To Upgrade。Using this wizard you will be able to update your installed products 。Upgradeable Products。MySQL Server 可以从 5.7.19 升级至 5.7.44。不能够升级至 8.0。如图1

下载 MySQL 8.0 的安装包后,开始安装,过程中提示:Select Products To Upgrade。Using this wizard you will be able to update your installed products 。Upgradeable Products。MySQL Server 可以从 5.7.19 升级至 5.7.44。不能够升级至 8.0

图1

2、决定先备份数据库,然后卸载 MySQL 5.7,再安装 8.0。将所有以 MySQL 开头的软件全部卸载。如图2

决定先备份数据库,然后卸载 MySQL 5.7,再安装 8.0。将所有以 MySQL 开头的软件全部卸载

图2

3、安装过程中,不再提示软件升级,而是提示全新安装的选项,选择 full 。如图3

安装过程中,不再提示软件升级,而是提示全新安装的选项,选择 full

图3

4、提示:以下产品将被安装。符合预期。如图4

提示:以下产品将被安装。符合预期

图4

]]>
https://www.shuijingwanwq.com/2024/02/05/8436/feed/ 0
在 MySQL 5.7 中清空表时,提示:1206 – The total number of locks exceeds the lock tablesize https://www.shuijingwanwq.com/2023/12/15/8282/ https://www.shuijingwanwq.com/2023/12/15/8282/#respond Fri, 15 Dec 2023 02:31:46 +0000 https://www.shuijingwanwq.com/?p=8282 Post Views: 283 1、在 MySQL 5.7 中清空表时,表中有 300 多万条记录,清空至还剩下 200 多万记录时,提示:1206 – The total number of locks exceeds the lock tablesize。如图1
在 MySQL 5.7 中清空表时,表中有 300 多万条记录,清空至还剩下 200 多万记录时,提示:1206 - The total number of locks exceeds the lock tablesize

图1

2、参考: THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE。 3、这个问题可以通过设置 MySQL 变量“innodb_buffer_pool_size”的较高值来解决。 innodb_buffer_pool_size 的默认值为 8,388,608。在 Windows 中的位置在:”C:\ProgramData\MySQL\MySQL Server 5.7\my.ini” 。 4、编辑 C:\ProgramData\MySQL\MySQL Server 5.7\my.ini ,


# innodb_buffer_pool_size=8M
innodb_buffer_pool_size=16M


5、在计算机管理 – 服务和应用程度 – 服务,找到 MySQL57,重新启动。如图2
在计算机管理 - 服务和应用程度 - 服务,找到 MySQL57,重新启动

图2

6、Windows无法停止MySQL57服务(位于本地计算机上)。错误1053:服务没有及时响应启动或控制请求。最终决定重启计算机。如图3
Windows无法停止MySQL57服务(位于本地计算机上)。错误1053:服务没有及时响应启动或控制请求。最终决定重启计算机

图3

7、在 MySQL 5.7 中再次清空表,清空成功。如图4
在 MySQL 5.7 中再次清空表,清空成功

图4

]]>
https://www.shuijingwanwq.com/2023/12/15/8282/feed/ 0
在 MySQL 5.7 中,如何在 json 字段上查询同一层级结构中的多个相同字段的值? https://www.shuijingwanwq.com/2023/11/22/8220/ https://www.shuijingwanwq.com/2023/11/22/8220/#respond Wed, 22 Nov 2023 01:39:52 +0000 https://www.shuijingwanwq.com/?p=8220 Post Views: 65 1、在 json 字段中的值如下所示。需要查询的字段:$.current.sections.announcement-bar.blocks.*.settings.text。其中 * 所对应的字段 key 是未知的。如图1
在 json 字段中的值如下所示。需要查询的字段:$.current.sections.announcement-bar.blocks.*.settings.text。其中 * 所对应的字段 key 是未知的

图1

<pre class="wp-block-syntaxhighlighter-code">

{
  "current": {
    "sections": {
      "announcement-bar": {
        "type": "announcement-bar",
        "blocks": {
          "announcement-bar-0": {
            "type": "announcement",
            "disabled": false,
            "settings": {
              "link": "/",
              "text": "❤Free Shipping Over $100.0❤",
              "image": null,
              "text_color": "#ffffff",
              "mobile_text": "",
              "mobile_image": null,
              "background_color": "#000000"
            }
          },
          "6oaZmAscmONct-2pLAp5O": {
            "type": "announcement",
            "disabled": false,
            "settings": {
              "link": "/",
              "text": "<p>❤Free Shipping Over $100.0❤ 1</p>",
              "image": null,
              "text_color": "#ffffff",
              "mobile_text": "",
              "mobile_image": null,
              "background_color": "#000000"
            }
          },
          "JrFCMGW-EBnVZQ3sQT-WP": {
            "type": "announcement",
            "disabled": false,
            "settings": {
              "link": "/",
              "text": "<p>❤Free Shipping Over $100.0❤ 2</p>",
              "image": null,
              "text_color": "#ffffff",
              "mobile_text": "",
              "mobile_image": null,
              "background_color": "#000000"
            }
          }
        },
        "disabled": false,
        "settings": {
          "sticky": false,
          "homepage_only": false
        },
        "block_order": [
          "announcement-bar-0",
          "6oaZmAscmONct-2pLAp5O",
          "JrFCMGW-EBnVZQ3sQT-WP"
        ]
      }
    },
    "radius__image": 5,
    "radius__button": 6
  }
}

</pre>
2、最后整理的 SQL 如下,查询结果为数组,如果不存在,则为 NULL。如图2
最后整理的 SQL 如下,查询结果为数组,如果不存在,则为 NULL

图2



SELECT
	JSON_EXTRACT( `schema`, '$.current.sections."announcement-bar".blocks.*.settings.text' ) 
FROM
	`theme_asset2` 
WHERE
	`theme_id` = '9a1ce422-a2cc-4559-9c9b-2edd1c50db87' 
	AND `asset_key` = 'config/settings_data.json'


<pre class="wp-block-syntaxhighlighter-code">

["❤Free Shipping Over $100.0❤", "<p>❤Free Shipping Over $100.0❤ 1</p>", "<p>❤Free Shipping Over $100.0❤ 2</p>"]

</pre>
3、announcement-bar 需要加上双引号,否则会报错:Invalid JSON path expression. The error is around character position 35.。如图3
announcement-bar 需要加上双引号,否则会报错:Invalid JSON path expression. The error is around character position 35.

图3

]]>
https://www.shuijingwanwq.com/2023/11/22/8220/feed/ 0
在操作 MySQL 5.7 中的 json 字段时,并发执行(查询、修改、重新赋值)时数据丢失的问题处理(使用修改 JSON 值的函数 JSON_ARRAY_APPEND) https://www.shuijingwanwq.com/2023/10/07/8085/ https://www.shuijingwanwq.com/2023/10/07/8085/#respond Sat, 07 Oct 2023 01:39:45 +0000 https://www.shuijingwanwq.com/?p=8085 Post Views: 108 1、json 字段中存储的是数组格式,值示例:[249, 247, 250, 244, 243, 255, 246, 245, 257, 248, 259, 252, 256]。如图1
json 字段中存储的是数组格式,值示例:[249, 247, 250, 244, 243, 255, 246, 245, 257, 248, 259, 252, 256]

图1

2、现有的在队列作业中运行的代码实现如下,生成的 SQL 如下


$themeSaasTask = ThemeSaasTask::whereJsonContains('theme_task_ids', $this->themeInstallationTask->id)->first();
$fromThemeInstallationIds = json_decode($themeSaasTask->from_theme_installation_ids, true);
$fromThemeInstallationIds[] = $this->themeInstallation->originalThemeInstallation->id;
$themeSaasTask->from_theme_installation_ids = json_encode($fromThemeInstallationIds);
$themeSaasTask->updated_at = now()->utc()->toDateTimeString();
$themeSaasTask->save();




select * from `theme_saas_task` where json_contains(`theme_task_ids`, '393') limit 1

update `theme_saas_task` set `from_theme_installation_ids` = '[380]', `theme_saas_task`.`updated_at` = '2023-08-21 03:10:57' where `id` = 23


3、由于队列任务可以同时运行,在并发执行时,会导致需要添加至 json 字段中的值丢失。丢失了 [251, 253, 254, 258]。由于有大量的查询修改保存操作皆在同一时间点 2023-06-20 06:36:27 触发,进而导致了数据丢失。如图2
由于队列任务可以同时运行,在并发执行时,会导致需要添加至 json 字段中的值丢失。丢失了 [251, 253, 254, 258]。由于有大量的查询修改保存操作皆在同一时间点 2023-06-20 06:36:27 触发,进而导致了数据丢失

图2

4、参考:12.17.4 修改 JSON 值的函数 JSON_ARRAY_APPEND  代码实现如下,生成的 SQL 如下 。如图3
参考:12.17.4 修改 JSON 值的函数 JSON_ARRAY_APPEND

图3



$themeSaasTask->from_theme_installation_ids = DB::raw('JSON_ARRAY_APPEND(from_theme_installation_ids, "$", ' . $this->themeInstallation->originalThemeInstallation->id . ')');
$themeSaasTask->updated_at = now()->utc()->toDateTimeString();
$themeSaasTask->save();




update
  `theme_saas_task`
set
  `from_theme_installation_ids` = JSON_ARRAY_APPEND(from_theme_installation_ids, "$", 403),
  `theme_saas_task`.`updated_at` = '2023-08-22 02:39:12'
where
  `id` = 30


5、除了往 json 数组中追加值以外,还存在往 json 数组中删除值的情况,现有的实现如下


$themeSaasTask = ThemeSaasTask::whereJsonContains('theme_task_ids', $this->themeInstallationTask->id)->first();
if ($this->themeInstallation->type == ThemeInstallation::TYPE_UPDATE && $themeSaasTask) {
	$themeInstallationTaskIds = json_decode($themeSaasTask->theme_task_ids, true);
	$themeInstallationTaskIds = array_values(array_diff($themeInstallationTaskIds, [$this->themeInstallationTask->id]));
	if (empty($themeInstallationTaskIds)) {
		ThemeSaasTask::where('id', $themeSaasTask->id)->delete();
	} else {
		$themeSaasTask->theme_task_ids = json_encode($themeInstallationTaskIds);
		$themeSaasTask->save();
	}
}


6、当在队列中并发运行时,字段 theme_task_ids 中总计有 17 个值,理论上来说,执行了 17 次以后,`id` = 22 应该会被删除掉。但是,实际上,字段 theme_task_ids 中还剩下 [380, 381, 382, 383, 385, 386, 389, 394]。执行的 SQL 如下: 如图4
当在队列中并发运行时,字段 theme_task_ids 中总计有 17 个值,理论上来说,执行了 17 次以后,`id` = 22 应该会被删除掉。但是,实际上,字段 theme_task_ids 中还剩下 [380, 381, 382, 383, 385, 386, 389, 394]

图4



update `theme_saas_task` set `theme_task_ids` = '[379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395]', `theme_saas_task`.`updated_at` = '2023-08-23 01:54:15' where `id` = 22;

/* 删除掉 379 */
select * from `theme_saas_task` where json_contains(`theme_task_ids`, '379') limit 1;
update `theme_saas_task` set `theme_task_ids` = '[380,381,382,383,384,385,386,387,388,389,390,394]', `theme_saas_task`.`updated_at` = '2023-08-23 02:01:39' where `id` = 22; 

/* 只需要删除掉 380,但是 379 又被添加 */
select * from `theme_saas_task` where json_contains(`theme_task_ids`, '380') limit 1;
update `theme_saas_task` set `theme_task_ids` = '[379,381,382,383,384,385,386,387,388,389,390,394]', `theme_saas_task`.`updated_at` = '2023-08-23 02:01:39' where `id` = 22;

/* 只需要删除掉 381,但是 379,380 又被添加 */
select * from `theme_saas_task` where json_contains(`theme_task_ids`, '381') limit 1;
update `theme_saas_task` set `theme_task_ids` = '[379,380,382,383,384,385,386,387,388,389,390,394]', `theme_saas_task`.`updated_at` = '2023-08-23 02:01:39' where `id` = 22;

/* 只需要删除掉 384,但是 380,381 又被添加 */
select * from `theme_saas_task` where json_contains(`theme_task_ids`, '384') limit 1;
update `theme_saas_task` set `theme_task_ids` = '[380,381,382,383,385,386,387,388,389,390,394]', `theme_saas_task`.`updated_at` = '2023-08-23 02:01:40' where `id` = 22;



7、参考:Mysql json 数组删除数据  。发现 JSON_SEARCH 的参数 search_str 暂不支持数字。如图5
发现 JSON_SEARCH 的参数 search_str 暂不支持数字

图5



SELECT
	JSON_SEARCH( theme_task_ids, 'all', '375' ) 
FROM
	theme_saas_task 
WHERE
	id = 21;


8、最终决定在插入到字段 theme_task_ids 中的值时,先转换为字符串。在队列作业中运行的代码调整实现如下


$themeInstallationTaskId = '' . $this->themeInstallationTask->id . '';
$themeSaasTask = ThemeSaasTask::whereJsonContains('theme_task_ids', $themeInstallationTaskId)->first();

$themeSaasTask->theme_task_ids = DB::raw('JSON_REMOVE(theme_task_ids,	JSON_UNQUOTE(JSON_SEARCH(theme_task_ids, "one", ' . $themeInstallationTaskId . ')))');
$themeSaasTask->save();




update
  `theme_saas_task`
set
  `theme_task_ids` = '[\"516\",\"517\",\"518\",\"519\",\"520\",\"521\",\"522\",\"523\",\"524\",\"525\",\"526\",\"527\",\"528\",\"529\",\"530\",\"531\",\"532\"]',
  `theme_saas_task`.`updated_at` = '2023-08-23 08:33:57'
where
  `id` = 30;

"JSON_REMOVE(theme_task_ids, JSON_UNQUOTE(JSON_SEARCH(theme_task_ids, \"one\", 519)))";


9、但是在并发情况下,仍然存在一些值未被删除掉,总计 17 个,删除了 12 个,还剩下 5 个。如图6
但是在并发情况下,仍然存在一些值未被删除掉,总计 17 个,删除了 12 个,还剩下 5 个

图6

10、由于这一段代码运行的频率很低,最终决定添加锁实现


        $themeSaasTask = ThemeSaasTask::whereJsonContains('theme_task_ids', $this->themeInstallationTask->id)->first();

        if ($this->themeInstallation->type == ThemeInstallation::TYPE_UPDATE && $themeSaasTask) {

            app(ThemeManager::class)->cleanTheme($this->themeInstallation, $this->themeInstallation->theme);

            DB::beginTransaction();
            $themeSaasTask = ThemeSaasTask::lockForUpdate()->find($themeSaasTask->id);
            $themeInstallationTaskIds = json_decode($themeSaasTask->theme_task_ids, true);
            Log::info(
                '$themeInstallationTaskIds3-'.$this->themeInstallationTask->id,
                [$themeInstallationTaskIds]
            );
            $themeInstallationTaskIds = array_values(array_diff($themeInstallationTaskIds, [$this->themeInstallationTask->id]));
            if (empty($themeInstallationTaskIds)) {
                ThemeSaasTask::where('id', $themeSaasTask->id)->delete();
            } else {
                $themeSaasTask->theme_task_ids = json_encode($themeInstallationTaskIds);
                Log::info(
                    '$themeSaasTask->theme_task_ids3-'.$this->themeInstallationTask->id,
                    [$themeSaasTask->theme_task_ids]
                );
                $themeSaasTask->save();
            }
            DB::commit();
        }


11、最终确认此方案存在问题,最后剩下 [567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581]。期望为 空数组。通过日志记录发现,在查询时,查询出的数据不符合预期,减少了许多,分别打印出第 1 次与 第 17 次的日志。


[2023-08-24 10:10:06] local.INFO: $themeInstallationTaskIds3-567 [
    [
        567,
        568,
        569,
        570,
        571
    ]
] 
[2023-08-24 10:10:06] local.INFO: $themeSaasTask->theme_task_ids3-567 [
    "[568,569,570,571]"
] 

[2023-08-24 10:10:09] local.INFO: $themeInstallationTaskIds3-583 [
    [
        567,
        568,
        569,
        570,
        571,
        572,
        573,
        574,
        575,
        576,
        577,
        578,
        579,
        580,
        581,
        583
    ]
] 
[2023-08-24 10:10:09] local.INFO: $themeSaasTask->theme_task_ids3-583 [
    "[567,568,569,570,571,572,573,574,575,576,577,578,579,580,581]"
]


12、最终找到根源,根源在于同一条记录,在另一个地方在不断地向字段 theme_task_ids 添加值,添加完毕后,就立即入队列,然后在队列中在不断地从字段 theme_task_ids 中删除值。决定向字段 theme_task_ids 添加值完毕后,再统一批量入队列。 调整前的实现:


foreach ($list as $theme) {
	$themeTaskIds = json_decode($themeSaasTask->theme_task_ids, true);
	$themeTaskIds[] = $themeInstallationTask->id;
	$themeSaasTask->theme_task_ids = json_encode($themeTaskIds);
	$themeSaasTask->updated_at = now()->utc()->toDateTimeString();
	$themeSaasTask->save();
	// 推送任务到队列
	InstallThemeToDb::dispatch($themeInstallationTask)->onQueue('longtime');
}


调整后的实现:


$themeInstallationTasks = [];
foreach ($list as $theme) {
	$themeTaskIds = json_decode($themeSaasTask->theme_task_ids, true);
	$themeTaskIds[] = $themeInstallationTask->id;
	$themeSaasTask->theme_task_ids = json_encode($themeTaskIds);
	$themeSaasTask->updated_at = now()->utc()->toDateTimeString();
	$themeSaasTask->save();


	$themeInstallationTasks[] = $themeInstallationTask;
}

foreach ($themeInstallationTasks as $themeInstallationTask) {
	// 推送任务到队列
	InstallThemeToDb::dispatch($themeInstallationTask)->onQueue('longtime');
}


13、确认调整后,第 12 步骤的方案可行,最后剩下 []。期望为 空数组,符合预期。分别打印出第 1 次与 第 16 次、 第 17 次的日志。


[2023-08-24 13:42:24] local.INFO: $themeInstallationTaskIds6-635 [
    [
        635,
        636,
        637,
        638,
        639,
        640,
        641,
        642,
        643,
        644,
        645,
        646,
        647,
        648,
        649,
        650,
        651
    ]
] 
[2023-08-24 13:42:24] local.INFO: $themeSaasTask->theme_task_ids6-635 [
    "[636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651]"
]  

[2023-08-24 13:42:25] local.INFO: $themeInstallationTaskIds6-650 [
    [
        650,
        651
    ]
] 
[2023-08-24 13:42:25] local.INFO: $themeSaasTask->theme_task_ids6-650 [
    "[651]"
] 

[2023-08-24 13:42:26] local.INFO: $themeInstallationTaskIds6-651 [
    [
        651
    ]
] 


]]>
https://www.shuijingwanwq.com/2023/10/07/8085/feed/ 0
在 MySQL 5.7 中,如何在 json 字段上使用 JOIN 来实现关联查询? https://www.shuijingwanwq.com/2023/09/27/8078/ https://www.shuijingwanwq.com/2023/09/27/8078/#respond Wed, 27 Sep 2023 01:29:32 +0000 https://www.shuijingwanwq.com/?p=8078 Post Views: 73 1、现有表 theme_table1 ,其中字段 theme_table2_ids 为 json 类型,值为数组,数组中的值关联了表 theme_table2 的主键ID。如图1
现有表 theme_table1 ,其中字段 theme_table2_ids 为 json 类型,值为数组,数组中的值关联了表 theme_table2 的主键ID

图1

2、之前编写过查询某一条 theme_table2 中的 id 是否存在于表 theme_table1 中。SQL 实现如下


select * from `theme_table1` where json_contains(`theme_table2_ids`, '391') limit 1


3、现在需要查询出有哪些表 theme_table2 中的记录存在于 表 theme_table1 的 字段 theme_table2_ids 中。SQL 实现如下,如图2
现在需要查询出有哪些表 theme_table2 中的记录存在于 表 theme_table1 的 字段 theme_table2_ids 中。SQL 实现如下

图2



SELECT
	`theme_table1`.theme_table2_ids,
	`theme_table2`.id,
	`theme_table2`.processing,
	`theme_table2`.processing_failed 
FROM
	`theme_table1`
	INNER JOIN `theme_table2` ON JSON_CONTAINS(
		`theme_table1`.theme_table2_ids,
	CONVERT ( `theme_table2`.id, CHAR )) = 1 
WHERE
	`processing` = 0 
	AND `processing_failed` = 1


]]>
https://www.shuijingwanwq.com/2023/09/27/8078/feed/ 0
在 MySQL 5.7 中,在两张关联表中,查询在一张表中存在,但在另一张表中不存在的数据 https://www.shuijingwanwq.com/2023/09/21/8063/ https://www.shuijingwanwq.com/2023/09/21/8063/#respond Thu, 21 Sep 2023 01:31:36 +0000 https://www.shuijingwanwq.com/?p=8063 Post Views: 61 1、在表 table1 中的字段 table2_id 关联了 表 table2 的主键字段 id。现在需要查询在表 table1 中存在,但在表 table2 中不存在的数据。最终实现的 SQL 如下。如图1
在表 table1 中的字段 table2_id 关联了 表 table2 的主键字段 id。现在需要查询在表 table1 中存在,但在表 table2 中不存在的数据。最终实现的 SQL 如下

图1



SELECT
	* 
FROM
	`db1_20230810`.`table1` 
WHERE
	NOT EXISTS ( SELECT * FROM `db2_20230810`.`table2` WHERE ( `table2`.`id` = `table1`.`table2_id` ) AND ( `table2`.`deleted_at` IS NULL ) ) 
	AND ( `table1`.`role` != 'development' ) 
	AND (
	`table1`.`deleted_at` IS NULL 
	)


]]>
https://www.shuijingwanwq.com/2023/09/21/8063/feed/ 0
删除 MySQL 中的表时,报错:1217 – Cannot delete or update a parent row: a foreign key constraaint fail。如何查找此表被哪些表的外键引用? https://www.shuijingwanwq.com/2023/09/19/8057/ https://www.shuijingwanwq.com/2023/09/19/8057/#respond Tue, 19 Sep 2023 01:19:00 +0000 https://www.shuijingwanwq.com/?p=8057 Post Views: 53

1、删除 MySQL 中的表时,报错:1217 – Cannot delete or update a parent row: a foreign key constraaint fail。如图1

删除 MySQL 中的表时,报错:1217 - Cannot delete or update a parent row: a foreign key constraaint fail

图1

2、参考:您可以从INFORMATION_SCHEMA.KEY_COLUMN_USAGEtable 中获取有关外键的信息。此处显示了针对该 table 的查询示例  。如图2

参考:您可以从INFORMATION_SCHEMA.KEY_COLUMN_USAGEtable 中获取有关外键的信息。此处显示了针对该 table 的查询示例

图2

3、查询结果中包含了当前连接的所有外键信息。找到相对应的数据库、表及字段。如图3

查询结果中包含了当前连接的所有外键信息。找到相对应的数据库、表及字段

图3

4、先删除掉查询结果中列 TABLE_NAME 所对应的表,然后再删除此表。删除成功。

]]>
https://www.shuijingwanwq.com/2023/09/19/8057/feed/ 0
部署 Shopify PHP 主题应用扩展至生产环境的应用中(阿里云、ECS 中的 CentOS 7.7 64位、MySQL 5.7) https://www.shuijingwanwq.com/2023/07/20/7878/ https://www.shuijingwanwq.com/2023/07/20/7878/#respond Thu, 20 Jul 2023 01:42:15 +0000 https://www.shuijingwanwq.com/?p=7878 Post Views: 157

1、参考:部署 Shopify PHP 应用至生产环境(阿里云、ECS 中的 CentOS 7.7 64位、MySQL 5.7)

2、当在应用:learn-php-app-ubuntu-2004-0605 新建且部署主题应用扩展后。测试通过,符合预期。如图1

当在应用:learn-php-app-ubuntu-2004-0605 新建且部署主题应用扩展后。测试通过,符合预期

图1

3、上传本地环境中的应用:learn-php-app-ubuntu-2004-0605 中 web 下的代码至生产环境,目录:vendor、frontend/node_modules、frontend/dist 不上传。

4、上传代码至生产环境后,重新构建前端与后端。

[root@iZ23wv7v5ggZ frontend]# npm install
[root@iZ23wv7v5ggZ frontend]# SHOPIFY_API_KEY=c7f826670575f7ae069f7e56350465ef npm run build
[root@iZ23wv7v5ggZ frontend]# cd ..
[root@iZ23wv7v5ggZ learn-php-app-0605-prod.shuijingwanwq.com]# composer build
> composer build-frontend-links
> ln -sf ../frontend/dist/assets public/assets && ln -sf ../frontend/dist/index.html public/index.html

5、在开发商店中测试生产环境的应用,可正常工作。

6、由于主题应用扩展需要基于 Shopify CLI 创建,但是生产环境中没有安装 Shopify CLI。创建与部署主题应用扩展只能够在本地环境中执行。只不过需要绑定的应用为生产环境中的应用(即合作伙伴中心的应用:learn-php-app-0605-prod)。复制learn-php-app-ubuntu-2004-0605 中的代码至目录:learn-php-app-0605-prod,目录:node_modules、web/vendor、web/frontend/node_modules、web/frontend/dist 不复制。最佳实践应该是这2个应用目录共用同一个 Git 仓库,以保持代码的同步。参考:Work on an existing app  。安装项目的依赖。

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod$  sudo npm install

7、安装应用的依赖项后,检查版本以确保 Shopify CLI 可用。

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod$ npm run shopify version

> learn-php-app-ubuntu-2004-0605@1.0.0 shopify
> shopify version

Current Shopify CLI version: 3.46.2
💡 Version 3.46.5 available! Run npm run shopify upgrade

8、运行 dev 命令,在第一次运行此命令时,将其连接到合作伙伴中心的现有应用程序:learn-php-app-0605-prod,选择对应的商店以预览应用,且不更新应用的 URL,以避免影响到生产环境的应用使用。如图2

运行 dev 命令,在第一次运行此命令时,将其连接到合作伙伴中心的现有应用程序:learn-php-app-0605-prod,选择对应的商店以预览应用,且不更新应用的 URL,以避免影响到生产环境的应用使用

图2

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod$ npm run dev

> learn-php-app-ubuntu-2004-0605@1.0.0 dev
> shopify app dev


Looks like this is the first time you're running dev for this project.
Configure your preferences by answering a few questions.


Before you preview your work, it needs to be associated with an app.

?  Create this project as a new app on Shopify?

>  (y) Yes, create it as a new app
   (n) No, connect it to an existing app

✔  No, connect it to an existing app

?  Which existing app is this for?

>  learn-php-app-0605-prod
   learn-php-app-ubuntu-2004-0605
   app-partners-20230530
   theme-app-ext-example1
   theme-app-extension-example
   learn-node-app-20230524
   learn-php-app-20230510
   learn-node-app-20230509
   learn-app-20221019
   learn-app

✔  learn-php-app-0605-prod

?  Which store would you like to use to view your project?

>  shuijingwanwq-development
   shuijingwanwq-development3
   shuijingwanwq-development2

✔  shuijingwanwq-development

?  Have Shopify automatically update your app's URL in order to create a preview experience?

       Current app URL:        • https://learn-php-app-0605-prod.shuijingwanwq.com/

       Current redirect URLs:  • https://learn-php-app-0605-prod.shuijingwanwq.com/api/auth/callback

   (1) Always by default
   (2) Yes, this time
   (3) No, not now
>  (4) Never, don't ask again

✔  Never, don't ask again

╭─ info ───────────────────────────────────────────────────────────────────────╮
│                                                                              │
│  To make URL updates manually, you can add the following URLs as redirects   │
│  in your Partners Dashboard [1]:                                             │
│                                                                              │
│                                                                              │
│    • https://tiger-foto-incident-sparc.trycloudflare.com/auth/callback       │
│    • https://tiger-foto-incident-sparc.trycloudflare.com/auth/shopify/callb  │
│      ack                                                                     │
│    • https://tiger-foto-incident-sparc.trycloudflare.com/api/auth/callback   │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
[1] https://partners.shopify.com/2442779/apps/44885082113/edit

theme-app-extension-0612 (Theme App Extension)
Follow the dev doc instructions ( https://shopify.dev/apps/online-store/theme-app-extensions/getting-started#step-3-test-your-changes ) by deploying your work as a draft

2023-06-15 03:17:46 │ frontend   │
2023-06-15 03:17:46 │ frontend   │ > shopify-frontend-template-react@1.0.0 dev
2023-06-15 03:17:46 │ frontend   │ > vite
2023-06-15 03:17:46 │ frontend   │
2023-06-15 03:17:47 │ backend    │ > Composer\Config::disableProcessTimeout
2023-06-15 03:17:47 │ backend    │ > php artisan serve
2023-06-15 03:17:47 │ backend    │ PHP Warning:  require(/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/vendor/autoload.php): Failed to open
                                   stream: No such file or directory in /mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/artisan on line 18
2023-06-15 03:17:47 │ backend    │ PHP Fatal error:  Uncaught Error: Failed opening required
                                   '/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/vendor/autoload.php' (include_path='.:/usr/share/php') in
                                   /mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/artisan:18
2023-06-15 03:17:47 │ backend    │ Stack trace:
2023-06-15 03:17:47 │ backend    │ #0 {main}
2023-06-15 03:17:47 │ backend    │   thrown in /mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/artisan on line 18
2023-06-15 03:17:47 │ backend    │ Script php artisan serve handling the serve event returned with error code 255

Preview URL: https://tiger-foto-incident-sparc.trycloudflare.com?shop=shuijingwanwq-development.myshopify.com&host=c2h1a
Wppbmd3YW53cS1kZXZlbG9wbWVudC5teXNob3BpZnkuY29tL2FkbWlu

── external error ──────────────────────────────────────────────────────────────

Error coming from `composer serve`

Command failed with exit code 255: composer serve
> Composer\Config::disableProcessTimeout
> php artisan serve
PHP Warning:  require(/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/vendor/autoload.php): Failed to open
stream: No such file or directory in /mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/artisan on line 18
PHP Fatal error:  Uncaught Error: Failed opening required
'/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/vendor/autoload.php' (include_path='.:/usr/share/php') in
/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/artisan:18
Stack trace:
#0 {main}
  thrown in /mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web/artisan on line 18
Script php artisan serve handling the serve event returned with error code 255

────────────────────────────────────────────────────────────────────────────────

9、执行命令:composer serve 时报错,因为 PHP 的依赖项还未安装。不过,主题应用扩展:theme-app-extension-0612 已经推送至合作伙伴中心。如图3

执行命令:composer serve 时报错,因为 PHP 的依赖项还未安装。不过,主题应用扩展:theme-app-extension-0612 已经推送至合作伙伴中心

图3

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/web$ composer install

10、启用开发商店预览后,执行 deploy 命令后,可以创建扩展版本,然后安装过此应用的店铺便可以使用此主题应用扩展。无法创建扩展版本,不符合预期。如图4

启用开发商店预览后,执行 deploy 命令后,可以创建扩展版本,然后安装过此应用的店铺便可以使用此主题应用扩展。无法创建扩展版本,不符合预期

图4

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod$ npm run deploy

> learn-php-app-ubuntu-2004-0605@1.0.0 deploy
> shopify app deploy

?  Make the following changes to your extensions in Shopify Partners?
✔  Yes, deploy to push changes


Deploying your work to Shopify Partners. It will be part of learn-php-app-ubuntu-2004-0605

 theme_extensions │ Running theme check on your Theme app extension...
 theme_extensions │ Checking /mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/extensions/theme-app-extension-0612 ...
 theme_extensions │ 4 files inspected, 0 offenses detected, 0 offenses auto-correctable


╭─ success ────────────────────────────────────────────────────────────────────╮
│                                                                              │
│  Deployed to Shopify!                                                        │
│                                                                              │
│  Summary                                                                     │
│    • theme-app-extension-0612 is deployed to Shopify but not yet live        │
│                                                                              │
│  Next steps                                                                  │
│    • Publish theme-app-extension-0612 [1]                                    │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
[1] https://partners.shopify.com/2442779/apps/44874072065/extensions/theme_app_extension/25583255553

11、发现仍然是被推送部署至复制前的应用 learn-php-app-ubuntu-2004-0605 。如图5

发现仍然是被推送部署至复制前的应用 learn-php-app-ubuntu-2004-0605

图5

12、重新运行开发(重新配置)命令。在执行过程中,存在:Pushed » ‘theme-app-extension-0612’ to a draft。已经可以创建应用扩展的版本。如图6

重新运行开发(重新配置)命令。在执行过程中,存在:Pushed » 'theme-app-extension-0612' to a draft。已经可以创建应用扩展的版本

图6

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod$ npm run dev -- --reset

> learn-php-app-ubuntu-2004-0605@1.0.0 dev
> shopify app dev --reset


Before you preview your work, it needs to be associated with an app.

?  Create this project as a new app on Shopify?
✔  No, connect it to an existing app

?  Which existing app is this for?
✔  learn-php-app-0605-prod

?  Which store would you like to use to view your project?
✔  shuijingwanwq-development

?  Have Shopify automatically update your app's URL in order to create a preview experience?
✔  Never, don't ask again

╭─ info ───────────────────────────────────────────────────────────────────────╮
│                                                                              │
│  To make URL updates manually, you can add the following URLs as redirects   │
│  in your Partners Dashboard [1]:                                             │
│                                                                              │
│                                                                              │
│    • https://shape-vaccine-crop-xl.trycloudflare.com/auth/callback           │
│    • https://shape-vaccine-crop-xl.trycloudflare.com/auth/shopify/callback   │
│    • https://shape-vaccine-crop-xl.trycloudflare.com/api/auth/callback       │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
[1] https://partners.shopify.com/2442779/apps/44885082113/edit

theme-app-extension-0612 (Theme App Extension)
Follow the dev doc instructions ( https://shopify.dev/apps/online-store/theme-app-extensions/getting-started#step-3-test-your-changes ) by deploying your work as a draft

2023-06-15 05:54:27 │ frontend   │
2023-06-15 05:54:27 │ frontend   │ > shopify-frontend-template-react@1.0.0 dev
2023-06-15 05:54:27 │ frontend   │ > vite
2023-06-15 05:54:27 │ frontend   │
2023-06-15 05:54:29 │ webhooks   │ Sending APP_UNINSTALLED webhook to app server
2023-06-15 05:54:29 │ webhooks   │ App isn't responding yet, retrying in 5 seconds
2023-06-15 05:54:34 │ webhooks   │ App isn't responding yet, retrying in 5 seconds
2023-06-15 05:54:35 │ frontend   │
2023-06-15 05:54:35 │ frontend   │   VITE v4.3.9  ready in 7797 ms
2023-06-15 05:54:35 │ frontend   │
2023-06-15 05:54:35 │ frontend   │   ➜  Local:   http://localhost:33151/
2023-06-15 05:54:36 │ backend    │ > Composer\Config::disableProcessTimeout
2023-06-15 05:54:36 │ backend    │ > php artisan serve
2023-06-15 05:54:39 │ webhooks   │ App isn't responding yet, retrying in 5 seconds
2023-06-15 05:54:44 │ webhooks   │ App hasn't started in time, giving up
2023-06-15 05:54:44 │ webhooks   │ APP_UNINSTALLED webhook delivery failed
2023-06-15 05:54:54 │ backend    │ Starting Laravel development server: http://127.0.0.1:41775
2023-06-15 05:54:54 │ backend    │ [Thu Jun 15 13:54:54 2023] PHP 8.1.18 Development Server (http://127.0.0.1:41775) started
2023-06-15 05:55:21 │ extensions │
2023-06-15 05:55:21 │ extensions │ --------- Pushing theme files to App Ext. Host (96e4f9-DESKTOP-QLPK8QM) (#133838078137) on
                                   shuijingwanwq-development.myshopify.com
2023-06-15 05:55:21 │ extensions │ Pushing theme...     0%
2023-06-15 05:55:33 │ backend    │ [Thu Jun 15 13:55:33 2023] 127.0.0.1:42178 Accepted
2023-06-15 05:55:42 │ backend    │ [Thu Jun 15 13:55:42 2023] 127.0.0.1:42178 Closing
2023-06-15 05:55:42 │ backend    │ [Thu Jun 15 13:55:42 2023] 127.0.0.1:42184 Accepted
2023-06-15 05:55:45 │ backend    │ [Thu Jun 15 13:55:44 2023] 127.0.0.1:42184 Closing
2023-06-15 05:56:25 │ extensions │ Pushing theme...    36%
2023-06-15 05:56:27 │ extensions │ Pushing theme...    52%
2023-06-15 05:56:27 │ extensions │ Pushing theme...    68%
2023-06-15 05:56:27 │ extensions │ Pushing theme...    84%
2023-06-15 05:56:27 │ extensions │ Pushing theme...   100%
2023-06-15 05:56:29 │ extensions │ ------------------
2023-06-15 05:56:31 │ extensions │
2023-06-15 05:56:31 │ extensions │ --------- Viewing extension…
2023-06-15 05:56:31 │ extensions │ Enable your theme app extension:
2023-06-15 05:56:31 │ extensions │ https://partners.shopify.com/2442779/apps/44885082113/extensions/theme_app_extension/25719668737
2023-06-15 05:56:31 │ extensions │
2023-06-15 05:56:31 │ extensions │ Setup your theme app extension in the host theme:
2023-06-15 05:56:31 │ extensions │ https://shuijingwanwq-development.myshopify.com/admin/themes/133838078137/editor
2023-06-15 05:56:31 │ extensions │
2023-06-15 05:56:31 │ extensions │ Preview your theme app extension:
2023-06-15 05:56:31 │ extensions │ http://127.0.0.1:9292
2023-06-15 05:56:31 │ extensions │ ------------------
2023-06-15 05:56:34 │ extensions │ Pushed » 'theme-app-extension-0612' to a draft
2023-06-15 05:56:34 │ extensions │ - blocks/star_rating.liquid
2023-06-15 05:56:34 │ extensions │ - snippets/stars.liquid
2023-06-15 05:56:34 │ extensions │ - locales/en.default.json
2023-06-15 05:56:34 │ extensions │ - assets/thumbs-up.png
2023-06-15 06:39:08 │ extensions │ Stopping…

› Press p │ preview in your browser
› Press q │ quit

Preview URL: https://shape-vaccine-crop-xl.trycloudflare.com?shop=shuijingwanwq-development.myshopify.com&host=c2h1aWppb
md3YW53cS1kZXZlbG9wbWVudC5teXNob3BpZnkuY29tL2FkbWlu

13、应用:learn-php-app-0605-prod 已经被分发至店铺:shuijingwanwq-development3。如图7

应用:learn-php-app-0605-prod 已经被分发至店铺:shuijingwanwq-development3

图7

14、创建主题应用扩展的版本并发布:1.0.0。如图8

创建主题应用扩展的版本并发布:1.0.0

图8

15、打开店铺:shuijingwanwq-development3 的任一主题的编辑器,可通过添加应用块的方式,使用主题应用扩展。如图9

打开店铺:shuijingwanwq-development3 的任一主题的编辑器,可通过添加应用块的方式,使用主题应用扩展

图9

16、查看首页,主题应用扩展已经显示。如图10

查看首页,主题应用扩展已经显示

图10

17、更新主题扩展应用的代码后,执行部署命令。发现仍然是部署至复制前的应用:learn-php-app-ubuntu-2004-0605

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod$ npm run deploy

> learn-php-app-ubuntu-2004-0605@1.0.0 deploy
> shopify app deploy

?  Make the following changes to your extensions in Shopify Partners?
✔  Yes, deploy to push changes


Deploying your work to Shopify Partners. It will be part of learn-php-app-ubuntu-2004-0605

 theme_extensions │ Running theme check on your Theme app extension...
 theme_extensions │ Checking /mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/extensions/theme-app-extension-0612 ...
 theme_extensions │ 4 files inspected, 0 offenses detected, 0 offenses auto-correctable


╭─ success ────────────────────────────────────────────────────────────────────╮
│                                                                              │
│  Deployed to Shopify!                                                        │
│                                                                              │
│  Summary                                                                     │
│    • theme-app-extension-0612 is deployed to Shopify but not yet live        │
│                                                                              │
│  Next steps                                                                  │
│    • Publish theme-app-extension-0612 [1]                                    │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
[1] https://partners.shopify.com/2442779/apps/44874072065/extensions/theme_app_extension/25583255553

18、重新执行 env pull 命令,以指定应用为:learn-php-app-0605-prod。以更新现有的应用与应用扩展的环境变量。如图11

重新执行 env pull 命令,以指定应用为:learn-php-app-0605-prod。以更新现有的应用与应用扩展的环境变量

图11

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod$ npm run shopify app env pull

> learn-php-app-ubuntu-2004-0605@1.0.0 shopify
> shopify app env pull

?  Which existing app is this for?
✔  learn-php-app-0605-prod

Updated .env to be:

SHOPIFY_API_KEY=c7f826670575f7ae069f7e56350465ef
SHOPIFY_THEME_APP_EXTENSION_0612_ID=a680449a-9537-45bf-a010-4d8cadacf5e1

SHOPIFY_API_SECRET=4de64aa8281e97055cf7698bbe56039e
SCOPES=write_products

Here's what changed:

- SHOPIFY_API_KEY=02c894a155c74ca837babfcf043ae4ed
+ SHOPIFY_API_KEY=c7f826670575f7ae069f7e56350465ef
SHOPIFY_THEME_APP_EXTENSION_0612_ID=a680449a-9537-45bf-a010-4d8cadacf5e1
+ SHOPIFY_API_SECRET=4de64aa8281e97055cf7698bbe56039e
+ SCOPES=write_products

19、再次执行部署命令,然后创建新的扩展版本:1.2.0 并且发布。等待了大约 3 分钟左右,首页发生变化,已经应用到新的更改,从 0615 变化为 0616。如图12

再次执行部署命令,然后创建新的扩展版本:1.2.0 并且发布。等待了大约 3 分钟左右,首页发生变化,已经应用到新的更改,从 0615 变化为 0616

图12

wangqiang@DESKTOP-QLPK8QM:/mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod$ npm run deploy

> learn-php-app-ubuntu-2004-0605@1.0.0 deploy
> shopify app deploy

?  Make the following changes to your extensions in Shopify Partners?
✔  Yes, deploy to push changes


Deploying your work to Shopify Partners. It will be part of learn-php-app-0605-prod

 theme_extensions │ Running theme check on your Theme app extension...
 theme_extensions │ Checking /mnt/e/wwwroot/shopify-app/learn-php-app-0605-prod/extensions/theme-app-extension-0612 ...
 theme_extensions │ 4 files inspected, 0 offenses detected, 0 offenses auto-correctable


╭─ success ────────────────────────────────────────────────────────────────────╮
│                                                                              │
│  Deployed to Shopify!                                                        │
│                                                                              │
│  Summary                                                                     │
│    • theme-app-extension-0612 is deployed to Shopify but not yet live        │
│                                                                              │
│  Next steps                                                                  │
│    • Publish theme-app-extension-0612 [1]                                    │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
[1] https://partners.shopify.com/2442779/apps/44885082113/extensions/theme_app_extension/25719668737

]]>
https://www.shuijingwanwq.com/2023/07/20/7878/feed/ 0