You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Debian 9安装mysql-server未提示root密码无法登录的解决问询

解决Debian 9下MySQL root无法登录及安全配置问题

Hey there, let's work through this problem together— I’ve dealt with exactly these quirks on Debian 9 before, so I know the ins and outs here.

第一步:先无密码登录MySQL root账户

On Debian 9, when you install Oracle’s MySQL (not the default MariaDB), the root user is set up to use unix socket authentication by default instead of a password. That means you can log in directly with sudo without entering a password:

  • Run this command in your terminal:
    sudo mysql -u root
    

You’ll be dropped right into the MySQL command-line console.

第二步:设置MySQL root账户的密码

Once you’re inside MySQL, let’s set a secure password for root and adjust the authentication method so you can log in with a password later:

  1. First, switch to the mysql system database where user credentials are stored:
    USE mysql;
    
  2. Update the root user’s password and switch to password-based authentication (you can keep socket auth too if you want, but this lets you log in with mysql -u root -p):
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your-strong-password-here';
    
    Replace your-strong-password-here with a secure password of your choice.
  3. Flush the privileges to apply the changes immediately:
    FLUSH PRIVILEGES;
    
  4. Exit the MySQL console:
    EXIT;
    

Now you should be able to log in with mysql -u root -p by entering the password you just set.

第三步:解决mysql_secure_installation只针对MariaDB的问题

The issue here is that Debian 9 comes with MariaDB’s version of mysql_secure_installation pre-installed, and it’s probably prioritized in your system’s PATH over the MySQL version. Here’s how to fix it:

Option 1: Use the MySQL-specific mysql_secure_installation script

First, find where MySQL’s version of the script lives:

find / -name mysql_secure_installation 2>/dev/null

If you installed MySQL via Oracle’s official repo, it’s likely in /usr/bin/ but you can confirm it’s the MySQL version by checking its ownership. Alternatively, if you did a binary install, it’ll be in /usr/local/mysql/bin/.

Run the script with sudo using its full path. For example:

sudo /usr/local/mysql/bin/mysql_secure_installation

Follow the prompts— it’ll now configure your MySQL instance’s security settings, not MariaDB’s.

Option 2: Manually apply the security steps (if the script still misbehaves)

If you can’t get the right script to run, you can do exactly what mysql_secure_installation does manually in the MySQL console (log in with sudo mysql -u root first):

  • Delete anonymous user accounts (these are a security risk):
    DROP USER ''@'localhost';
    
  • Remove the test database (it’s unused and accessible to anonymous users by default):
    DROP DATABASE test;
    
  • Prevent root from logging in remotely (unless you explicitly need this, it’s safer to disable):
    DROP USER 'root'@'%';
    
  • Flush privileges again to save changes:
    FLUSH PRIVILEGES;
    

Bonus: Fix your PATH to prioritize MySQL

To avoid this confusion in the future, add MySQL’s bin directory to the front of your system PATH. Edit your ~/.bashrc file:

nano ~/.bashrc

Add this line at the end (adjust the path if your MySQL bin is in a different location):

export PATH=/usr/local/mysql/bin:$PATH

Save and exit, then reload your bash profile:

source ~/.bashrc

Now when you run mysql_secure_installation, it’ll use the MySQL version by default.

Let me know if you run into any issues with these steps— I’m happy to help troubleshoot further!

内容的提问来源于stack exchange,提问作者tommy716

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:47:31