Linux下對MYSQL資料庫的操作

來源:互聯網
上載者:User

首先在ubuntu/debian上安裝下列軟體包:mysql-client-
5.0      mysql-server-5.0
   
在Redhat上安裝下列軟體包:mysql-3.23.54a-11.i386.rpm  
mysql-server-3.23.54a-11.i386.rpm
和mysql-devel-3.23.54a-11.i386.rmp    (可以裝上更高的版本,本文在ubuntu下實驗)

1、在終端運
行:mysql_install_db
   (這一步好像可以不要)

      /*啟動mysqld

伺服器並且建立初始MySQL
授權表,包含決定使用者如何被允許串連伺服器的許可權*/
2、啟動資料庫


       方法一:終端運行:sudo mymysqld_safe  
&
        //啟動mysql資料庫
       方法二:終端運行:sudo /etc/init.d/mysql
start
                 
3、檢查MYSQL是否被啟動


     方法一:終端運行:pstree | grep mysqld

     出現如下結果則表明已經啟動:
      | -mysqld_safe-+-logger
      |              `-mysqld---9*[{mysqld}]
     方法二:終端運行:sudo /etc/init.d/mysql  
status

     出現如下結果則表明已經啟動:(不同的系統版本略有差異)
       * /usr/bin/mysqladmin   Ver 8.41 Distrib 5.0.24a, for
pc-linux-gnu on i486
     Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX
DataKonsult AB
     This software comes with ABSOLUTELY NO WARRANTY. This is free
software,
     and you are welcome to modify and redistribute it under the GPL
license
     Server version           5.0.24a-Debian_9-log
     Protocol version         10
     Connection               Localhost via UNIX socket
     UNIX socket              /var/run/mysqld/mysqld.sock
     Uptime:                  1 min 23 sec
     Threads: 1   Questions: 147   Slow queries: 0   Opens: 98   Flush
tables: 1   Open tables: 14   Queries per second avg: 1.771
另:如想開機後自動啟動,可下載一個sysvconfig命令:sudo
apt-get install sysvconfig
(Redhat裡有這個命令可用:ntsysv)   
然後在終端運行:sudo sysvconfig   在menu中進入Enable/Disable,將mysql選中儲存即可。
4、關閉MYSQL資料庫

     方法一、sudo /etc/init.d/mysql   stop

     方法二、sudo mysqladmin   shutdown

5、進入資料庫

     終端運行:mysql -u root -p
     
//進入密碼為空白,mysql第一次預設root使用者密碼為空白,敲斷行符號即可
6、建立資料庫

      mysql> create database test;
   
//建立test資料庫
       輸出結果:Query OK, 1 row affected (0.00 sec)
      mysql> show databases;
    
//顯示所有的資料庫
     輸出結果:
     +--------------------+
     | Database                                 |
     +--------------------+
     | information_schema             |
     | bear                                         |
     | mysql                                       |
     | test                                         |
     +--------------------+
     4 rows in set (0.00 sec)
7、刪除資料庫


    mysql> database test;
  
//刪除test資料庫
     輸出結果:
     Query OK, 0 rows affected (0.00 sec)
    mysql> show databases;
   
//顯示所有的資料庫
     輸出結果:
     +--------------------+
     | Database                                 |
     +--------------------+
     | information_schema             |
     | bear                                         |         
     | mysql                                       |
     +--------------------+
     3 rows in set (0.01 sec)
8、建立表


    mysql> create database test;
  
//建立test資料庫
    mysql> use test;
  
//進入到test資料庫中
    mysql> show tables ;

//查看所有的表
     輸出顯示結果:Empty set (0.00 sec)
    mysql> create table people(  
//
建立一個表名為people的表,四列分別是:
     -> people_id char(6) primary
key,  
//people_id     為主鍵
     -> name
char(10),                             
//name
     -> sex
char(2),                               
//sex
     -> birthday
date);                         
//birthday
     輸出結果:
     Query OK, 0 rows affected (0.02 sec)   
    mysql> show tables ;  
//顯
示所有的表
     輸出結果:
     +----------------+
     | Tables_in_test            |
     +----------------+
     | people                            |
     +----------------+
     1 row in set (0.00 sec)
9、刪除表

     /* 這一步可到最後做,因為下面還要用到表people */
     mysql>    table   people;
  
//刪除表格
     mysql> show tables ;  
//
顯示所有的表
10、查看錶結構


    mysql> use test;  
  
//進入要查看錶結構的資料庫
    mysql> describe people;  
  
//查看錶結構
     顯示結果:
     +--------+-------+----+----+------+-----+
     |   Field           |   Type         |   Null |   Key    |  
Default |   Extra   |
     +--------+-------+----+--- +------+------+
     | people_id    |   char(6)    |   NO     |   PRI   
|                 |                |
     | name             |   char(10)   |   YES   |            |     
NULL   |                |
     | sex               |   char(2)    |   YES   |             |     
NULL   |                |
     | birthday     |   date         |   YES   |             |     
NULL   |                |
     +--------+-------+---+-----+-----+------+
    4 rows in set (0.00 sec)
11、向表中添加和查看資料

    mysql> use test;  
    
//進入test資料庫
    mysql> into people
values         
//向people表中添加兩組資料
         ->
('200701','熊鋒','m','1983/04/23');

    mysql> into people values

        
->('200702','鄭玲','w','1986-08-18');

   mysql> select * people;  
        
//查看people表中的資料
    顯示結果:
     +-----------+--------+------+------------+
     |      people_id      |      name         |      sex     
|          birthday      |
     +-----------+--------+------+------------+
     |      200701             |      熊鋒        |        m       
|       1983-04-23     |
     |      200702            |      鄭玲        |         w       |      
1986-08-18      |
     +-----------+--------+------+------------+
2 rows in set (0.00 sec)
mysql> select * people
people_id='200701';

//查看people表中主索引值為200701的資料
    顯示結果:
+-----------+--------+------+---------+
| people_id          | name            |       sex     | birthday       |
+-----------+--------+------+---------+
| 200701                 | 熊鋒            |       m         | 1983-04-23 |
+-----------+--------+------+---------+
1 row in set (0.00 sec)
12、修改表結構


mysql> alter table people add score
int(3) not null;

顯示結果:
Query OK, 2 rows affected (0.01 sec)
Records: 2   Duplicates: 0   Warnings: 0
mysql> describe people;  
//顯示
表結構
顯示結果:
+--------+-------+----+----+------+----+
|   Field           |   Type        |   Null |   Key   |   Default |  
Extra |
+--------+-------+----+---+-------+----+
| people_id   |   char(6)    |   NO     |   PRI |                  
|             |
| name            |   char(10)   |   YES   |          |       NULL   
|             |
| sex              |   char(2)     |   YES   |          |      NULL    
|             |
| birthday    |   date          |   YES   |          |      NULL    
|             |
| core            |   int(11)       |   YES   |          |      NULL    
|             |
| score          |   int(3)       |    NO     |        
|                    |             |
+--------+-------+----+--+--------+----+
6 rows in set (0.00 sec)

註:MYSQL中的資料類型

Data Type                          
Default Value

char/varchar/binary          ""

date                                      
0000-00-00

time                                      
00:00:00

datetime                          
0000-00-00 00:00:00

timestamp                       
2005-06-21 12:01:00(now()!)

year                                  
0000

enum('a','b','c')                   
'a'

set('a','b','c')                     
()(empty set)

聯繫我們

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