linux下Mysql 的安裝、配置、數據導入導出

<p><span>MySQL</span><span>是一種</span>開放源代碼<span>的關係型</span>數據庫管理<span>系統(RDBMS),<span>雖然功能未必很強大,但因它的免費開源而廣受歡迎。</span></span></p> <p><span><span>此次,接着上一篇《</span></span><a id="cb_post_title_url" href="http://www.cnblogs.com/alylee/archive/2013/05/03/LinuxInit_CDevelSetup.html">CentOs minimal安裝和開發環境部署</a>》,講下Linux環境mysql的安裝、初始化配置、以及參數優化,中間會穿插在實際操做過程當中遇到的新知識擴展。有些是承接上面的知識,若是不太清楚,能夠到上一篇去看下。</p> <p>1、安裝準備</p> <p>下載獲得安裝包很簡單。</p> <p>一、rpm安裝:A、在linux下使用wget獲取 B、在虛擬機所在的宿主機windows下,在網絡上下載Rpm安裝包,而後經過以前咱們配置的samba共享目錄拷貝到centos下。</p> <p>單獨下載要注意你須要的rpm包:</p> <div class="cnblogs_code"> <pre>MySQL-server-<span>5.1</span>.x-<span>0</span>.i386.rpm <span>//</span><span>服務端</span> MySQL-client-<span>5.1</span>.x-<span>0</span>.i386.rpm <span>//</span><span>客戶端 </span> MySQL-devel-<span>5.1</span>.x-<span>0</span>.i386.rpm <span>//</span><span>開發包<br />...</span></pre> </div>html

<p>二、yum 在線安裝:使用CentOs的Yum(若是有特定的Mysql版本需求,要肯定你的Yum庫裏有須要的mysql版本)</p>前端

<div class="cnblogs_code"> <pre>  Yum (Yellow dog Updater, Modified)是一個基於rpm包管理的字符前端軟件包管理器。可以從指定服務器自動下載rpm包而且安裝,處理依賴性關係,而且一次安裝全部依賴的軟件包,無須繁瑣地一次次下載、安裝。<br />  簡單認識下幾個操做:<br />  yum -y install samba  #安裝samba,「-y」接受全部,不用手動一個個去確認每一步。<br />  yum remove samba    #卸載samba<br />  yum info installed   #列出系統中<span style="text-decoration: underline">全部安裝過的</span>rpm包<br />  yum list samba     #列出資源庫中<span style="text-decoration: underline">指定的</span>能夠安裝或更新以及已經安裝的rpm包<br />  yum info samba     #列出資源庫中<span style="text-decoration: underline">指定的</span>能夠安裝或更新以及已經安裝的rpm包的信息<br />  yum search samba    #搜索匹配指定字符的rpm包  </pre> </div>mysql

<p>2、安裝Mysql</p>linux

<p>檢測當前系統是否已經安裝了mysql :運行下面的命令,若是已經安裝過,會顯示mysql的版本信息。</p>sql

<div class="cnblogs_code"> <pre>rpm -qa|<span style="color: #0000ff">grep</span> mysql </pre> </div>數據庫

<p>若是已經安裝過mysql,可是不是本身須要的版本,好比須要高版本的mysql對事件的支持等,怎麼辦?刪除唄!注意,這可能會刪除一些相關的依賴庫而影響到其餘軟件,不過能夠從新安裝。</p>windows

<div class="cnblogs_code"> <pre><span style="color: #0000ff">yum</span> remove mysql-server mysql-devel mysql-client #刪除mysql的server、client、以及devel</pre> </div>centos

<p>&#160; 一、rpm安裝:</p>安全

<div class="cnblogs_code"> <pre>rpm -ivh MySQL-server-<span style="color: #800080">5.1</span>.x-<span style="color: #800080">0</span><span style="color: #000000">.i386.rpm #安裝服務端(x:你下載的對應版本號) rpm </span>-ivh MySQL-client-<span style="color: #800080">5.1</span>.x-<span style="color: #800080">0</span><span style="color: #000000">.i386.rpm #安裝客戶端 rpm </span>-ivh MySQL-devel-<span style="color: #800080">5.1</span>.x-<span style="color: #800080">0</span>.i386.rpm #安裝開發包 </pre> </div>服務器

