Linux營運常用的 MySQL基礎命令

來源:互聯網
上載者:User

標籤:linux營運常用的 mysql基礎命令

MySQL基礎命令

-------------

建立MySQL庫,授權。

>create database mysqldb character set utf8;

>grant all privileges on mysqldb.* to ‘mysqluser‘@‘localhost‘ identified by ‘mysqlpasswd‘;

>flush privileges;


建立了mysqldb資料庫,建立mysqluser使用者,並授權mysqldb的所有許可權。___________________________________________________________________________


設定遠端存取MySQL

>grant all privileges on mysqldb.* to ‘mysqluser‘@‘%‘ identified by ‘mysqlpasswd‘;

# mysqluser是使用者名稱,%代表任意主機,‘mysqlpasswd‘指定的登入密碼(這個和本地的密碼可以設定不同的,互不影響)

>flush privileges; # 重載系統許可權


設定只允許192.168.1.2的主機用root使用者訪問MySQL

>grant all privileges on *.* to ‘root‘@‘192.168.1.2‘ identified by ‘123456‘ with grant option;

>flush privileges;


設定防火牆,讓 3306 連接埠對外可訪問

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

iptables -nL 

service iptables save 


設定允許192.168.1.1/24 網段的所有主機用root使用者訪問MySQL

>grant all privileges on *.* to ‘root‘@‘192.168.1.%‘ identified by ‘123456‘ with grant option;

flush privileges;

_________________________________________________________________


MySQL匯出匯入

匯出資料庫用mysqldump命令(注意mysql的安裝路徑,即此命令的路徑):


1、只匯出表結構( -d 參數)

mysqldump -u使用者名稱 -p密碼 -d 資料庫名 > 資料庫名.sql

#/usr/local/mysql/bin/mysqldump -uroot -p‘password‘ -d mysqldb > abc.sql


匯出表結構多幾個參數,但沒什麼壞處。

mysqldump -uroot -p --default-character-set=utf8 -R -E --triggers  --single-transaction --master-data=2 --hex-blob -B  -d mysqldb > mysqldb_str.sql



2、資料庫匯入匯出:

mysqldump -u使用者名稱 -p密碼  資料庫名 > 備份檔案名.sql


匯出

#/usr/local/mysql/bin/mysqldump -uroot -p‘password‘ mysqldb > bak.sql


#/usr/local/mysql/bin/mysqldump -uroot -p‘password‘  --routines --events --single-transaction  --all-databases --master-data=2 | gzip > 2017-07-17_bak.sql


匯入

#/usr/local/mysql/bin/mysqldump -uroot -p‘password‘ mysqldb < bak.sql


_________________________________________________________________________


查看所有MySQL使用者

>SELECT DISTINCT CONCAT(‘User: ‘‘‘,user,‘‘‘@‘‘‘,host,‘‘‘;‘) AS query FROM mysql.user;


查看資料庫中具體某個使用者的許可權

>show grants for ‘root‘@‘%‘;

>select * from mysql.user where user=‘root‘ \G

----------------------------------------------------

重設MySQL root密碼

mysqladmin -uroot password ‘mysql_pass‘; (設定mysql的root密碼)

  1. 停止正在啟動並執行MySQL進程

    netstat -anput | grep mysql

    ps -ef | grep mysql

    先查看mysql的pid,然後殺掉。或者service mysql stop停止。

 kill pid


2.以安全模式啟動MySQL

 mysqld_safe --skip-grant-tables &

   注意mysqld_safe 命令的路徑


3.更改MySQL資料庫root密碼

>use mysql     

>update user set password=password("new_pass") where user="root";  

>flush privileges;    


4.重啟MySQL

service mysql restart 

———————————————————————————————————

雖然公司有專業的DBA,在營運工作中,難免對資料庫的一些接觸,也是必須的。

所以整理了幾條MySQL的命令,這些都是我在工作中常用的。也非常實用。

因為我是營運新手,所以這幾條命令也夠我應付工作中遇到的大部分MySQL問題了。

營運=背鍋俠,對資料備份這塊一定要做好,我深有感觸。

有一次開發誤刪資料庫,怪我備份沒做好。其實我也挺冤的,其實我備份做了,還通過ftp同步到其他伺服器上儲存,但是開發在前幾天就把ftp使用者名稱密碼改了,還是我的問題。許可權沒控制好。

唉!!! 誰讓我年輕呢,多背背鍋,也挺好。

                                                     背黑鍋我們最專業

                                                     ------營運


Linux營運常用的 MySQL基礎命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.