linux下安裝mysql及命令學習

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   color   os   使用   sp   

目錄:

  1. 安裝mysql

  2. mysql檔案部署

  3. mysql啟停命令

  4. 串連資料庫

  5. 操作資料庫命令

  6. 資料的匯入匯出

  7. 其他

 

1.安裝mysql

1、使用yun安裝mysql,可自動安裝完成

yun install mysql mysql-server  #詢問是否要安裝,輸入Y即可自動安裝,知道安裝完成

 2、為mysql的root賬戶設定密碼

mysql_secure_installation

  斷行符號,根據提示輸入Y
      輸入2次密碼,斷行符號
      根據提示一路輸入Y
      最後出現:Thanks for using MySQL!
      MySql密碼設定完成,重新啟動 MySQL:

2.mysql檔案部署

mysql安裝完它的資料庫檔案、設定檔和命令檔案分別在不同的目錄 

1.資料庫目錄
    /var/lib/mysql/
2.設定檔
    /usr/share/mysql(mysql.server命令及設定檔)mysql5.5之後的預設安裝路徑,mysql5.5之前的是/usr/local/mysql
3.相關命令
    /usr/bin(mysqladmin mysqldump等命令)
4、啟動指令碼
  /etc/rc.d/init.d/(啟動指令檔mysql的目錄)

3.mysql啟停命令   

1.linux下啟動mysql的命令:

/etc/init.d/mysqld start      #啟動

2.linux下重啟mysql的命令:

/ect/init.d/mysqld restart    #重啟

3.linux下關閉mysql的命令:  

/ect/init.d/mysqld shutdown   #關閉

4.linux下停止mysql的命令:

/etc/init.d/mysqld stop       #停止

5.設為開機啟動

chkconfig mysqld on 
/sbin/chkconfig #查看自啟動列表  
4.串連資料庫

1、串連本機上的mysql

mysql -uroot -proot

2、修改mysql密碼:

mysqladmin -u使用者名稱 -p舊密碼 password 新密碼
5.操作資料庫命令

1、顯示資料庫列表。 

show databases;

2、顯示庫中的資料表:

use mysql;      #開啟庫show tables;     #顯示所有表

3、顯示資料表的結構:

describe 表名; 
或show columns from tableName;

4、建庫:

create database 庫名;#指定編碼格式GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;UTF8: CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

5、建表:

use 庫名;create table 表名(欄位設定列表);

6、刪庫和刪表:

drop database 庫名; #刪庫drop table 表名;  #刪表

7、將表中記錄清空:

delete from 表名;
或truncate table 表名;

8、顯示表中的記錄:

select * from 表名;

9、編碼的修改
  如果要改變整個mysql的編碼格式:
  啟動mysql的時候,mysqld_safe命令列加入
    --default-character-set=gbk
  如果要改變某個庫的編碼格式:在mysql提示符後輸入命令

alter database db_name default character set gbk;

10、重新命名表

alter table t1 rename t2;

11、查看sql語句的效率

explain < table_name >例如:explain select * from t3 where id=3952602;

12、用文本方式將資料裝入資料庫表中(例如D:/mysql.txt)

mysql> LOAD DATA LOCAL INFILE "D:/mysql.txt" INTO TABLE MYTABLE;

13、查詢資料庫資訊

select now();       #查詢時間select user();      #查詢目前使用者select version();   #查詢資料庫版本select database();  #查詢當前使用的資料庫
6.資料的匯入匯出

1、文本資料轉到資料庫中
文本資料應符合的格式:欄位資料之間用tab鍵隔開,null值用來代替。

例:
  1 name duty 2006-11-23
資料傳入命令:

load data local infile "檔案名稱" into table 表名; #只是匯入資料

2、匯出資料庫和表
mysqldump是外部命令,備份指定資料庫中的所有表:

mysqldump -h localhost -uroot -proot databaseName > /tmp/dump.sql

3、執行sql檔案語句

mysql>source news.sql;(在mysql命令下執行sql語句表)
 7.其他

1、刪除student_course資料庫中的students資料表:
rm -f student_course/students.*


2、備份資料庫:(將資料庫test備份)
mysqldump -u root -p test>c:\test.txt
備份表格:(備份test資料庫下的mytable表格)
mysqldump -u root -p test mytable>c:\test.txt
將備份資料匯入到資料庫:(導回test資料庫)
mysql -u root -p test

3、建立暫存資料表:(建立暫存資料表zengchao)
create temporary table zengchao(name varchar(10));

4、建立表是先判斷表是否存在
create table if not exists students(……);

5、從已經有的表中複製表的結構
create table table2 select * from table1 where 1<>1;

6、複製表
create table table2 select * from table1;

7、對錶重新命名
alter table table1 rename as table2;

8、修改列的類型
alter table table1 modify id int unsigned;//修改列id的類型為int unsigned
alter table table1 change id sid int unsigned;//修改列id的名字為sid,而且把屬性修改為int unsigned

9、建立索引
alter table table1 add index ind_id (id);
create index ind_id on table1 (id);
create unique index ind_id on table1 (id);//建立唯一性索引

10、刪除索引
drop index idx_id on table1;
alter table table1 drop index ind_id;

11、聯合字元或者多個列(將列id與":"和列name和"="串連)
select concat(id,‘:‘,name,‘=‘) from students;

12、limit(選出10到20條)<第一個記錄集的編號是0>
select * from students order by id limit 9,10;

13、MySQL不支援的功能
事務,視圖,外鍵和參考完整性,預存程序和觸發器

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.