MySQL mysql_secure_installation 安全配置向导

本文详细介绍如何使用mysql_secure_installation向导增强MySQL安全性,包括设置root密码、删除匿名账户、禁用root远程登录、移除test数据库及刷新权限表,确保生产环境MySQL服务器安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL----mysql_secure_installation 安全配置向导

安装完mysql-server 会提示可以运行mysql_secure_installation。运行mysql_secure_installation会执行几个设置:

--为root用户设置密码
--删除匿名账号
--取消root用户远程登录
--删除test库和对test库的访问权限
--刷新授权表使修改生效

通过这几项的设置能够提高mysql库的安全。
建议生产环境中mysql安装这完成后一定要运行一次mysql_secure_installation,相关操作如下:

[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):<–初次运行直接回车
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n]    #是否设置root用户密码,输入y并回车或直接回车
New password:               #设置root用户的密码
Re-enter new password:      #再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]   #是否删除匿名用户,生产环境建议删除,所以直接回车
… Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] #是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] #是否删除test数据库,直接回车
- Dropping test database…
… Success!
- Removing privileges on test database…
… Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] #是否重新加载权限表,直接回车
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
--------------------- 

原文:https://blog.youkuaiyun.com/damys/article/details/50177331

<think>我们正在处理用户的问题:mysql_secure_installation command not found error 根据引用内容,这个问题通常发生在尝试运行mysql_secure_installation命令时,系统找不到该命令。引用中提供了不同操作系统下的解决方法。 引用[1]和[2]针对Ubuntu系统: - 引用[1]提到将apt换成yum,但注意Ubuntu通常使用apt,而yum是CentOS/RHEL的包管理器,所以这个建议可能不适用。 - 引用[2]给出了Ubuntu下的标准安装步骤:先更新,然后安装mysql-server,最后运行mysql_secure_installation。 引用[3]针对macOS(使用brew安装): - 启动mysql服务后运行mysql_secure_installation。 引用[4]提到了编译安装mysql时的一些存储引擎选项,与当前问题关系不大。 因此,我们需要根据用户的操作系统提供解决方案。由于用户没有明确操作系统,我们将分别提供Ubuntu和macOS(使用Homebrew)的解决方案,并补充其他可能的情况。 可能的原因: 1. MySQL未安装:如果尚未安装MySQL,则不会安装mysql_secure_installation脚本。 2. MySQL安装路径不在PATH环境变量中:特别是当通过源码编译安装或使用非包管理器安装时。 解决方案: 对于Ubuntu/Debian系统: 步骤1:更新包列表 sudo apt update 步骤2:安装MySQL服务器 sudo apt install mysql-server 步骤3:运行安全安装脚本 sudo mysql_secure_installation 对于CentOS/RHEL系统: 步骤1:安装MySQL服务器(如果尚未安装) sudo yum install mysql-server 步骤2:启动MySQL服务 sudo systemctl start mysqld 步骤3:运行安全安装脚本 sudo mysql_secure_installation 对于macOS(使用Homebrew): 步骤1:安装MySQL(如果尚未安装) brew install mysql@版本号 # 例如:brew install mysql@5.7 步骤2:启动MySQL服务 brew services start mysql@5.7 # 使用实际安装的版本 步骤3:运行安全安装脚本 mysql_secure_installation 如果已经安装了MySQL但仍然找不到命令,可能是因为安装路径不在PATH中。可以尝试以下方法: - 查找mysql_secure_installation的位置: sudo find / -name mysql_secure_installation 2>/dev/null - 将找到的路径添加到PATH环境变量中,或者使用完整路径运行,例如: /usr/local/mysql/bin/mysql_secure_installation 注意:在某些安装中(例如从源码编译安装),可能需要手动运行安全脚本,或者它可能位于非标准位置。 另外,引用[3]中提到在macOS上设置密码后一路按y,这实际上是运行mysql_secure_installation时的交互过程,用户可以根据提示进行安全设置。 总结:首先确保MySQL已正确安装,然后根据操作系统使用相应的命令运行安全脚本。如果仍然找不到命令,尝试定位脚本的路径并直接运行。 生成相关问题: 1. 如何检查MySQL是否已安装? 2. 如何将MySQL的可执行文件目录添加到PATH环境变量中? 3. 运行mysql_secure_installation时有哪些配置选项? 4. 如何在不同操作系统上启动MySQL服务?</think>### MySQL `mysql_secure_installation` Command Not Found 解决方案 当出现 `mysql_secure_installation: command not found` 错误时,通常是由于 **MySQL未正确安装** 或 **环境变量未配置** 导致的。以下是分步解决方案: #### 一、核心解决步骤 1. **确认 MySQL 是否安装** 运行以下命令检查安装状态: ```bash mysql --version ``` - 若返回版本信息(如 `mysql Ver 8.0.35`),说明 MySQL 已安装但路径未配置。 - 若提示 `command not found`,需重新安装 MySQL。 2. **安装 MySQL 服务端** **Ubuntu/Debian**: ```bash sudo apt update sudo apt install mysql-server # 安装核心服务端[^2] ``` **CentOS/RHEL**: ```bash sudo yum install mysql-server # 替换apt为yum[^1] ``` **macOS (Homebrew)**: ```bash brew install mysql@5.7 # 指定版本安装[^3] brew services start mysql@5.7 # 启动服务 ``` 3. **定位脚本路径并运行** 通过 `find` 命令搜索脚本位置: ```bash sudo find / -name mysql_secure_installation 2>/dev/null ``` 常见路径: - Ubuntu: `/usr/bin/mysql_secure_installation` - macOS: `/usr/local/opt/mysql@5.7/bin/mysql_secure_installation` 使用完整路径运行: ```bash sudo /usr/bin/mysql_secure_installation # 替换为实际路径 ``` 4. **配置环境变量(可选)** 将 MySQL 路径加入 `PATH`: ```bash echo 'export PATH="/usr/local/mysql/bin:$PATH"' >> ~/.bashrc # macOS示例 source ~/.bashrc ``` --- #### 二、关键注意事项 1. **权限问题** 必须使用 `sudo` 运行安全脚本: ```bash sudo mysql_secure_installation ``` 2. **安装后未自动包含脚本** 某些安装方式(如源码编译)可能不包含安全脚本,需通过包管理器安装[^4]。 3. **交互式配置向导** 运行后会进入安全设置向导,需按提示: - 设置 root 密码 - 移除匿名用户(选 `Y`) - 禁止远程 root 登录(选 `Y`) - 删除测试数据库(选 `Y`) - 重载权限表(选 `Y`)[^2][^3] --- #### 三、验证是否解决 ```bash mysql -u root -p # 用设置的密码登录 SHOW DATABASES; # 成功显示数据库即配置正常 ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值