Ubuntu Linux 9.04 下MySQL安裝與基本配置

來源:互聯網
上載者:User

今天第一次在Ubuntu Linux 9.04上安裝 MySQL,之前也沒安裝過,所以參考了上些網路文章,進行了一些配置,以下是我安裝的全過程

一、安裝和基本使用配置
我安裝是直接使用 apt-get 進行安裝,這也是我選擇的安裝方式,我的安裝方法如下:
1、sudo apt-get install mysql-server --安裝mysql服務端,我在9.04上安裝時提示設定root密碼,如果沒有可以在安裝好後變更設定,安裝後的預設路徑為: /etc/init.d/mysql
2、sudo apt-get install mysql-client --安裝mysql用戶端,用來操作mysql的CRUD操作,不過在9.04上安裝服務端時以經安裝好了用戶端可以不需要安裝,如果沒有則安裝
一旦安裝完成,MySql伺服器應該會自動啟動,你可以在終端輸入命令來查看是否啟動:
chentao@amber-chentao:~$ sudo netstat -tap | grep mysql
當你執行該命令時可以看到如下資訊
tcp 0 0 localhost:mysql *:* LISTEN 8368/mysqld
恭喜你,這說明mysql服務已啟動,安裝成功,如果不能啟動可以通過下列命令來重新啟動它
sudo /etc/init.d/mysql restart --restart

當確認mysql啟動後 我們需要進行登入到mysql,這個時候可以在終端輸入
chentao@amber-chentao:~$ mysql -uroot -p --執行Enter後提示輸入root密碼
Enter password:
當輸入密碼後就可進行對資料庫操作了
那麼如果你想設定mysql root 的密碼可以通過下列命令
chentao@amber-chentao:~$ sudo mysqladmin -u root -oldpassword password newpassword

還有二種方式進入mysql終端後變更密碼,就是進入mysql後,執行下列命令來更改目前使用者密碼
1、mysql>set password for root=password("root");
2、mysql> grant all privileges on *.* to root@localhost identified by "123456";

當所有的這些設定好了後我們可以來建立一個資料庫,並添加一個使用者來管理該資料庫的CRUD操作
--建立一個名為 test 資料庫
mysql> create database test;
--建立一個 test_root 的使用者,他將對資料庫 test 有CRUD的許可權,以後管理資料庫就可以用他來管理了,而不需要超級使用者root
mysql> grant all privileges on test.* to test_root@localhost identified by "654321"
--我們看到上面的配置是一個限定死的,test_root 使用者只能對test資料庫執行管理,有的時候我們不想只是限定在一個資料庫,我們想讓他對所有資料庫都擁用CRUD的許可權,那麼執行下面命令
mysql> grant all privileges on *.* to test_root@localhost identified by "654321";
或者你也可以將 all privileges 改成 insert,update,delete,create,select,二種方式實現都是一樣

mysql 有一個不好的地方就是,在安裝好後只允許在本地進行訪問,不允許通過遠端存取主機,如果想設定某使用者通過遠程主機訪問,這個時候需要設定mysql的配置,其體需要做二件事,如下:
1、mysql> grant all privileges on test_root@"%" identified by "654321"
2、chentao@amber-chentao:~$ sudo gedit /etc/mysql/my.cnf
在這裡我使用了gedit文本編輯方式開啟,當然你也可以使用 vi或vim 來執行編輯,在該檔案中找到 bind-address = 127.0.0.1 的位置,將其更改為 bind-address=你原生網路IP
這樣設定就可以允許你的mysql使用其它機器訪問了
至些你的mysql服務的基本配置就到些完成,下面是一些在網路上找的一些常用的mysql命令,附上以方便大家使用

/etc/init.d/mysql start --啟動mysql
/etc/init.d/mysql restart --重新啟動mysql
/etc/init.d/mysql shutdown --關閉mysql的命令
/etc/init.d/mysql stop --停止mysql 服務
chentao@amber-chentao:~$ mysql -uroot -p --登入mysql 伺服器,在上面有講解

下面還有關一些常用mysql內部操作提示符
show databases; -- 顯示所有資料庫列表
use test; --開啟庫
show tables; --查看找開資料庫中所有資料表
describe tableName; -- 查詢表結構
create table 表名(欄位設定表); --建立表
create database 資料庫名; --建立資料庫
drop database 資料庫名; --刪除資料庫
drop table tablename --刪除表結構
delete from 表名; --刪除表資料
select * from 表名; -- 查詢指定表中所有資料

聯繫我們

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