ubuntu22.04 mysql 移动数据目录后 systemctl start mysql无法启动的解决方案

ubuntu22.04 mysql 移动数据目录后 systemctl start mysql无法启动的解决方案

默认安装的数据目录在/var/lib/mysql下

我把它移到了/data/mysql/data下,修改mysql数据文件夹

root@iZbp:~# nano /etc/mysql/my.cnf
  GNU nano 6.2                                                /etc/mysql/my.cnf                                                         
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
datadir=/data/mysql/data

执行 systemctl start mysql如下所示:

root@iZbp:~# systemctl start mysql
Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xeu mysql.service" for details.
root@iZbp:~# journalctl -xeu mysql.service            
░░ Support: http://www.ubuntu.com/support
░░ 
░░ The unit mysql.service has entered the 'failed' state with result 'exit-code'.
Aug 12 16:58:21 iZbp systemd[1]: Failed to start MySQL Community Server.
░░ Subject: A start job for unit mysql.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░ 
░░ A start job for unit mysql.service has finished with a failure.
░░ 
░░ The job identifier is 7805 and the job result is failed.
Aug 12 16:58:21 iZbp systemd[1]: mysql.service: Scheduled restart job, restart counter is at 5.
░░ Subject: Automatic restarting of a unit has been scheduled
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░ 
░░ Automatic restarting of the unit mysql.service has been scheduled, as the result for
░░ the configured Restart= setting for the unit.
Aug 12 16:58:21 iZbp systemd[1]: Stopped MySQL Community Server.
░░ Subject: A stop job for unit mysql.service has finished
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░ 
░░ A stop job for unit mysql.service has finished.
░░ 
░░ The job identifier is 7897 and the job result is done.
Aug 12 16:58:21 iZbp systemd[1]: mysql.service: Start request repeated too quickly.
Aug 12 16:58:21 iZbp systemd[1]: mysql.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░ 
░░ The unit mysql.service has entered the 'failed' state with result 'exit-code'.
Aug 12 16:58:21 iZbp systemd[1]: Failed to start MySQL Community Server.
░░ Subject: A start job for unit mysql.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░ 
░░ A start job for unit mysql.service has finished with a failure.
░░ 
░░ The job identifier is 7897 and the job result is failed.
Aug 12 16:58:28 iZbp systemd[1]: mysql.service: Start request repeated too quickly.
Aug 12 16:58:28 iZbp systemd[1]: mysql.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░ 
░░ The unit mysql.service has entered the 'failed' state with result 'exit-code'.
Aug 12 16:58:28 iZbp systemd[1]: Failed to start MySQL Community Server.
░░ Subject: A start job for unit mysql.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░ 
░░ A start job for unit mysql.service has finished with a failure.
░░ 
░░ The job identifier is 7990 and the job result is failed.

其中 /data/mysql/data已chown -R mysql:mysql了,还是不行

最终找到的原因和解决方法
/etc/apparmor.d/usr.sbin.mysqld文件有对权限的控制,需要修改相关项,如下所示

root@iZbp:~# sudo nano /etc/apparmor.d/usr.sbin.mysqld
# vim:syntax=apparmor
# Last Modified: Tue Feb 09 15:28:30 2016
#include <tunables/global>

