Linux學習筆記(MySql操作)

來源:互聯網
上載者:User

Linux學習筆記(MySql操作)

忘記MySql密碼:

編輯mysql主設定檔 my.cnf 在[mysqld]欄位下添加參數 skip-grant

重啟資料庫服務,這樣就可以進入資料庫不用授權了 mysql -uroot

修改相應使用者密碼 use mysql;

update user setpassword=password('密碼') where user='root';

flushprivileges; (重新整理)

最後修改/etc/my.cnf 去掉 skip-grant, 重啟mysql服務

 

查看root使用者:

mysql -uroot -p密碼 -e "use mysql;select user,host from user where user='root';"

 

mysq的命令曆史:/root/.mysql_history

這樣不安全,先刪除這個檔案,然後指向黑洞,就不會產生命令曆史了

 

mysql:

-h:加ip

-S:可以使用指定的sock檔案

-P:加連接埠號碼

-u:加使用者名稱

-p:加密碼

show databases;:顯示資料庫

use database;:選擇資料庫

show tables;:查看資料庫中的表

 

查看都有哪些庫 show databases;
查看某個庫的表 use db; show tables;
查看錶的欄位 desc tb;
查看建表語句 show create table tb;
當前是哪個使用者 select user();
當前庫 select database();
建立庫 create database db1;
建立表 create table t1 (`id`int(4), `name` char(40));
查看資料庫版本 select version();
查看mysql狀態 show status;
修改mysql參數

show variables like '%timeout%';

set global table_open_cache= 128;


查看mysql隊列

showprocesslist; (相當於ps)


建立普通使用者並授權

grant all on *.* to user identified by '密碼';

grant all on db1.* to 'user'@'10.0.2.100' identified by '密碼';

grant all on db1.* to 'user3'@'%' identified by '密碼';
更改密碼

UPDATE mysql.user SET password=PASSWORD("newpwd")WHERE user='username' ;
查詢

select count(*) from mysql.user;

select * from mysql.db;

select * from mysql.db where host like '10.0.%';
插入

update db1.t1 set name='*****' where id=1;
清空表

truncate table db1.t1;
刪除表

drop table db1.t1;
刪除資料庫

drop database db1;
修複表

repair table tb1 [use_frm];(通過/data/www/資料庫名/這個檔案下面的檔案修複)

mysql的備份和恢複:

備份 mysqldump -uroot -p db > 1.sql

-d:只是備份建立表的語句
恢複 mysql -uroot -p db < 1.sql
只備份一個表 mysqldump -uroot -p dbtb1 > 2.sql
備份時指定字元集mysqldump -uroot -p --default-character-set=utf8 db > 1.sql
恢複也指定字元集 mysql -uroot -p --default-character-set=utf8 db < 1.sql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.