<p>&#160;&#160; 二、yum安裝:</p>

<div class="cnblogs_code"> <pre><span style="color: #0000ff">yum</span> -y <span style="color: #0000ff">install</span> mysql-<span style="color: #000000">server #安裝服務器端 </span><span style="color: #0000ff">yum</span> -y <span style="color: #0000ff">install</span> mysql-<span style="color: #000000">client #安裝客戶端 </span><span style="color: #0000ff">yum</span> -y <span style="color: #0000ff">install</span> mysql-devel #安裝開發包</pre> </div>

<p>&#160; 這裏注意:</p>

<p>A、根據你的開發環境須要,你能夠選擇對應的rpm包。你能夠在命令行,使用&quot;<span style="color: #333300">yum search mysql&quot;搜索與mysql有關的安裝包,從中選擇你須要的。若是你記不住名字也能夠這樣搜索下。</span></p>

<div class="cnblogs_code"> <pre><span style="color: #0000ff">yum</span> search mysql</pre> </div>

<p>&#160;<span style="color: #333300">&#160; <img alt="" src="http://images.cnitblog.com/blog/313639/201305/04001206-2425cffdf6794cefac832f8ff6186731.jpg" /></span></p>

<p><span style="color: #333300">B、上面列出rpm包名字和簡介,還能夠經過yum info獲取具體rpm包的具體的版本信息</span></p>

<div class="cnblogs_code"> <pre><span style="color: #0000ff">yum</span> <span style="color: #0000ff">info</span> mysql-<span style="color: #000000">server  #列出mysql-server版本信息 </span><span style="color: #0000ff">yum</span> <span style="color: #0000ff">info</span> mysql-<span style="color: #000000">client </span><span style="color: #0000ff">yum</span> <span style="color: #0000ff">info</span> mysql-<span style="color: #000000">devel

#或者 </span><span style="color: #0000ff">yum</span> <span style="color: #0000ff">info</span> mysql* #一個命令一會兒列出mysql相關的全部rpm安裝包版本信息</pre>

</div>

<p>&#160;<span style="color: #333300"> <img alt="" src="http://images.cnitblog.com/blog/313639/201305/04002240-ea4961dda62941679d9ae36e3dcabce1.jpg" /></span></p>

<p>&#160;</p>

<p>3、Mysql初始配置</p>

<p>剛剛安裝好的mysql須要進行必定的配置,才適合使用。</p>

<p>一、設置Mysql開機啓動</p>

<div class="cnblogs_code"> <pre><span style="color: #000000"><span style="color: #ff00ff">chkconfig --list mysql #查看mysql服務開機啓動狀態</span><br />mysql&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0:off1:off2:on3:on4:on5:on6:off<br /></span></pre>

<pre><span style="color: #000000"><span style="color: #ff00ff">#若是2--5都是on,就不須要下面的命令了<br />chkconfig mysql on 設置mysql服務開機啓動</span> </span></pre>

</div>

<p>chkconfig,若是不知道是幹什麼的,那如今來了解下</p>

<div class="cnblogs_code"> <pre><span style="color: #000000"><span style="color: #ff00ff">chkconfig</span>,用來更新(啓動或中止)和查詢系統服務的運行級信息。 使用語法: chkconfig [</span>--add][--del][--list][系統服務] 或 chkconfig [--level &lt;等級代號&gt;][系統服務][on/off/<span style="color: #000000">reset]</span><span style="color: #000000"> eg: chkconfig </span>--<span style="color: #000000">list [name]:顯示全部運行級系統服務的運行狀態信息(on或off)。若是指定了name,那麼只顯示指定的服務在不一樣運行級的狀態。 chkconfig </span>--<span style="color: #000000">add name:增長一項新的服務 chkconfig </span>--del name:刪除服務<span style="color: #000000"> chkconfig [</span>--level levels] name:設置某一服務在指定的運行級是被啓動,中止仍是重置。<br /><br /><br /><span style="color: #ff00ff">level</span>,運行級別,能夠用「runlevel」這個命令查詢到當前系統的運行級別,也能夠「init 0-6」切換運行級別</pre>

