1. 程式人生 > 其它 >Mariadb不顯示mysql資料庫問題排查

Mariadb不顯示mysql資料庫問題排查

現象

安裝mariadb-server後,使用systemctl start mariadb啟動mariadb,並使用mysql -uroot -p登入資料庫。
執行show databases;命令檢視資料庫,發現只有information_schema和test資料庫:

\# mysql -u root -p
Enter password: 
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+

2 rows in set (0.00 sec)
正常情況下檢視資料庫應該有:information_schema;mysql;performance_schema;test四個資料庫。
但是到檔案系統中檢視/var/lib/mysql路徑下是有mysql目錄的,代表mysql資料庫是存在的。

 MariaDB [(none)]> use information_schema;
 MariaDB [information_schema]> select * from USER_PRIVILEGES;
+----------------+---------------+----------------+--------------+
| GRANTEE        | TABLE_CATALOG | PRIVILEGE_TYPE | IS_GRANTABLE |
+----------------+---------------+----------------+--------------+
| ''@'localhost' | def           | USAGE          | NO           |
+----------------+---------------+----------------+--------------+
1 row in set (0.00 sec)
MariaDB [information_schema]> grant all privileges on  *.* to 'root'@'localhost';
ERROR 1045 (28000): Access denied for user ''@'localhost' (using password: NO)

解決方案

可能是沒有許可權導致:
在mariadb資料庫的配置檔案/etc/my.cnf的[mysqld]配置段中新增配置項skip-grant-tables,重啟mariadb服務。再次登入mariadb檢視資料庫,可以看到所有的資料庫都可見了:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

嘗試給root使用者賦權可以檢視所有的資料庫,報錯:

MariaDB [(none)]> grant all privileges on *.* to 'root'@'localhost'; 
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement  #意思是使用了配置引數--skip-grant-tables後不可以修改許可權。
可以通過執行flush privileges;解決以上報錯,然後執行賦權操作並給root使用者配置密碼:
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> grant all privileges on *.* to 'root'@'localhost';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> SET password for 'root'@'localhost'=password('sql123');
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit

退出資料庫並將之前在/etc/my.cnf中的配置項#skip-grant-tables註釋,然後重啟mariadb,再次登入mariadb後就可以看到所有的資料庫了。

學無止境,你說呢?