You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下用systemd运行Golang服务(GORM+MySQL)遇连接问题求助

问题:systemd运行Golang服务时无法连接MySQL

环境与现象

  • AWS Ubuntu发行版
  • 直接执行./user-service-linux或sudo ./user-service-linux时,服务可正常运行并连接MySQL
  • 通过systemd启动服务时,出现MySQL连接错误

systemd配置文件

创建的/etc/systemd/system/user.service内容:

[Unit]
Description=User service
ConditionPathExists=/home/ubuntu/repos/go-api-starter/
After=network.target

[Service]
Type=simple
User=ubuntu
Group=ubuntu
# 改成root也无法解决问题
LimitNOFILE=1024

Restart=on-failure
RestartSec=10
startLimitIntervalSec=60

WorkingDirectory=/home/ubuntu/repos/go-api-starter/
ExecStart=/home/ubuntu/repos/go-api-starter/user-service-linux

[Install]
WantedBy=multi-user.target

执行命令

$ sudo systemctl daemon-reload
$ sudo systemctl enable user.service
$ sudo systemctl start user

错误日志

执行sudo journalctl -f -u user查看日志,出现:

panic: Failed to connect to database: %w dial tcp 127.0.0.1:3306: connect: connection refused

此前还遇到过权限错误:

[error] failed to initialize database, got error Error 1045: Access denied for user 'root'@'localhost' (using password: YES)

其他关联问题

  • 不使用sudo执行systemctl start user时,会提示需登录org.freedesktop.systemd1.manage-units,但ubuntu用户无对应密码
  • 执行mysql -u root -p使用Go项目GORM配置内的凭据可正常连接;但直接执行mysql -p会报错:
ERROR 1045 (28000): Access denied for user 'ubuntu'@'localhost' (using password: YES)
  • 尝试修改ubuntu用户的MySQL密码时:
ALTER USER ‘ubuntu’@‘localhost’ IDENTIFIED BY 'newPass';

返回错误:

ERROR 1396 (HY000): Operation ALTER USER failed for 'ubuntu'@'localhost'

解决方案

1. 解决MySQL连接拒绝问题

  • 检查MySQL绑定地址:查看/etc/mysql/mysql.conf.d/mysqld.cnf内的bind-address配置,确保值为127.0.0.1或注释掉(允许所有IP访问)
  • 确保MySQL先于服务启动:修改systemd服务文件的[Unit]段,添加mysql.target依赖:
    [Unit]
    ...
    After=network.target mysql.target
    
    执行sudo systemctl daemon-reload后重启服务:sudo systemctl restart user
  • 确认MySQL服务状态:执行sudo systemctl status mysql,确保服务正常运行

2. 解决MySQL权限问题

为什么ALTER USER失败?

ubuntu'@'localhost用户不存在,需先创建再授权:

CREATE USER 'ubuntu'@'localhost' IDENTIFIED BY 'your-password';
GRANT ALL PRIVILEGES ON your-db-name.* TO 'ubuntu'@'localhost';
FLUSH PRIVILEGES;

(替换your-password和your-db-name为实际值)

或使用已有的root用户连接

确认GORM连接字符串正确,示例:

dsn := "root:your-password@tcp(127.0.0.1:3306)/your-db?charset=utf8mb4&parseTime=True&loc=Local"

同时检查root用户的本地连接权限:

SELECT user, host FROM mysql.user WHERE user='root';

若权限不足,执行:

GRANT ALL PRIVILEGES ON your-db-name.* TO 'root'@'localhost';
FLUSH PRIVILEGES;

3. systemd服务权限问题

  • 允许ubuntu用户管理服务:通过sudo visudo添加权限,示例:
    ubuntu ALL=(ALL) NOPASSWD: /bin/systemctl start user.service, /bin/systemctl stop user.service, /bin/systemctl restart user.service
    
  • 检查文件路径权限:确认ubuntu用户对/home/ubuntu/repos/go-api-starter/有读写权限,执行ls -ld /home/ubuntu/repos/go-api-starter/验证

4. 快速排查步骤

  1. 用ubuntu用户身份测试MySQL连接:sudo -u ubuntu mysql -u root -p(输入GORM配置内的密码)
  2. 确认GORM配置文件路径正确:确保配置文件在WorkingDirectory下,或使用绝对路径读取

内容的提问来源于stack exchange,提问作者Michael Poyatt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 07:55:03