mariadb數據庫
1.安裝mysql
yum search mariadb 查找與mariadb相關的軟件包
yum install mariadb mariadb-server -y 安裝maridb的server軟件和client軟件
啓動mariadb服務
systemctl start mariadb
systemctl enable mariadb
mariadb監聽的端口
netstat -antlpe | grep mysql
ss -antlpe | grep mysql
vim /etc/services 所有的服務與端口默認的對應關係
只允許本地連接,阻斷所有來自網絡的連接
vim /etc/my.cnf
skip-networking=1
systemctl restart mariadb
2.mariadb的初始化
設置mysql的登陸密碼
mysql_secure_installation
mysql -uroot -p
mysql基本操作語句
show databases; 顯示數據庫,類似於目錄,裏面包含多個表
use mysql; 進入名稱爲mysql的數據庫中的表
show tables; 顯示該數據庫中的表
desc user; 顯示錶的結構
select * from user; 顯示user表中的內容
select Host,User,Password from user; 顯示錶中某幾列
create database cooffee; 創建以數據庫名稱爲cooffee
create table cooffeeuser( 創建表
-> username varchar(10) not null,
-> passwd varchar(6) not null);
insert into cooffeeuser values('user1','123'); 向表中插入數據
insert into cooffeeuser(passwd,username) values('456','user2'); 按照指定順序向表中插入數據
update cooffeeuser set passwd='456' where username='user1'; 更新表中的內容
alter table cooffeeuser add sex varchar(5); 添加sex列到cooffeeuser表中
delete from cooffeeuser where username='user1'; 出表中用戶名爲user1的記錄
drop table cooffeeuser; 除表
drop database cooffee; 除數據庫
用戶和訪問權限的操作
create user cooffee@localhost identified by 'hello'; 創建用戶cooffee,可在本機登陸,密碼爲hello
create user cooffee@'%' identified by 'cooffee'; 創建用戶cooffee,可在遠程登陸,密碼爲cooffee
create database mariadb;
grant all on mariadb.* to cooffee@localhost; 給cooffee@localhost用戶授權,如果爲all,授權所有權限 (insert,update,delete,select,create)
flush privileges; 刷新,重載受權表
show grants for cooffee@localhost; 查看用戶受權
revoke delete,update on mariadb.* from cooffee@localhost; 刪除指定用戶授權
drop user cooffee@localhost; 刪除用戶
忘記mysql用戶密碼時,怎麼找回
- 關閉mariadb服務
systemctl stop mariadb.service 跳過受權表
mysqld_safe –skip-grant-table &
修改root密碼
mysql
update mysql.user set Password=password(‘cooffee’) where User=’root’;
- 關閉跳過授權表的進程,啓動mariadb服務,使用新密碼即可
ps aux | grep mysql
kill -9 pid
mysql -uroot -p
5.mysql的備份與恢復
備份:
mysqldump -uroot -p mariadb > mariadb.dump
mysqldump -uroot -pcooffee --no-data mariadb > 'date +%Y_%m_%d'_mariadb.dump 備份mariadb數據庫,不備份數據庫中的數據
mysqldump -uroot -pcooffee --all-databases > mariadb4.dump 備份所有的數據庫
恢復:
mysqladmin -uroot -p create mariadb3
mysql -uroot -p mariadb3< mar/mariadb.dump