📅  最后修改于: 2023-12-03 15:17:28.331000             🧑  作者: Mango
MySQL 是一款广泛使用的开源关系型数据库管理系统。MySQL 默认安装时有一些不必要的设置,因此,MySQL 安装之后必须进行一些配置,保证数据库的安全性。mysql_secure_installation 是 MySQL 自带的一款脚本,通过运行该脚本可以完成 MySQL 的安全配置。
在 Mac 中运行 mysql_secure_installation 脚本时,需要输入 MySQL 的 root 用户密码。如果是首次安装 MySQL,则默认密码为空,直接按回车即可。但如果 MySQL 已经被安装,则默认密码会被设置为之前设置的密码。
如果 MySQL 已经被安装,那么默认密码可以在 MySQL 配置文件中找到。MySQL 配置文件通常位于 /etc/mysql 目录下,示例代码如下:
$ cat /etc/mysql/my.cnf
在这个文件中,可以找到 MySQL 的 root 用户的默认密码及其他的 MySQL 配置信息。
# The MySQL database server configuration file.
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
#
# * Fine Tuning
#
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam_recover_options = BACKUP
#max_connections = 100
#table_cache = 64
#thread_concurrency = 10
#
# * Query Cache Configuration
#
query_cache_limit = 1M
query_cache_size = 16M
#
# * Logging and Replication
#
#
# ** Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# ** Here you can see queries with especially long duration
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# ** Replication
#
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
#expire_logs_days = 10
#max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates you can use for example the GUI tool "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
#no-auto-rehash # faster start of mysql but no tab completition
[isamchk]
key_buffer = 16M
#
# * 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/
从配置文件中获取 MySQL 的 root 用户默认密码后,可以使用以下命令登录 MySQL:
$ mysql -u root -p
Enter password: # 输入 MySQL 的 root 用户默认密码
MySQL 的 root 用户默认密码在 MySQL 配置文件中进行设置,如果是首次安装,则默认密码为空。运行 mysql_secure_installation 脚本时需要输入 root 用户的密码,如果这个密码被更改了,则需要手动输入更改后的密码。