現象
安裝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數據庫是存在的。
# ls -l /var/lib/mysql/ |grep "^d"
drwx------. 2 mysql mysql 4096 Sep 1 12:02 mysql
drwx------. 2 mysql mysql 4096 Sep 1 12:02 performance_schema
drwx------. 2 mysql mysql 4096 Sep 1 12:02 test
解決方案
可能是沒有權限導致:
嘗試賦權,但是mariadb報錯了。
MariaDB [information_schema]> grant all privileges on *.* to 'root'@'localhost';
ERROR 1045 (28000): Access denied for user ''@'localhost' (using password: NO)
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)
查看information_schema數據庫的USER_PRIVILEGES表,發現只有一條賦權數據,說明當前mariadb的賦權是有問題的。
在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后就可以看到所有的數據庫了。