MariaDBがUbuntu 15.04でパスワードを確認しない問題:原因、解決策、解説
MariaDB が Ubuntu 15.04 でパスワードを確認しない問題:解決策と解説
MySQL や MariaDB を Ubuntu 15.04 にインストールすると、root ユーザーを含むすべてのユーザーに対してパスワード認証が機能しない場合があります。
原因
この問題は、デフォルトの認証プラグインが mysql_native_password
から caching_sha2_password
に変更されたことにより発生します。 caching_sha2_password
は、古いパスワードハッシュ形式をサポートしていないため、古いパスワードハッシュを持つユーザーはログインできません。
解決策
以下の手順で問題を解決することができます。
古いパスワードハッシュ形式を使用するように設定する
sudo nano /etc/mysql/my.cnf
my.cnf
ファイルに以下の行を追加します。
[client]
password_hash=0
ファイルを保存して閉じます。
MySQL/MariaDB を再起動する
sudo systemctl restart mysql
古いパスワードでログインできるはずです。
新しいパスワードハッシュ形式にアップグレードする
古いパスワードハッシュ形式はセキュリティが低いため、新しいパスワードハッシュ形式にアップグレードすることを推奨します。
mysql -u root -p
MySQL にログインしたら、以下のコマンドを実行します。
ALTER USER root@localhost PASSWORD REQUIRE PASSWORD('new_password');
FLUSH PRIVILEGES;
new_password
を新しいパスワードに置き換えます。
解説
認証プラグイン
MySQL/MariaDB は、ユーザー認証に使用される認証プラグインをサポートしています。 デフォルトの認証プラグインは、mysql_native_password
から caching_sha2_password
に変更されました。
パスワードハッシュ形式
MySQL/MariaDB は、パスワードを保存するためにパスワードハッシュ形式を使用します。 デフォルトのパスワードハッシュ形式は、OLD_PASSWORD
から caching_sha2_password
に変更されました。
古いパスワードハッシュ形式は、新しいパスワードハッシュ形式よりもセキュリティが低いため、推奨されていません。
新しいパスワードハッシュ形式は、古いパスワードハッシュ形式よりもセキュリティが向上しています。 また、より多くの文字と記号を使用できるため、パスワードを推測しにくくします。
注意事項
- 古いパスワードハッシュ形式を使用する場合は、セキュリティリスクが高まることに注意してください。
- 新しいパスワードハッシュ形式にアップグレードする場合は、すべてのユーザーのパスワードをリセットする必要があることに注意してください。
Creating a database and table
CREATE DATABASE mydatabase;
USE mydatabase;
CREATE TABLE mytable (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
Inserting data into a table
INSERT INTO mytable (name, email)
VALUES ('John Doe', '[email protected]'),
('Jane Doe', '[email protected]');
SELECT * FROM mytable;
SELECT id, name FROM mytable;
SELECT name, email FROM mytable WHERE email LIKE '%@example.com';
UPDATE mytable
SET name = 'John Smith', email = '[email protected]'
WHERE id = 1;
DELETE FROM mytable
WHERE id = 1;
Connecting to MySQL from PHP
<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "password";
$dbname = "mydatabase";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM mytable";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Installing MariaDB on Ubuntu
sudo apt-get update
sudo apt-get install mariadb-server mariadb-client
Securing MariaDB installation
sudo mysql_secure_installation
Backing up a MySQL/MariaDB database
mysqldump -u root -p mydatabase > mydatabase.sql
mysql -u root -p mydatabase < mydatabase.sql
Other Methods to Resolve "MariaDB is Not Checking Password on Ubuntu 15.04" Issue
Using the mysql_upgrade Tool
The mysql_upgrade
tool provided by the MariaDB package can be utilized to upgrade the password hash format without manually editing the my.cnf
file. Follow these steps:
- Stop the MariaDB service:
sudo systemctl stop mysql
- Execute the mysql_upgrade command:
sudo mysql_upgrade
sudo systemctl start mysql
- Login using the old password:
You should be able to log in using your existing password.
Resetting the Root Password
If you're unable to access the my.cnf
file or the mysql_upgrade
tool fails to resolve the issue, you can reset the root password using the following method:
- Boot into single-user mode:
Restart your system and select "Advanced options for Ubuntu" from the GRUB boot menu. Then, choose "Ubuntu, with Linux 4.4.0-101-generic (recovery mode)" and press Enter.
mount -o remount,rw /
echo mysql root | chpasswd -m
- Reboot the system.
After the system reboots, you should be able to log in to MariaDB using the new password "mysql".
Additional Considerations
- Upgrade to a Newer Ubuntu Version:
Consider upgrading to a newer Ubuntu version, such as Ubuntu 18.04 or 20.04, as these versions have updated MariaDB packages that utilize the newer and more secure caching_sha2_password
plugin by default.
- Regular Security Audits:
Conduct regular security audits of your MySQL/MariaDB server to ensure it's configured securely and up-to-date with the latest patches and security updates.
Remember:
- Modifying system configuration files and resetting passwords should be done with caution and proper backups in place.
- Always follow recommended security practices to protect your MySQL/MariaDB server from unauthorized access and potential data breaches.
mysql ubuntu mariadb