NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here. Enter current password for root (enter for none): //设置数据库root密码 OK, successfully used password, moving on... Setting the root password or using the unix_socket ensures that nobody can log into the MariaDB root user without the proper authorisation. You already have your root account protected, so you can safely answer 'n'. Switch to unix_socket authentication [Y/n] n //切换到unix套接字身份验证 ... skipping. You already have your root account protected, so you can safely answer 'n'. Change the root password? [Y/n] n ... skipping. By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y //移除匿名账户 ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y //禁止root账户远程登陆 ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y //删除test数据库和对此数据库访问权限 - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y // 刷新权限 ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure.
Thanks for using MariaDB!
切换到root账户登陆mariadb
1 2 3 4 5 6 7 8 9 10 11 12 13
$ su 密码: root@yourUserName-pc:# mariadb -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 40 Server version: 10.6.5-MariaDB-1:10.6.5+maria~bullseye mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
MariaDB [(none)]>
二、设置权限相关
1 2 3
$ mariadb -uroot -p Enter password: ERROR 1698 (28000): Access denied for user 'root'@'localhost'
解决办法如下: 跳过安全规则
1 2 3 4 5 6 7 8 9 10
$ sudo mysqld_safe --skip-grant-tables $ mysql -u root -p Enter password: //无密码,直接回车 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 10.6.5-MariaDB-1:10.6.5+maria~bullseye mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
此时root权限如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
MariaDB [(none)]> show grants for'root'@'localhost'; +-----------------------------------------------------------------------------------------------------------------------------------------+ | Grants for root@localhost | +-----------------------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO `root`@`localhost` IDENTIFIED VIA mysql_native_password USING 'invalid' OR unix_socket WITH GRANT OPTION | | GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION | +-----------------------------------------------------------------------------------------------------------------------------------------+ 2 rows inset (0.000 sec) # 刷新权限 MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.147 sec) MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'you password'; // 重新设置密码 Query OK, 0 rows affected (0.022 sec)
MariaDB [(none)]> \q Bye
重新登陆:
1 2 3 4 5 6 7 8 9 10 11
$ mariadb -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 10.6.5-MariaDB-1:10.6.5+maria~bullseye mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
MariaDB [(none)]>
(1)查看权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
MariaDB [(none)]> show grants for'root'@'localhost'; +----------------------------------------------------------------------------------------------------------------------------------------+ | Grants for root@localhost | +----------------------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO `root`@`localhost` IDENTIFIED BY PASSWORD '*0F57*******' WITH GRANT OPTION | | GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION | +----------------------------------------------------------------------------------------------------------------------------------------+ 2 rows inset (0.000 sec)
MariaDB [(none)]> show grants for'yourUserName'@'localhost'; +--------------------------------------------------------------------------------------------------------------------+ | Grants for yourUserName@localhost | +--------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO `yourUserName`@`localhost` IDENTIFIED BY PASSWORD '*0F571********' | +--------------------------------------------------------------------------------------------------------------------+ 1 row inset (0.000 sec)
(2)授予权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO `yourUserName`@`localhost` IDENTIFIED BY 'password'; /* 其中`*.*`表示所有数据库中所有表; 'password' 表示密码;*/ MariaDB [(none)]> GRANT ALL PRIVILEGES ON `novel_plus`.* TO `yourUserName`@`localhost`; Query OK, 0 rows affected (0.011 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.147 sec) MariaDB [(none)]> show grants for'yourUserName'@'localhost'; +--------------------------------------------------------------------------------------------------------------------+ | Grants for yourUserName@localhost | +--------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO `yourUserName`@`localhost` IDENTIFIED BY PASSWORD '*0F571A639A45A10D51B1F0F65FF29E5D3*******' | | GRANT ALL PRIVILEGES ON `novel_plus`.* TO `yourUserName`@`localhost` | +--------------------------------------------------------------------------------------------------------------------+ 2 rows inset (0.000 sec)
(3)取消权限
1 2 3 4 5 6 7 8 9 10
MariaDB [(none)]> REVOKE ALL PRIVILEGES ON `novel_plus`.* FROM `yourUserName`@`localhost`; Query OK, 0 rows affected (0.026 sec)
MariaDB [(none)]> show grants for'yourUserName'@'localhost'; +--------------------------------------------------------------------------------------------------------------------+ | Grants for yourUserName@localhost | +--------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO `yourUserName`@`localhost` IDENTIFIED BY PASSWORD '*0F571A639A45A10D51B1F0F65FF29E5D3736C4E0' | +--------------------------------------------------------------------------------------------------------------------+ 1 row inset (0.000 sec)
mysql> GRANT system_user ON *.* to 'root'; Query OK, 0 rows affected (0.07 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'yourUserName'@'%'; Query OK, 0 rows affected (0.62 sec) mysql> REVOKE ALL PRIVILEGES ON *.* FROM 'yourUserName'@'%'; Query OK, 0 rows affected (0.16 sec)] mysql> SET PASSWORD FOR 'yourUserName'@'%'='abc'; Query OK, 0 rows affected (0.20 sec)
MariaDB [study_test]> UPDATE usr_table SET is_vip = 1 WHERE usr_id=(SELECT usr_id FROM usr_table WHERE usr_id IN (SELECT usr_id FROM usr_score_table GROUP BY usr_id HAVING COUNT(usr_id) >= 3)); Query OK, 1 rows affected (0.014 sec) Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [study_test]> UPDATE usr_table SET is_vip = 1 WHERE usr_id IN (SELECT usr_id FROM usr_table WHERE usr_id I N (SELECT usr_id FROM usr_score_table GROUP BY usr_id HAVING COUNT(usr_id) >= 2)); Query OK, 2 rows affected (0.014 sec) Rows matched: 5 Changed: 2 Warnings: 0
$ mariadb --help mariadb Ver 15.1 Distrib 10.6.5-MariaDB, for debian-linux-gnu (x86_64) using readline EditLine wrapper Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Usage: mariadb [OPTIONS] [database]
Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf