首页 >> 工具知识
mysql命令-mysql_secure_installation
mysql_secure_installation 是一个用于提高 MySQL/MariaDB 安装安全性的交互式脚本。通常在安装 MySQL 后立即运行此脚本,以设置基本的安全配置。
主要功能
1. 设置/更改 root 密码
如果 root 账户没有密码,脚本会提示设置密码
如果已有密码,可以选择更改密码
2. 删除匿名用户
默认安装会创建匿名用户(空用户名)
匿名用户允许任何人无需密码登录 MySQL
脚本会询问是否删除这些匿名账户
3. 禁止 root 远程登录
默认情况下,root 可以从任何主机连接
脚本会限制 root 只能从 localhost 连接
如果需要远程管理,建议创建单独的管理账户
4. 删除测试数据库
默认会创建名为
test的数据库所有用户都有权限访问测试数据库
生产环境中建议删除
5. 重新加载权限表
应用所有安全更改
使更改立即生效
使用方式
基本用法
sudo mysql_secure_installation
非交互式使用(自动化部署)
# 设置密码并执行默认安全选项
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';"
sudo mysql -e "DELETE FROM mysql.user WHERE User='';"
sudo mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
sudo mysql -e "DROP DATABASE IF EXISTS test;"
sudo mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
sudo mysql -e "FLUSH PRIVILEGES;"详细执行步骤示例
$ sudo mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. # 1. 验证密码插件配置(MySQL 8.0+) VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM, 2 = STRONG: 1 # 2. 设置 root 密码 Please set the password for root here. New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y # 3. 删除匿名用户 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? (Press y|Y for Yes, any other key for No) : y # 4. 禁止 root 远程登录 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? (Press y|Y for Yes, any other key for No) : y # 5. 删除测试数据库 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? (Press y|Y for Yes, any other key for No) : y # 6. 重新加载权限表 Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y All done!
最新文章
mysql命令-mysqlimport2026-02-01
mysql命令-mysql_tzinfo_to_sql2026-02-01
mysql命令-mysql2026-02-01
mysql命令-my_print_defaults2026-02-01
mysql命令-mysqldumpslow2026-02-01
mysql命令-mysqld2026-02-01
mysql命令-mysql_secure_installation2026-02-01
mysql命令-myisampack2026-02-01
mysql命令-perror2026-02-01
mysql命令-mysqldump2026-02-01