<pre><span>0:表示關機 1:單用戶模式 2:無網絡鏈接的多用戶命令行模式 3:有網絡鏈接的多用戶命令行模式 4:不可用 5:帶圖形界面的多用戶模式 6:從新啓動 </span></pre>

</div>

<p>二、Mysql服務的啓動/關閉/重啓</p>

<div class="cnblogs_code"> <pre>service mysql start/shutdown/restart/<span style="color: #000000">status  #啓動、關閉、重啓、查看服務當前狀態 或者 </span>/etc/rc.d/init.d/mysqld start/shutdown/restart/status  #啓動、關閉、重啓、查看服務當前狀態</pre> </div>

<p>三、安全設置</p>

<div class="cnblogs_code"> <pre><span style="color: #000000">⑴、剛安裝時,root用戶是空的,須要修改密碼:   ①mysqladmin </span><span style="color: #808080">-</span>u root password ‘new<span style="color: #808080">-</span><span style="color: #000000">password’   ②或者,登錄後,直接修改用戶表裏的密碼     mysql </span><span style="color: #808080">-</span><span style="color: #000000">u root #用root用戶登陸     </span><span style="color: #800000; font-weight: bold">1</span>. <span style="color: #0000ff">select</span> <span style="color: #ff00ff">user</span>,host,password <span style="color: #0000ff">from</span> mysql.<span style="color: #ff00ff">user</span><span style="color: #000000">; 查看數據庫的用戶     </span><span style="color: #800000; font-weight: bold">2</span>. <span style="color: #0000ff">use</span><span style="color: #000000"> mysql #切換到mysql表     </span><span style="color: #0000ff">set</span> password <span style="color: #0000ff">for</span> root<span style="color: #008000">@localhost</span><span style="color: #808080">=</span>password(<span style="color: #ff0000">'</span><span style="color: #ff0000">123456</span><span style="color: #ff0000">'</span><span style="color: #000000">); #設置指定域名下root帳號的密碼      </span><span style="color: #0000ff">set</span> password <span style="color: #0000ff">for</span> root<span style="color: #008000">@域名</span><span style="color: #808080">=</span>password(<span style="color: #ff0000">'</span><span style="color: #ff0000">123456</span><span style="color: #ff0000">'</span><span style="color: #000000">);     </span><span style="color: #800000; font-weight: bold">3</span><span style="color: #000000">.或者,更常見的sql操做:     </span><span style="color: #0000ff">update</span> <span style="color: #ff00ff">user</span> <span style="color: #0000ff">set</span> password<span style="color: #808080">=</span>password(<span style="color: #ff0000">'</span><span style="color: #ff0000">123456</span><span style="color: #ff0000">'</span>) <span style="color: #0000ff">where</span> <span style="color: #ff00ff">user</span><span style="color: #808080">=</span>‘root<span style="color: #008000">@localhost</span><span style="color: #000000">’;<br /> ⑵、匿名用戶是危險的<br />      </span><span style="color: #0000ff">delete</span> <span style="color: #0000ff">from</span> mysql.<span style="color: #ff00ff">user</span> <span style="color: #0000ff">where</span> <span style="color: #ff00ff">user</span><span style="color: #808080">=</span><span style="color: #ff0000">''</span>; #刪除匿名用戶<br />⑶、退出mysql:  <span style="color: #0000ff">exit</span>;</pre> </div>

<p>四、測試</p>

<p>登錄:</p>

<div class="cnblogs_code"> <pre><span style="color: #ff0000">[</span><span style="color: #ff0000">root@dream ~</span><span style="color: #ff0000">]</span>mysql <span style="color: #808080">-</span><span style="color: #000000">u root #匿名登陸,已設置密碼或者刪除了匿名用戶,登錄失敗 </span><span style="color: #ff0000">[</span><span style="color: #ff0000">root@dream ~</span><span style="color: #ff0000">]</span>mysql <span style="color: #808080">-</span>u root <span style="color: #808080">-</span><span style="color: #000000">p  #經過密碼用root登陸 Enter password: #在這裏輸入密碼123456 </span><span style="color: #ff0000">[</span><span style="color: #ff0000">root@dream ~</span><span style="color: #ff0000">]</span>mysql <span style="color: #808080">-</span>u root <span style="color: #808080">-</span>p <span style="color: #800000; font-weight: bold">123456</span>  #顯示密碼登錄</pre> </div>

