Ubuntu24.04在jdk8的环境安装seata2.2(MySQL8+nacos2.5.3)

Title Ubuntu24.04在jdk8的环境安装seata2.2(MySQL8+nacos2.5.3)
Framework Ubuntu
User wy8817399@vip.qq.com
Id 90
Created 8/20/26, 2:58 PM
Modified 8/22/26, 9:39 PM
Published Yes
Content

1、创建目录

mkdir -p /Seata && cd /Seata

 

2、下载并解压

wget https://archive.apache.org/dist/incubator/seata/2.2.0/apache-seata-2.2.0-incubating-bin.tar.gz
tar -xzf apache-seata-2.2.0-incubating-bin.tar.gz
cd seata-server

 

3、MySQL创建seata用户

mysql -uroot -p
-- 1. 建库
CREATE DATABASE IF NOT EXISTS seata DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

-- 2. 建用户并授权(Seata 专用账号,不要用 root)
CREATE USER IF NOT EXISTS 'seata'@'%' IDENTIFIED BY 'seata';
GRANT ALL PRIVILEGES ON seata.* TO 'seata'@'%';
FLUSH PRIVILEGES;

 

4、导入数据

cat > seata_server.sql <<'EOF'
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements.  See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License.  You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAR(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAR(32),
    `transaction_service_group` VARCHAR(32),
    `transaction_name`          VARCHAR(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAR(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_status_gmt_modified` (`status` , `gmt_modified`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(128),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_status` (`status`),
    KEY `idx_branch_id` (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAR(20) NOT NULL,
    `lock_value`     VARCHAR(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);


CREATE TABLE IF NOT EXISTS `vgroup_table`
(
    `vGroup`    VARCHAR(255),
    `namespace` VARCHAR(255),
    `cluster`   VARCHAR(255),
  UNIQUE KEY `idx_vgroup_namespace_cluster` (`vGroup`,`namespace`,`cluster`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;
EOF
mysql -uroot -p seata < seata_server.sql

 

5、配置application.yml

"seata.store.db..vgroup-table": vgroup_table

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${log.home:${user.home}/logs/seata}
  extend:
    logstash-appender:
      destination: 127.0.0.1:4560
    kafka-appender:
      bootstrap-servers: 127.0.0.1:9092
      topic: logback_to_logstash

console:
  user:
    username: seata
    password: seata
seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: file
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server # 注册到 Nacos 的服务名(客户端按此名查找 TC)
      server-addr: 192.168.20.139:8848   # ★ Nacos连接地址
      group: SEATA_GROUP        # Nacos 中的分组名
      namespace: ""             # 命名空间 ID,留空 = public(注意:填 ID 不填名称)
      cluster: default          # 集群名,客户端配置必须与此一致
      username: nacos           # Nacos 控制台账号
      password: nacos           # Nacos 控制台密码
  store:
    # support: file 、 db 、 redis 、 raft
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://192.168.20.139:3306/seata?useSSL=true&serverTimezone=Asia/Shanghai
      user: seata                 # Step 4 创建的 Seata 专用账号
      password: seata             # 同上
      min-conn: 5                 # 最小连接数
      max-conn: 30                # 最大连接数
      global-table: global_table          # 对应的表名(和 DDL 一致,不用改)
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 100            # 单次查询最大行数
      max-retry-size: 10          # 提交/回滚最大重试次数
  #  server:
  #    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    csrf-ignore-urls: /metadata/v1/**
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login,/version.json,/health,/error,/vgroup/v1/**

 

6、下载MySQL驱动。

mkdir -p /Seata/seata-server/lib/jdbc
wget -P /Seata/seata-server/lib/jdbc/ https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.0.33/mysql-connector-j-8.0.33.jar

 

7、启动

cd /Seata/seata-server/bin
./seata-server.sh

 

8、浏览器验证

http://192.168.20.139:7091/

 

9、配 systemd 后台运行

# 先探测 JAVA_HOME(systemd 不读 profile,必须显式写入服务)
JAVA_HOME_PATH=$(dirname $(dirname $(readlink -f $(which java))))
echo "JAVA_HOME = $JAVA_HOME_PATH"

# 注意:下面 heredoc 用的是不带引号的 << EOF,bash 会在写文件那一刻把 $JAVA_HOME_PATH
# 替换成变量值——所以上面两行赋值必须在 cat 之前执行,顺序不能颠倒(否则写入空值)。
cat > /etc/systemd/system/seata.service << EOF
[Unit]
Description=Seata Server (TC) 2.2.0
Documentation=https://seata.apache.org
After=network.target

[Service]
Type=forking
Environment=JAVA_HOME=$JAVA_HOME_PATH
WorkingDirectory=/Seata/seata-server
ExecStart=/Seata/seata-server/bin/seata-server.sh
ExecStop=/Seata/seata-server/bin/seata-server.sh stop
Restart=on-failure
RestartSec=10
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload  
systemctl enable seata        # 开机自启  
systemctl start seata         # 启动  
sleep 10  
systemctl status seata        # 查看状态(应为 active (running),Main PID 是 java)  
ss -lntp | grep 7091          # Console 端口在监听即成功