MySQL / MariaDB 數據庫常用操作命令

1.基本操作命令

# systemctl start mariadb.service #啓動數據庫
# mysql -u root -p  			  #進入數據庫
# MariaDB [(none)]> alter user 'root'@'localhost' identified by '***'; #修改root密碼
# MariaDB [(none)]> show status;                                       #顯示數據庫的狀態信息
# MariaDB [(none)]> use mysql;                                         #選擇數據mysql
用戶管理:
# MariaDB [mysql]> select user from user; #查看所有用戶
# MariaDB [mysql]> create user bob;       #創建用戶
# MariaDB [mysql]> drop user bob;         #刪除用戶
# MariaDB [mysql]> show grants for bob;   #查看用戶安全權限
# MariaDB [mysql]> grant select on *.* to bob;  #授予用戶bob的 select 訪問權限
# MariaDB [mysql]> revoke select on *.* to bob; #取消用戶bob的 select 訪問權限
# MariaDB [mysql]> set password for bob = password('***'); #修改用戶bob的密碼
# MariaDB [mysql]> set password = password('***');         #修改當前用戶的密碼
創建數據庫 abc:
# MariaDB [mysql]> create databases abc;    #創建數據庫 abc
# MariaDB [mysql]> use abc;                 #選擇數據庫 abc
# MariaDB [mysql]> show databases;          #顯示數據庫列表
# MariaDB [mysql]> show tables;             #顯示數據庫內的表
# MariaDB [mysql]> show columns from ***;   #顯示錶列
# MariaDB [mysql]> describe ***;            #顯示錶列的快捷方式
# MariaDB [mysql]> exit or quit  			#退出數據庫
數據庫服務的狀態、開啓、關閉:
# service mysql.server status
# service mysql.server start
# service mysql.server stop

2.導入數據庫文件

導入數據庫文件 abc.sql:
# mysql -uroot -p*** abc<文件名.sql
導出數據庫文件 abc.sql:
# mysqldump -uroot -p*** abc<abc.sql
使用 source 導入 abc.sql:
# MariaDB [mysql]> use abc;
# MariaDB [abc]> set names utf8;  #設置編碼
# MariaDB [abc]> source /home/abc/abc.sql;

3.附件(abc.sql)

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------------
--  Table structure for `websites`
-- ----------------------------------
DROP TABLE IF EXISTS `websites`;
CREATE TABLE `websites` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL DEFAULT '' COMMENT '站點名稱',
  `url` varchar(255) NOT NULL DEFAULT '',
  `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
  `country` char(10) NOT NULL DEFAULT '' COMMENT '國家',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `websites`
-- ----------------------------
BEGIN;
INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', '淘寶', 'https://www.taobao.com/', '13', 'CN'), ('3', '百度', 'http://www.baidu.com/', '8', 'CN'), ('4', '微博', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章