<p>sql操做:</p>

<div class="cnblogs_code"> <pre>mysql<span style="color: #808080">&gt;</span> <span style="color: #0000ff">create</span> <span style="color: #0000ff">database</span><span style="color: #000000"> test; #創建名爲test的數據庫,這個庫安裝後就有的。 mysql</span><span style="color: #808080">&gt;</span><span style="color: #000000"> show databases; #查看系統已存在的數據庫 mysql</span><span style="color: #808080">&gt;</span> <span style="color: #0000ff">use</span><span style="color: #000000"> test  #切換到指定的數據庫 mysql</span><span style="color: #808080">&gt;</span> <span style="color: #0000ff">create</span> <span style="color: #0000ff">table</span> tbl_user(ID <span style="color: #0000ff">int</span>, Name <span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">50</span><span style="color: #000000">)); #創建表 mysql</span><span style="color: #808080">&gt;</span><span style="color: #000000"> show tables;  #查看數據庫中已存在的表 mysql</span><span style="color: #808080">&gt;</span> <span style="color: #0000ff">drop</span> <span style="color: #0000ff">table</span><span style="color: #000000"> tbl_user; #刪除表 mysql</span><span style="color: #808080">&gt;</span><span style="color: #0000ff">drop</span> <span style="color: #0000ff">database</span><span style="color: #000000"> test; #刪除名爲test數據庫 mysql</span><span style="color: #808080">&gt;</span> <span style="color: #0000ff">exit</span> #退出MySQL服務器</pre> </div>

<p>&#160;</p>

<p>五、NaviCat遠程鏈接的權限問題</p>

<pre>遠程登陸會出現以下提示: </pre>

<div class="cnblogs_code"> <pre><span style="color: #800000; font-weight: bold">1103</span> <span style="color: #808080">-</span> host xxx.xxx.xxx.xx <span style="color: #0000ff">is</span> <span style="color: #808080">not</span> allowed <span style="color: #0000ff">to</span> connec <span style="color: #0000ff">to</span><span style="color: #000000"> this mysql server. 或 </span><span style="color: #800000; font-weight: bold">1045</span> <span style="color: #808080">-</span> Access denied <span style="color: #0000ff">for</span> <span style="color: #ff00ff">user</span>&#160;<span style="color: #ff0000">'</span><span style="color: #ff0000">root</span><span style="color: #ff0000">'</span>@<span style="color: #ff0000">'</span><span style="color: #ff0000">192.168.1.11</span><span style="color: #ff0000">'</span> (using password: YES).</pre> </div>

<p>解決方案:</p>

<div class="cnblogs_code"> <pre><span style="color: #808080">//</span><span style="color: #000000">mysql訪問 </span><span style="color: #0000ff">grant</span> <span style="color: #808080">all</span> <span style="color: #0000ff">privileges</span> <span style="color: #0000ff">on</span> <span style="color: #808080">*</span>.<span style="color: #808080">*</span> <span style="color: #0000ff">to</span> <span style="color: #ff0000">'</span><span style="color: #ff0000">root</span><span style="color: #ff0000">'</span>@<span style="color: #ff0000">'</span><span style="color: #ff0000">%</span><span style="color: #ff0000">'</span> identified <span style="color: #0000ff">by</span> <span style="color: #ff0000">'</span><span style="color: #ff0000">123456</span><span style="color: #ff0000">'</span><span style="color: #000000">;

#其實就是權限問題,設置root在全部域名(</span>%<span style="font-family: 宋體">表示任何地址</span>)登錄時,賦給全部權限,密碼123456。根據須要可設置更嚴格些。</pre>

</div>

<p>&#160;</p>

<p>4、數據庫和表的導入、導出</p>

