今天在公司看到一個環境是用二進制格式包安裝的mysql,下面我們也來試試看,如何用二進制格式包安裝mysql,然後基本的操作mysql。
首先解壓:
# tar -zxvf mysql-5.5.33-linux2.6-i686.tar.gz -C /usr/local/
# cd /usr/local
# ln -sv mysql-5.5.33-linux2.6-i686/ mysql
修改檔案屬組:
# chown root:mysql mysql/*
# cd support-files/
# ls
這裡面的檔案都是樣例
# cp my-large.cnf /etc/my.cnf 配置檔案
# cp mysql.server /etc/init.d/mysqld 服務腳本檔案
加入service 管理,設定開機啟動。
# chkconfig --add mysqld
# chkconfig --list mysqld
執行初始化腳本:
rm -rf data/*
# ./scripts/mysql_install_db --help
# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/data
初始化完成,這樣資料檔案夾就有預設的資料檔案了。
添加PATH環境變量:
# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
# . /etc/profile.d/mysql.sh
連結頭檔案:
# ln -sv /usr/local/mysql/include/ /usr/include/mysql
導出庫檔案:
# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
# ldconfig -v | grep mysql
啟動Mysql
# service mysqld start
進入mysql互動模式:
# mysql
> select version(); 顯示mysql版本
> show databases; 顯示所有資料庫
> show [global | session] variables; 顯示伺服器參數變量,有些變量的值可以修改,能夠改變mysql的工作特性。
> show [global | session] status; 顯示伺服器狀态變量,記錄了目前包括過去的時間内mysql的運作統計資料。
> use mysql 設定預設資料庫
> select user,host,password from user; 查詢user表中的,user,host,password字段。
> create user 'wpuser'@'192.168.1.%' identified by 'wppass'; 建立使用者
%:比對任意長度的任意字元;
_ : 比對任意單個字元;
> flush privileges; 寫入記憶體,立即生效。
更新使用者密碼:
set password = password('123456');
授權文法:
> grant all privileges on dbname.tablename to 'username'@'host' [identified by 'password'];
> grant all privileges on wpdb.* to 'wpuser'@'192.168.1.%'; 授權wpdb給wpuser。
修改使用者密碼:
第一種方法:
> set password for 'wpuser'@'192.168.1.%'=password('redhat');
第二種方法:
# mysqladmin -uroot -p password 'mypass'
清除使用者:
> drop user 'wpuser'@'192.168.1.%'
> drop user 'root'@'::1';
> drop user ''@'localhost';
> drop user ''@'localhost.localdomain';
删完了以後再看下:
基本的SQL語句:
建立表:
> create database testdb;
> use testdb;
> create table students (name char(30) not null primary key,id tinyint unsigned ,Age tinyint unsigned,class varchar(20) not null);
> show tables; 顯示表
> desc students; 顯示表結構
> show indexes from students; 檢視鍵
再建立一個表:
> create table tb1 (ID tinyint unsigned not null primary key auto_increment,Name char(30) not null unique key , Age tinyint unsigned, Gender char(1) default 'M' , Course varchar(50) not null);
顯示下結構:
>desc tb1
清除表:
> drop table students;
插入資料:
> insert into tb1(ID,Name,Age,Course) values (1,'Jerry',15,'English');
> insert into tb1 values (2,'Tom',15,'English');
> insert into tb1 (name,Course) values ('tuchao','computer'),('tyz','ssh'); 多行插入
查詢下剛剛插入的資料:
> select * from tb1; 查詢表所有字段資料
> select ID,Name from tb1; 選擇字段顯示
使用查詢别名:
> select ID,Name as 'student_name',Course from tb1;
表達式查詢:
> select ID,Name,Age from tb1 where Age is null;
通配查詢:
> select * from tb1 where name like 't%';
包含查詢:
現在我們要查詢表中Age 19和20的資料。
> select ID,Name,Age,Course from tb1 where Age in (19,20);
RLIKE 正規表達式書寫模式:
查詢tb1表中Name字段,包含了字元u的行。
> select * from tb1 where Name rlike '.*u.*';
查詢tb1表中Name字段,以fzT字元開頭的行。
> select * from tb1 where Name rlike '^[fzT]';
修改資料:
把tb1表中Name字段tuchao的年齡改為20
> update tb1 set Age=20 where Name='tuchao';
清除表中Age為20的使用者。
> delete from tb1 where Age=20;
實驗題:
建立如下結構表:
插入如下資料:
要求:
1、建立此表,插入資料
2、找出性别為女性的所有人;
3、找出年齡大于或等于20的所有人;
4、修改xiaohong的Course為redhat;
5、清除年齡小于20的所有人;
6、授權給testuser使用者對testdb庫所有通路權限;
一、建立此表,插入資料;
> create table uu (ID tinyint unsigned not null primary key auto_increment,Name char(20) not null unique key ,Age tinyint unsigned not null ,Gender char(5) default 'Man',Course varchar(50) not null);
> insert into uu values (1,'tuchao',20,'Man','Computer'),(2,'fangchao',19,'Man','Computer'),(3,'jerry',12,'Man','study'),(4,'Bob',16,'Man','English'),(5,'xiaohong',23,'Woman','C++'),(6,'minli',20,'Woman','CCNP'),(7,'zhangxue',15,'Woman','Java');
二、找出性别為女性的所有人:
> select * from uu where Gender='Woman';
三、找出年齡大于或等于20的所有人:
> select * from uu where Age>=20;
四、 修改xiaohong的Course為redhat
> update uu set Course='C++' where Name='xiaohong'
五、清除年齡小于20的所有人
> delete from uu where Age<20;
六、授權給testuser使用者對testdb庫所有通路權限
> create user 'testuser'@'192.168.1.%' identified by '123456';
> grant all on testdb.* to 'testuser'@'192.168.1.%';
寫了好久好久啊,終于完了。
有問題歡迎與我交流QQ:1183710107
本文轉自qw87112 51CTO部落格,原文連結:http://blog.51cto.com/tchuairen/1422258