/usr/sbin/mysqld {
  #include <abstractions/base>
  #include <abstractions/nameservice>
  #include <abstractions/user-tmp>
  #include <abstractions/mysql>
  #include <abstractions/winbind>

# Allow system resource access
  /proc/*/status r,
  /sys/devices/system/cpu/ r,
  /sys/devices/system/node/ r,
  /sys/devices/system/node/** r,
  capability sys_resource,
  capability dac_override,
  capability dac_read_search,
  capability setuid,
  capability setgid,

# Allow network access
  network tcp,

  /etc/hosts.allow r,
  /etc/hosts.deny r,

# Allow config access
  /etc/mysql/** r,

# Allow pid, socket, socket lock file access
  /var/run/mysqld/mysqld.pid rw,
  /var/run/mysqld/mysqld.sock rw,
  /var/run/mysqld/mysqld.sock.lock rw,
  /var/run/mysqld/mysqlx.sock rw,
  /var/run/mysqld/mysqlx.sock.lock rw,
# vim:syntax=apparmor
# Last Modified: Tue Feb 09 15:28:30 2016
#include <tunables/global>

/usr/sbin/mysqld {
  #include <abstractions/base>
  #include <abstractions/nameservice>
  #include <abstractions/user-tmp>
  #include <abstractions/mysql>
  #include <abstractions/winbind>

# Allow system resource access
  /proc/*/status r,
  /sys/devices/system/cpu/ r,
  /sys/devices/system/node/ r,
  /sys/devices/system/node/** r,
  capability sys_resource,
  capability dac_override,
  capability dac_read_search,
  capability setuid,
  capability setgid,

# Allow network access
  network tcp,

  /etc/hosts.allow r,
  /etc/hosts.deny r,

# Allow config access
  /etc/mysql/** r,

# Allow pid, socket, socket lock file access
  /var/run/mysqld/mysqld.pid rw,
  /var/run/mysqld/mysqld.sock rw,
  /var/run/mysqld/mysqld.sock.lock rw,
  /var/run/mysqld/mysqlx.sock rw,
  /var/run/mysqld/mysqlx.sock.lock rw,
  GNU nano 6.2                                         /etc/apparmor.d/usr.sbin.mysqld                                                  
  /run/mysqld/mysqld.pid rw,
  /run/mysqld/mysqld.sock rw,
  /run/mysqld/mysqld.sock.lock rw,
  /run/mysqld/mysqlx.sock rw,
  /run/mysqld/mysqlx.sock.lock rw,

# Allow systemd notify messages
  /{,var/}run/systemd/notify w,

# Allow execution of server binary
  /usr/sbin/mysqld mr,
  /usr/sbin/mysqld-debug mr,

# Allow plugin access
  /usr/lib/mysql/plugin/ r,
  /usr/lib/mysql/plugin/*.so* mr,
  /data/mysql/data/plugin/ r,
  /data/mysql/data/plugin/*.so* mr,


# Allow error msg and charset access
  /usr/share/mysql/ r,
  /usr/share/mysql/** r,

# Allow data dir access
  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,
  /data/mysql/data/ r,
  /data/mysql/data/** rwk,

# Allow data files dir access
  /var/lib/mysql-files/ r,
  /var/lib/mysql-files/** rwk,

# Allow keyring dir access
  /var/lib/mysql-keyring/ r,
  /var/lib/mysql-keyring/** rwk,

# Allow log file access
  /var/log/mysql.err rw,
  /var/log/mysql.log rw,
  /var/log/mysql/ r,
  /var/log/mysql/** rw,

# Allow read access to mecab files
  /var/lib/mecab/dic/ipadic-utf8/** r,

# Allow read access to OpenSSL config
  /etc/ssl/openssl.cnf r,
  # Site-specific additions and overrides. See local/README for details.
  #include <local/usr.sbin.mysqld>
}

我修改了两项

Allow plugin access

# Allow plugin access
  /usr/lib/mysql/plugin/ r,
  /usr/lib/mysql/plugin/*.so* mr,
  /data/mysql/data/plugin/ r,
  /data/mysql/data/plugin/*.so* mr,

Allow data dir access

# Allow data dir access
  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,
  /data/mysql/data/ r,
  /data/mysql/data/** rwk,

增加新的目标文件的访问权限,Ctrl+O 保存 Ctrl+X退出编辑。

再执行

root@iZbp:~# systemctl stop mysql
root@iZbp:~# systemctl start mysql

正常启动

<think>嗯,用户现在问的是关于在Ubuntu上使用`sudo systemctl start mysql.service`和`sudo mysql_secure_installation`这两个命令的具体介绍。之前的对话里,用户已经了解了如何用Python连接MySQL,现在可能是在配置MySQL环境时遇到了问题,或者想更深入了解这两个命令的作用。 首先,我需要确认用户的需求。他们可能刚安装好MySQL,但不知道如何启动服务或者进行安全设置。这时候解释这两个命令的具体用途和步骤是很重要的。用户可能对Linux系统服务管理不太熟悉,所以需要详细说明systemctl的作用,以及为什么需要启动MySQL服务。 接下来,关于`mysql_secure_installation`,用户可能对安全性配置不太了解,比如为什么要设置密码强度、删除匿名用户、禁止远程root登录等。需要一步步解释每个选项的意义,帮助用户理解每个步骤的重要性,以及如何根据实际需求选择配置。 还要注意用户可能的疑问点,比如是否需要每次都手动启动MySQL服务,或者如何设置开机自启。这里需要说明`systemctl enable`的作用,以及启动服务和启用服务的区别。 另外,用户可能在执行这些命令时遇到权限问题,比如是否必须使用sudo,或者配置过程中某些选项该如何选择。这时候需要提醒用户使用sudo的必要性,以及根据具体情况选择合适的配置,比如开发环境和生产环境的区别。 最后,要确保回答清晰,分步骤解释每个命令,并给出示例,让用户能够跟着操作。同时,注意语言简洁,避免技术术语过多,让新手也能理解。</think>以下是针对这两个命令的详细说明: --- ### **1. `sudo systemctl start mysql.service`** #### **作用** - 用于启动MySQL数据库服务。安装MySQL后,服务默认可能不会自动运行,需要手动启动或配置开机自启。 #### **操作说明** 1. **启动服务**: ```bash sudo systemctl start mysql.service ``` - 若成功不会有提示;可通过状态检查命令验证: ```bash sudo systemctl status mysql.service ``` - 显示 `active (running)` 表示服务已启动。 2. **设置开机自启**(可选): ```bash sudo systemctl enable mysql.service ``` - 避免每次重启后需手动启动--- ### **2. `sudo mysql_secure_installation`** #### **作用** - 安全配置向导,用于增强MySQL安全性,包括: - 设置root用户密码 - 删除匿名用户 - 禁止远程root登录 - 移除测试数据- 重新加载权限表 #### **操作步骤** 运行命令后,按提示逐步操作: ```bash sudo mysql_secure_installation ``` 1. **密码强度验证插件**: - 提示:`VALIDATE PASSWORD COMPONENT...` - 输入 `Y` 启用密码强度检查(建议生产环境启用)。 - 输入 `N` 跳过(开发环境可选)。 2. **设置root密码**: - 提示:`New password` - 输入并确认密码(若启用强度检查需符合规则)。 3. **删除匿名用户**: - 提示:`Remove anonymous users?` - 输入 `Y`(默认建议删除)。 4. **禁止远程root登录**: - 提示:`Disallow root login remotely?` - 输入 `Y`(除非需要远程root访问)。 5. **删除测试数据库**: - 提示:`Remove test database and access to it?` - 输入 `Y`(避免潜在漏洞)。 6. **重载权限表**: - 提示:`Reload privilege tables now?` - 输入 `Y` 使配置立即生效。 --- ### **注意事项** - **权限问题**:所有操作需 `sudo` 权限。 - **密码安全**:若忘记root密码,需手动重置(需停止服务后操作)。 - **开发环境灵活性**:若为本地测试环境,可跳过部分安全选项(如远程root限制)。 --- ### **常见问题** **Q1: 如果报错“Failed to start mysql.service”怎么办?** - 检查MySQL安装是否完整:`sudo apt install mysql-server --reinstall` - 查看日志定位问题:`journalctl -u mysql.service` **Q2: 如何恢复`mysql_secure_installation`的配置?** - 需手动修改MySQL权限表或重新运行向导。 --- 通过以上步骤,即可完成MySQL服务的启动与基础安全配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值