MySQL 安裝與使用 一 什么是 MySQL MySQL (發音為 My Ess Que Ell )是 Tcx 公司()開發的一個多人使用 多執行緒的 SQL 資料庫 Server MySQL 主要的目標在快速 穩定和容易使用 MySQL 可在此 取得 二 MySQL 的安裝 本文所使用的 MySQL 版本為 mysql tar gz(原始碼檔) 作業環境為 RedHat +CLE MySQL 預設情況下會安裝至 /usr/local 目錄下 不過為了日后移除方便 建議將 mysql 獨立安裝在 /usr/local/mysql 目錄 底下為安裝 MySQL 的步驟 取得 mysql tar gz 后 于 /usr/local 目錄下解開 # cd /usr/local# tar zxvf mysql tar gz# cd mysql 設定 configure 安裝選項 選擇安裝目錄 (prefix)以及支援中文 Big 碼(with charset=big ) # /configure prefix=/usr/local/mysql # with charset=big 開始編譯并安裝 # make# make install# scripts/mysql_install_db最后一個步驟是用來產生 MySQL grant tables(會建立一個 mysql 資料庫和一些 tables 用來管理使用 MySQL 的授權資訊 也就是使用者有哪些使用資料庫的權限) 三 啟動 停止 MySQL 要啟動 MySQL 的方法 (以本文將 MySQL 安裝在 /usr/local/mysql 為例)# /usr/local/mysql/share/mysql server start注意在第一次執行前 須將 mysql server 設成可執行(chmod mysql server) 另外可將這行指令加在 /etc/rc d/rc local 檔中 讓 MySQL 在開機時自動啟動 要停止 MySQL 的方法 # /usr/local/mysql/bin/mysqladmin shutdown如果你為 MySQL Administrator root 帳號(非作業系統的 root)設了密碼 要停止 MySQL 則必須像下列這樣做 MySQL 會詢問你 root 的密碼后才會執行 shutdown 的工作 # /usr/local/mysql/bin/mysqladmin u root p shutdown 四 管理與使用 MySQL 簡介 在你開始前MySQL 提供了許多工具 (Client Tools)來與 MySQL 資料庫 Server 連線 其中最主要的為 mysql 交談式連線工具與 mysqladmin 公用程式 大部份時候使用者會用 mysql 來和資料庫 Server 交談 底下就以 mysql 連線工具來介紹如何維護與使用 MySQL (以本文安裝為例 mysql 工具位于 /usr/local/mysql/bin/mysql) mysql 的使用語法如下 mysql [ u username] [ h host] [ p[password]] [dbname]MySQL 資料庫有自己一套使用者帳號與權限管控方法 所以這邊所指定的 username 與 password 是 MySQL 的使用者與密碼 而不是作業系統的使用者與密碼(當然任何使用者都能執行 mysql 然后以 MySQL 的任何帳號進入使用) 在你第一次安裝好 MySQL 時 MySQL 的管理帳號為 root 沒有設定密碼 (非作業系統的 root) 所以在開始前 請先照下列步驟為 root 設好密碼 使用 mysql 與 MySQL 資料庫 Server 連線 # /usr/local/mysql/bin/mysql u root mysqlReading table information for pletion of table and column namesYou can turn off this feature to get a quicker startup with AWele to the MySQL monitor Commands end with ; or \g Your MySQL connection id is to server version: Type help for help mysql在下了 mysql u root mysql 指令 指定以 root 帳號并開啟 mysql 系統資料庫 連線至 MySQL 后 會看到一些提示訊息與 mysql 工具的提示符號 以后大部份的工作皆在此提示符號下完成 更改 MySQL系統管理者 root 密碼 mysql update user set password=password( 新密碼 ) where user= root ;Query OK rows affected ( sec)Rows matched: Changed: Warnings: mysql FLUSH PRIVILEGES;Query OK rows affected ( sec)mysql quitBye注意每個指令后要加上一個分號 ; 才會讓 mysql 開始執行 而第二道指令會讓已載入記憶體的 mysql 系統資料庫更新 最后離開 mysql 工具程式 在更新 root 密碼后 日后要與 MySQL 連線的方法為 mysql u root p新密碼或者是這樣 讓 mysql 詢問 root 的密碼 mysql u root p資料庫維護接下來 我們以簡單的通訊錄資料庫作為例子 來介紹如何用 mysql 工具程式來做資料庫的維護(新增 授權 資料表維護等) 首先 以 MySQL root 帳號連線后建立一 addbook 資料庫 # /usr/local/mysql/bin/mysql u root pEnter password:Wele to the MySQL monitor Commands end with ; or \g Your MySQL connection id is to server version: Type help for help mysql create databae addbook;Query OK row affected ( sec)指定使用 addbook 資料庫 并建立一個 friends 資料表 mysql use addbook;Database changedmysql create table friends ( name Char( ) telphone VarChar( ) icq Char( ) address VarChar( ) );Query OK rows affected ( sec)新增幾筆資料 并查詢看看 mysql insert into friends values( maa 臺北縣新莊市 );Query OK row affected ( sec)mysql insert into friends (name icq telphone address ) Values ( cxlin 臺北縣 );Query OK row affected ( sec)mysql select * from friends;+ + + + +| name | telphone | icq | address |+ + + + +| maa | | | 臺北縣新莊市 || cxlin | | | 臺北縣 |+ + + + + rows in set ( sec)第二個 insert 指令指定了資料欄位的插入順序 用法較第一個為彈性 而第一個指令必須依資料表建立結構時的順序插入資料 更新 刪除資料表記錄 mysql update friends set address = 桃園縣 where name = cxlin ;Query OK row affected ( sec)Rows matched: Changed: Warnings: mysql select * from friends where name = cxlin ;+ + + + +| name | telphone | icq | address |+ + + + +| cxlin | | | 桃園縣 |+ + + + + row in set ( sec)mysql delete from friends where name = maa ;Query OK row affected ( sec)mysql select * from friends;+ + + + +| name | telphone | icq | address |+ + + + +| cxlin | | | 桃園縣 |+ + + + + row in set ( sec)最后 建好資料庫與資料表后 把 addbook 資料庫中所有資料表的使用權限(select insert update delete)授權給 maa@localhost(再次提醒 此處的 maa 為 MySQL 的使用者帳號 而非作業系統的 maa 帳號) mysql grant select insert update delete on addbook * to maa@localhost identified by ;Query OK rows affected ( sec)之后 可用 maa 的身份進入 MySQL 存取 addbook 資料庫 # /usr/local/mysql/bin/mysql u maa p addbookEnter password:Reading table information for pletion of table and column namesYou can turn off this feature to get a quicker startup with AWele to the MySQL monitor Commands end with ; or \g Your MySQL connection id is to server version: Type help for help mysql status /mysql Ver Distrib for pc linux gnu (i )Connection id: Current database: addbookCurrent user: maa@localhostServer version Protocol version Connection Localhost via UNIX socketUNIX socket /tmp/mysql sockUptime: hours min secThreads: Questions: Slow queries: Opens: Flush tables: Open lishixinzhi/Article/program/MySQL/201311/29503
網站建設哪家好,找創新互聯建站!專注于網頁設計、網站建設、微信開發、小程序開發、集團企業網站建設等服務項目。為回饋新老客戶創新互聯還提供了汾陽免費建站歡迎大家使用!
步驟/方法1
打開下載的安裝文件,出現如下界面:
2
mysql安裝向導啟動,點擊“next”繼續。
3
選擇安裝類型,有“Typical(默認)”、“Complete(完全)”、“Custom(用戶自定義)”三個選項,我們選擇“Custom”,有更多的選項,也方便熟悉安裝過程。
4
在“MySQL Server(MySQL服務器)”上左鍵單擊,選擇“This feature, and all subfeatures, will be installed on local hard drive.”,即“此部分,及下屬子部分內容,全部安裝在本地硬盤上”。點選“Change...”,手動指定安裝目錄。
5
確認一下先前的設置,如果有誤,按“Back”返回重做。按“Install”開始安裝。
6
正在安裝中,請稍候,直到出現下面的界面。
7
點擊“next”繼續,出現如下界面。
8
現在軟件安裝完成了,出現上面的界面,這里有一個很好的功能,mysql 配置向導,不用向以前一樣,自己手動亂七八糟的配置my.ini 了,將“Configure the Mysql Server now”前面的勾打上,點“Finish”結束軟件的安裝并啟動mysql配置向導。
9
點擊“Finsh”,出現如下界面,MySQL Server配置向導啟動。
10
點擊“next”出現如下界面:
選擇配置方式,“Detailed Configuration(手動精確配置)”、“Standard Configuration(標準配置)”,我們選擇“Detailed Configuration”,方便熟悉配置過程。
選擇服務器類型,“Developer Machine(開發測試類,mysql 占用很少資源)”、“Server Machine(服務器類型,mysql占用較多資源)”、“Dedicated MySQL Server Machine(專門的數據庫服務器,mysql占用所有可用資源)”,大家根據自己的類型選擇了,一般選“Server Machine”,不會太少,也不會占滿。
選擇mysql數據庫的大致用途,“Multifunctional Database(通用多功能型,好)”、“Transactional Database Only(服務器類型,專注于事務處理,一般)”、“Non-Transactional Database Only(非事務處理型,較簡單,主要做一些監控、記數用,對MyISAM數據類型的支持僅限于non-transactional),隨自己的用途而選擇了,我這里選擇“Transactional Database Only”,按“Next”繼續。
對InnoDB Tablespace進行配置,就是為InnoDB 數據庫文件選擇一個存儲空間,如果修改了,要記住位置,重裝的時候要選擇一樣的地方,否則可能會造成數據庫損壞,當然,對數據庫做個備份就沒問題了,這里不詳述。我這里沒有修改,使用默認位置,直接按“Next”繼續。
選擇您的網站的一般mysql 訪問量,同時連接的數目,“Decision Support(DSS)/OLAP(20個左右)”、“Online Transaction Processing(OLTP)(500個左右)”、“Manual Setting(手動設置,自己輸一個數)”,我這里選“Online Transaction Processing(OLTP)”,自己的服務器,應該夠用了,按“Next”繼續。
是否啟用TCP/IP連接,設定端口,如果不啟用,就只能在自己的機器上訪問mysql 數據庫了,我這里啟用,把前面的勾打上,Port Number:3306,在這個頁面上,您還可以選擇“啟用標準模式”Enable Strict Mode),這樣MySQL就不會允許細小的語法錯誤。如果您還是個新手,我建議您取消標準模式以減少麻煩。但熟悉MySQL以后,盡量使用標準模式,因為它可以降低有害數據進入數據庫的可能性。還有一個關于防火墻的設置“Add firewall exception ……”需要選中,將MYSQL服務的監聽端口加為windows防火墻例外,避免防火墻阻斷。按“Next”繼續。
注意:如果要用原來數據庫的數據,最好能確定原來數據庫用的是什么編碼,如果這里設置的編碼和原來數據庫數據的編碼不一致,在使用的時候可能會出現亂碼。這個比較重要,就是對mysql默認數據庫語言編碼進行設置,第一個是西文編碼,第二個是多字節的通用utf8編碼,都不是我們通用的編碼,這里選擇第三個,然后在Character Set 那里選擇或填入“gbk”,當然也可以用“gb2312”,區別就是gbk的字庫容量大,包括了gb
使用yum安裝mysql數據庫的軟件包 [root@xuegod63 ~]# yum -y install mariadb-server mariadb 。
注:? mariadb-server?? #MariaDB數據庫 mariadb? ? ? # MariaDB服務器Linux下客戶端 。
注:從centos7系統開始,系統中自帶的mysql數據庫變成了mariadb-server,mariadb-server和mysql操作上一樣。mariadb-server是mysql的一個分支。
啟動數據庫服務[root@xuegod63 ~]# systemctl start? mariadb? #啟動MariaDB服務。[root@xuegod63 ~]# systemctl enable? mariadb? #設置開啟自動啟動MariaDB服務。
安裝完mariadb-server后,運行mysql_secure_installation去除安全隱患,[root@xuegod63 ~]# mysql_secure_installation #進入安全配置導向。
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQLSERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current,password for the root user. If you've just installed MySQL, and,you haven't set the root password yet, the password will be blank,,so you should just press enter here.Enter current password for root (enter for none):?? #初次運行直接回車,因為root用戶沒有密碼。
OK, successfully used password, moving on,Setting the root password ensures that nobody can log into the MySQL,root user without the proper authorisation.,Set root password? [Y/n] Y #是否設置root用戶密碼,輸入Y。
New password: 123456?? #新密碼123456,Re-enter new password: 123456,Password updated successfully!Remove anonymous users? [Y/n] Y?? #是否刪除匿名用戶,生產環境建議刪除,所以直接回車或Y。
Success!Normally, root should only be allowed to connect from 'localhost'.? Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] Y? #是否禁止root遠程登錄,根據自己的需求選擇Y/n并回車建議禁止。
Success!By default, MariaDB comes with a database named 'test' that anyone canaccess.? This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] Y?? #是否刪除test數據庫,直接回車或Y。