mariadb

1.數據庫部署

yum install mariadb-server -y
systemctl start mariadb

2.安全初始化

vim /etc/my.cnf
skip-networking=1     ##關閉數據庫開啓的網絡接口;默認情況下,數據庫網絡接口是打開的,爲了數據庫的安全需要關閉網絡接口

systemctl restart mariadb

測試:ss -antlupe

這裏寫圖片描述

這裏寫圖片描述

[root@localhost yum.repos.d]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   ##第一次修改密碼時因不知道當前mysql的密碼可直接按回車鍵
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y              ##是否要設定數據庫超級用戶密碼
New password:                           ##輸入要設定的超級用戶密碼
Re-enter new password:                  ##重新輸入
Password updated successfully!
Reloading privilege tables..
 ... Success!


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    ##是否禁止超級用戶通過遠程登陸
 ... 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     ##刷新數據庫
 - 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!

3.數據庫的基本sql語句操作
(1)登陸

mysql -uroot -p  ##-u表示用戶,後面緊接用戶名;-p表示密碼,後面可緊跟密碼,但是容易造成服務器密碼泄露,故建議使用mysql -uroot -p命令登陸服務器

這裏寫圖片描述

(2)數據庫及表的建立

CREATE DATABASE westos;              ##新建westos庫
CREATE TABLE linux (                 ##新建表格linux
username varchar(15) not null,       ##表格linux的表頭爲username,字符長度爲15,不能爲空
password varchar(15) not null        ##表格linux的表頭爲password,字符長度爲15,不能爲空
 );
INSERT INTO linux VALUES ('user1','passwd1');  ##向表格linux插入用戶爲user1,密碼爲pasword1

這裏寫圖片描述

這裏寫圖片描述

(3)查詢

SHOW DATABASES;             ##查詢數據庫
USE mysql                    ##進入數據庫mysql
SHOW  TABLES;               ##顯示數據庫mysql中的表格
SELECT *  FROM westos;      ##查詢user表中的所有內容(*可以用此表中的任何字段來代替)
DESC  linux;                ##查看數據庫westos表格linux的表頭 

這裏寫圖片描述

(4)更新數據庫信息

UPDATE  linux SET password='666' where username='li';      
##更新用戶li的密碼爲666
UPDATE linux SET password='456' where ( username='user1' or username='user2' );
##更新user1和user2的密碼
UPDATE linux SET password='666' where username='li' and age='22'; ##更新用戶爲liqie年紀爲22的用戶密碼爲666

這裏寫圖片描述

DELETE FROM linux where username='li';               ##刪除表格Linux中用戶li的數據
ALTER TABLE linux ADD age varchar(4);                ##在表格linux中添加表頭age
ALTER TABLE linux ADD class varchar(5) after password;   ##在表格linux的password後面添加class

這裏寫圖片描述

(5)刪除數據庫

DELETE FROM linux where username='xu';   ##刪除xu的數據從linux表中
DROP TABLE linux;                        ##刪除linux表
DROP DATABASE westos;                    ##刪除westos庫

這裏寫圖片描述

(6)數據庫的備份

mysqldump -u root -predhat --all-database       
##備份所有表中的左右數據
mysqldump -u root -predhat --all-database --no-data 
##備份所有表,但不備份數據
mysqldump -u root -predhat westos                  ##備份westos庫
mysqldump -u root -predhat westos  > /mnt/westos.sql    
##備份westos庫並把數據保存到westos.sql中
mysqldump -uroot -predhat westos linux > /mnt/linux.sql 
##備份westos庫中的linux表
mysqldump -uroot -predhat westos test > /mnt/test.sql   
##備份westos庫中的test表
mysql -uroot -predhat -e "create database westos;"  ##建立westos庫
mysql -uroot -predhat westos < /mnt/westos.sql 
##把數據導入westos庫(westos庫必須存在)

這裏寫圖片描述

這裏寫圖片描述

(7)用戶授權

CREATE USER westos@'localhost' identified by 'westos';  
##添加本地用戶
CREATE USER lee@'%' identified by 'lee';        
##建立用戶lee,此用戶可以通過網絡登陸
SELECT Host,User FROM mysql.user;       ##查看mysql的用戶
GRANT SELECT ON WESTOS.* TO westos@localhost;           
##授予本地用戶在WESTOS庫擁有SELECT權限
GRANT SELECT ON WESTOS.* TO westos@'%';
SHOW GRANTS FOR westos@localhost;                       
##查看本地用戶westos的權限
SHOW GRANTS FOR westos@'%';
REVOKE SELECT ON WESTOS.* FROM westos@localhost;        
##去掉用戶本地westos在WESTOS庫的SELECT權限
drop user lee@'%'                        ##刪除用戶
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章