MySQL 基本操作
阿新 • • 發佈:2021-09-20
一、設定密碼
mysqladmin -uroot password '+密碼' //設定 MySQL 密碼
mysql -uroot -p+密碼 //登入 MySQL
忘記了 MySQL 密碼怎麼辦呢?不要急下面就來了解如何忘記密碼後更換密碼。
先要找到 /etc/my.cnf 檔案來配置它。
skip-grant //忽略使用者直接登入 service msyql sertart //重啟 MySQL
配置完成之後 service msyql sertart 重啟 MySQL 方可使用。mysql -uroot 進入 MySQL 去修改裡面的密碼即可。
# 進入 MySQL
[root@bogon ~]# mysql -uroot
# 進入 MySQL 這個使用者
mysql> use mysql;
# 在 MySQL 這個使用者中去修改 password
mysql> update user set password=password('000000') where user='root';
Ctrl+D //退出 MySQL
修改完成之後把 /etc/my.cnf 檔案裡修改的那條程式碼刪掉。
二、連結 MySQL
mysql -uroot -p //隱藏密碼式登入 MySQL mysql -uroot -p+密碼 -h +IP地址 -P3306 //遠端登陸 MySQL (p是大寫) mysql -uroot -p+密碼 -S /rmp/mysql.sock //和直接連結一樣 mysql -uroot -p+密碼 -e "show databases;" //Linux 下檢視 MySQL 的庫
這是開啟可以遠端連結的教程。Linux中 MySQL 授權遠端連線的方法步驟_Mysql_指令碼之家 (jb51.net)
三、常用命令
# 查詢庫
show databases;
# 切換庫
use mysql;
# 檢視庫裡的表
show tables;
# 查看錶裡的欄位
desc tb_name;
# 檢視建表語句
show create table tb_name\G;
# 檢視當前使用者
select user();
# 檢視當前使用的資料庫
select database();
# 建立庫
create database db1;
# 建立表
use db1;
create table t1(`id` int(4), `name` char(40));
# 刪除表
drop table table_name;
# 檢視當前資料庫版本
select version();
# 檢視資料庫狀態
show status;
# 檢視各引數
show variables;
# 檢視指定引數
show variables like 'max_connect%';
# 修改引數
set global max_connect_errors=1000;
# 檢視佇列
show processlist; show full processlist;
# 檢視行數
select count(*) from mysql.user;
# 檢視mysql資料庫db表所有內容
select * from mysql.db;
# 檢視 db這一個欄位
select db from mysql.db;
# 檢視 db,user這兩個欄位
select db,user from mysql.db;
# 檢視 host欄位 滿足地址為192.168.欄位的資訊
select * from mysql.db where host like '192.168.%';
# 在db1資料庫t1表插入資料
insert into db1.t1 values (1, 'abc');
# 更新 改
update db1.t1 set name='aaa' where id=1;
# 刪除一行資料
delete from db1.t1 where id=3;
# 清空表,但是儲存表結構
truncate table db1.t1;
# 刪除表
drop table db1.t1;
# 刪除庫
drop database db1;
四、使用者管理
都是在進入MySQL後輸入的。
select user,host from mysql.user; //檢視用使用者
show grants //預設檢視root的授權
show grants for user2@192.168.0.0; //檢視某個使用者授權
/root/.mysql_history //檢視歷史檔案
grant 授權、all 所有許可權 增刪改查、on 在哪個庫、*.* 表示對所有的庫,所有的表、to 對哪一個使用者, 也可以指定來源IP、identified 認證方式,by 密碼
grant all on *.* to 'user1' identified by '000000'; //給user使用者授權(給user1 授權另一個所有許可權密碼是000000)
IP登入(例:grant all on *.* to 'user1'@'127.0.0.1' identified by 'passwd';)
grant all on *.* to 'user1'@'+本地地址' identified by '+密碼'; //使用ip才能登陸(單個IP)
使用某個段登入(例:grant all on *.* to 'user1'@'192.168.200.%' identified by 'passwd';)
grant all on *.* to 'user1'@'192.168.200.%' identified by '+密碼'; //使用某個段登入
使用sock登入
grant all on *.* to 'user1'@'localhost' identified by 'passwd';
grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd';
grant all on db1.* to 'user3'@'%' identified by 'passwd';
五、備份
在 Linux 下備份的 MySQL(例如:mysqldump -uroot -p000000 mysql > /tmp/mysql.sql)
# 備份庫
mysqldump -uroot -p+密碼 mysql > /tmp/mysql.sql
# 恢復庫
mysql -uroot -p+密碼 mysql < /tmp/mysql.sql
# 備份表
mysqldump -uroot -p+密碼 mysql user > /tmp/user.sql
# 恢復表
mysql -uroot -p+密碼 mysql < /tmp/user.sql
# 備份所有庫
mysqldump -uroot -p+密碼 -A >/tmp/123.sql
# 只備份表結構
mysqldump -uroot -p+密碼 -d mysql > /tmp/mysql.sql