<p>1.導出整個的數據庫:<span style="color: #ff6600">mysqldump -u 用戶名 -p 數據庫名 &gt; 導出的文件名</span></p>

<p><span style="color: #ff00ff">mysqldump -u root -p db_test &gt;/home/bak/dbtest.sql</span></p>

<p>2.導出指定數據庫中的一個表:<span style="color: #ff6600">mysqldump -u 用戶名 -p 數據庫名 表名&gt; 導出的文件名</span></p>

<p><span style="color: #ff00ff">mysqldump -u root -p db_test tbl_user &gt;/home/bak/test_user.sql</span></p>

<p>3.只導出一個結構(struct):加上-d參數</p>

<p><span>mysqldump -u root -p -d –add-drop-table db_test &gt;/home/bak/dbtest_struct.sql</span></p>

<p><span>4.只導出一個數據(data):加上-t參數</span></p>

<p><span>mysqldump -u root -p -t –add-drop-table db_test &gt;/home/bak/dbtest_struct.sql</span></p>

<div class="cnblogs_code"> <pre><span style="color: #000000">⑴.只導出表結構 -d   mysqldump </span><span style="color: #808080">-</span>h 192.<span style="color: #800000; font-weight: bold">168.1</span>.<span style="color: #800000; font-weight: bold">11</span> <span style="color: #808080">-</span>u root <span style="color: #808080">-</span>p 123456 <span style="color: #808080">-</span>d –<span style="color: #0000ff">default</span><span style="color: #808080">-</span><span style="color: #0000ff">character</span><span style="color: #808080">-</span><span style="color: #0000ff">set</span><span style="color: #808080">=</span>utf8 test <span style="color: #808080">&gt;</span><span style="color: #000000">sql.sql

⑵.只導出數據 -t   mysqldump </span><span style="color: #808080">-</span>h 192.<span style="color: #800000; font-weight: bold">168.1</span>.<span style="color: #800000; font-weight: bold">11</span> <span style="color: #808080">-</span>u root <span style="color: #808080">-</span>p 123456 <span style="color: #808080">-</span>t –<span style="color: #0000ff">default</span><span style="color: #808080">-</span><span style="color: #0000ff">character</span><span style="color: #808080">-</span><span style="color: #0000ff">set</span><span style="color: #808080">=</span>utf8 test <span style="color: #808080">></span>sql.sql<br /><br />注:①.<span style="color: #ff00ff">-d</span> 沒有數據(不導出數據,只導出數據結構和表結構)</pre>

<p>&#160; <span style="color: #ff00ff">-t</span> 不導出結構,只要數據</p>

<p><span style="color: #ff00ff">–add-drop-table</span> 在每一個create語句以前增長一個drop table</p>

<p><span style="color: #ff00ff">–default-character-set</span> 設置字符集</p>

<p><span style="color: #ff00ff"><span style="color: #000000">②.</span></span>這裏的導出文件沒有加路徑,就是相對路徑,當前的目錄下的文件。</p>

<p><span style="color: #ff00ff"><span style="color: #000000">③.</span></span>密碼是顯示輸入的,跟在-p後面。</p> </div>

<p>5.導入數據庫:source 命令</p>

<div class="cnblogs_code"> <pre><span style="color: #ff0000">[</span><span style="color: #ff0000">root@dream ~</span><span style="color: #ff0000">]</span> mysql <span style="color: #808080">-</span>u root <span style="color: #808080">-</span><span style="color: #000000">p #登錄 mysql</span><span style="color: #808080">&gt;</span><span style="color: #0000ff">use</span><span style="color: #000000"> test; mysql</span><span style="color: #808080">&gt;</span>source <span style="color: #808080">/</span>home<span style="color: #808080">/</span>bak<span style="color: #808080">/</span>db_test.sql</pre> </div>

<p>&#160;</p>

<p><a href="http://my.oschina.net/alylee/blog/187507">歡迎轉載,條件容許請註明出處,謝謝</a>! </p>

<p>做者:<a href="http://www.cnblogs.com/alylee/" target="_blank">子韋一</a></p>

相關文章
相關標籤/搜索