3. 第三步:安装 MySQL 8.0(Ubuntu 22.04/24.04 默认就是 8.0.43+)
1 2
sudo apt update sudo apt install mysql-server -y
Ubuntu 22.04 默认装 8.0.39~8.0.43,24.04 可能更高(如 8.0.39+),都属于 MySQL 8.0 系列,完全符合企业主流。
4. 第四步:安全加固(必须做!)
1
sudo mysql_secure_installation
按提示:
是否启用密码强度验证? → 作为初学者直接选 n。 选 y 就表示要强制高强度密码,如果太弱(如 123456),MySQL 会 拒绝接受。
然后依次问是否 (全 y):
删除匿名用户? → 选 y。
禁止 root 远程登录? → 选 y。
删除 test 数据库? → 选 y。
重载权限表? → 选 y。
5. 第五步:自定义密码
为什么没让设置 root 密码?
因为 Ubuntu 的 MySQL 包默认使用 auth_socket 插件(也叫 unix_socket)进行认证,而不是密码。auth_socket 是什么? 它允许 系统用户 root 直接通过 sudo mysql 无密码登录 MySQL,前提是你是 Linux 的 root 用户。
所以:MySQL 的 root 用户并没有密码,而是“信任”系统 root。这就是为什么 mysql_secure_installation 里跳过了设密码步骤,并提示:“Skipping password set for root as authentication with auth_socket is used by default.”
如果希望像传统方式那样用 mysql -u root -p 输入密码登录,需要手动改认证方式:
1. 步骤 1:用 socket 方式登录(无需密码)
1
sudo mysql
2. 步骤 2:修改 root 用户为密码认证
1 2 3 4 5 6 7 8 9 10 11
-- 查看当前 root 用户的认证方式 SELECTuser, host, plugin FROM mysql.user;
# # The MySQL database server configuration file. # # 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
# Here is entries for some specific programs # The following values assume you have at least 32M ram
[mysqld] # # * Basic Settings # user = mysql # pid-file = /var/run/mysqld/mysqld.pid # socket = /var/run/mysqld/mysqld.sock # port = 3306 # datadir = /var/lib/mysql
# If MySQL is running as a replication slave, this should be # changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir # tmpdir = /tmp # # 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 mysqlx-bind-address = 127.0.0.1
# This replaces the startup script and checks MyISAM tables if needed # the first time they are touched myisam-recover-options = BACKUP
# max_connections = 151
# table_open_cache = 4000
# # * Logging and Replication # # Both location gets rotated by the cronjob. # # Log all queries # Be aware that this log type is a performance killer. # general_log_file = /var/log/mysql/query.log # general_log = 1 # # Error log - should be very few entries. # log_error = /var/log/mysql/error.log # # Here you can see queries with especially long duration # slow_query_log = 1 # slow_query_log_file = /var/log/mysql/mysql-slow.log # long_query_time = 2 # log-queries-not-using-indexes # # The following can be used as easy to replay backup logs or for replication. # note: if you are setting up a replication slave, see README.Debian about # other settings you may need to change. # server-id = 1 # log_bin = /var/log/mysql/mysql-bin.log # binlog_expire_logs_seconds = 2592000 max_binlog_size = 100M # binlog_do_db = include_database_name # binlog_ignore_db = include_database_name
保存后重启服务让配置生效:
1 2
sudo systemctl restart mysql sudo systemctl status mysql