mysql常用操作 mysql備份與恢復

2021-07-10 20:21:31 字數 2364 閱讀 8284

先登入mysql  ==》mysql -uroot -p 

檢視資料庫的版本

select version();

檢視有哪些庫

show datases;

檢視當前處於哪個庫

select database();

檢視當前登入使用者

select user();

檢視某個庫下面的表;

use db; show tables;

檢視表的字段;

desc dong;

建立庫create database db1;

建立表create  table  tb1 (`id` int(4),`name`char(40))  engine=myisam default charset=gbk;

檢視建表語句

show create table tb1;

插入資料

insert into tb1 (`id`,`name`) values(1,'jxcia');   這裡的jxcia需要新增'',這是因為name定義的時候是char的字元。

另外預設也可以不需要新增(`id`, `name`),如果是只想新增name ,可以這樣。insert into tb1 (`name`) value('jxcia');   

檢視剛插入的資料 

select * from tb1;或者select *from tb1\g;

更新資料表的內容

update db.tb1 set id=1 where name='lin'; 將db庫下tb1表中名字為lin的id號改為1。

或者先進入db庫 use db ;  update tb1 set id=1 where name='lin';

清空表;

truncate table db1.tb1;

刪除表;

drop table db1.tb1;

刪除資料庫

drop database db1;

檢視mysql引數;

show varibales like 'max_connect%';  這裡的like類似於grep %類似於萬用字元。

修改mysql引數;

set global max_connnect_errors=100 

臨時修改max_connect_errors的值為100,如果想永久生效可以修改my.cnf的配置檔案。

flush privileges  重新整理許可權

檢視mysql佇列

show processlist;

建立普通使用者

create user 'user1'@'host' identified by '111';

建立普通使用者並授權;

grant all on *.* to user1  identified by '111';

如果是本地使用者

grant all on *.* to 

user1@localhost  identified by '111';

grant all on *.*  to 'user1'@'192.168.2.48' identified by 'lin';

grant all on db1. * to 'user2'@'192.168.2.%' identified by  'lin'; 針對乙個網段的使用者

刪除使用者

drop user 'username'@'host';

修復表repair table tb1

在shell命令下執行mysql的操作

mysql -uroot -plin mysql -e 「show tables」這裡的mysql指的是庫名字 -e 後面選項括起來的是mysql的命令。

二,mysql 備份與恢復

1,mysql備份

備份庫 以discuz為例

mysqldump -uroot -plin discuz > /data/discuz.sql

恢復庫mysql -uroot -plin  discuz

備份表mysqldump  -uroot -plin discuz  

pre_forum_post > /data/pre_forum_post.sql

恢復表mysql -uroot -plin  discuz <  /data/pre_forum_post.sql            discuz後無需再新增表名,只需要加資料名稱即可。

備份、恢復時指定字符集

mysql -uroot -plin --default-character-set=gbk   discuz >/data/discuz.sql

mysql -uroot -plin --default-character-set=gbk   discuz

mysql 冷備 Mysql資料冷備操作方法

定期的備份可使我們資料庫崩潰造成的損失大大降低。在mysql中進行資料備份的方法有兩種,一種是使用mysqldump程式,第二種是使用mysqlhotcopy cp tar或cpio等打包程式直接拷貝資料庫檔案。mysqldump程式備份資料庫較慢,但它生成的文字檔案便於移植。使用mysqlhotc...

my sql常用操作

1.grant allprivilegeson tomonty localhost identified by something with grant option monty 可以從任何地方連線伺服器的乙個完全的超級使用者,但是必須使用乙個口令 something 做這個。注意,我們必須對 mo...

mysql 常用操作

1 修改表名在mysql中修改表名的sql語句在使用mysql時,經常遇到表名不符合規範或標準,但是表裡已經有大量的資料了,如何保留資料,只更改表名呢?alter table table name rename to new table name 例如alter table admin user r...