#增加使用者
#格式:grant 權限 on 資料庫.* to 使用者名@登入主機 identified by '密碼'
/*
如,增加一個使用者user1密碼為password1,讓其可以在本機上登入, 并對所有資料庫有查詢、插入、修改、删除的權限。首先用以root使用者連入mysql,然後鍵入以下指令:
grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";
如果希望該使用者能夠在任何機器上登陸mysql,則将localhost改為"%"。
如果你不想user1有密碼,可以再打一個指令将密碼去掉。
grant select,insert,update,delete on mydb.* to user1@localhost identified by "";
*/
grant all privileges on wpj1105.* to sunxiao@localhost identified by '123'; #all privileges 所有權限
show grants for user1@'localhost'; #檢視使用者具有哪些權限。
revoke select on ucyzw.* from user1@'localhost'; #取消使用者授權。
#----------------------------
#-----MySql資料庫操作基礎-----
#顯示資料庫
show databases;
#判斷是否存在資料庫wpj1105,有的話先删除
drop database if exists wpj1105;
#建立資料庫
create database wpj1105;
#删除資料庫
drop database wpj1105;
#使用該資料庫
use wpj1105;
#顯示資料庫中的表
show tables;
#先判斷表是否存在,存在先删除
drop table if exists student;
#建立表
create table student(
id int auto_increment primary key,
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;
檢視表結構:
desc:
#删除表
drop table student;
#檢視表的結構
describe student; #可以簡寫為desc student;
檢視索引:
show indexes from student;
#插入資料
insert into student values(null,'aa','男','1988-10-2','......');
insert into student values(null,'bb','女','1889-03-6','......');
insert into student values(null,'cc','男','1889-08-8','......');
insert into student values(null,'dd','女','1889-12-8','......');
insert into student values(null,'ee','女','1889-09-6','......');
insert into student values(null,'ff','null','1889-09-6','......');
#查詢表中的資料
select * from student;
select id,name from student;
#修改某一條資料
update student set sex='男' where id=4;
#補充update資料庫多表查詢的指派(語句要掌握。有時候線上資料庫要修複):
UPDATE Track,MV
SET Track.is_show=MV.is_show
WHERE Track.trkid=MV.mvid and trkid<6 #多表查詢的時候注意也要找相等關系
#删除資料
delete from student where id=5;
# and 且
select * from student where date>'1988-1-2' and date<'1988-12-1';
# or 或
select * from student where date<'1988-11-2' or date>'1988-12-1';
#between
select * from student where date between '1988-1-2' and '1988-12-1';
#in 查詢制定集合内的資料
select * from student where id in (1,3,5);
#排序 asc 升序 desc 降序
select * from student order by id asc;
#分組查詢 #聚合函數
select max(id),name,sex from student group by sex;
select min(date) from student;
select avg(id) as '求平均' from student;
select count(*) from student; #統計表中總數
select count(sex) from student; #統計表中性别總數 若有一條資料中sex為空的話,就不予以統計~
select sum(id) from student;
#查詢第i條以後到第j條的資料(不包括第i條)
select * from student limit 2,5; #顯示3-5條資料
#鞏固練習
create table xiaoluo
(id int auto_increment primary key,
name varchar(50) not null,
sex varchar(50) not null,
age int unsigned, #年齡不能為負值
sno int unique)default charset=utf8;
drop table xiaoluo;
desc xiaoluo;
insert into xiaoluo (id,name,sex,age,sno) values (null,'濤哥','男',68,1);
insert into xiaoluo(id,name,sex,age,sno) values (null,'aa','男',68,2);
insert into xiaoluo(id,name,sex,age,sno) values (null,'平平','男',35,3);
...
select * from xiaoluo;
#修改資料
update xiaoluo set age=66 where id=2;
update xiaoluo set name='花花',age=21,sex='女' where id=2
delete from xiaoluo where age=21;
#常用查詢語句
select name,age ,id from xiaoluo
select * from xiaoluo where age>40 and age<60; #and
select * from xiaoluo where age<40 or age<60; #or
select * from xiaoluo where age between 40 and 60 #between
select * from xiaoluo where age in (30,48,68,99); #in 查詢指定集合内的資料
select * from xiaoluo order by age desc; #order by (asc升序 des降序)
#分組查詢
select name,max(age) from xiaoluo group by sex; #按性别分組查年齡最大值
#聚合函數
select min(age) from xiaoluo;
select avg(age) as '平均年齡 ' from xiaoluo;
select count(*) from xiaoluo; #統計表中資料總數
select sum(age) from xiaoluo;
#修改表的名字
#格式:alter table tbl_name rename to new_name
alter table xiaoluo rename to a;
#表結構修改
create table test
(
id int not null auto_increment primary key, #設定主鍵
name varchar(20) not null default 'NoName', #設定預設值
department_id int not null,
position_id int not null,
unique (department_id,position_id) #設定唯一值
);
alter table test rename to test_rename;
#向表中增加一個字段(列)
#格式:alter table tablename add columnname type;/alter table tablename add(columnname type);
alter table test add columnname varchar(20);
#修改表中某個字段的名字
alter table tablename change columnname newcolumnname type; #修改一個表的字段名
alter table test change name uname varchar(50);
select * from test;
#表position 增加列test
alter table position add(test char(10));
#表position 修改列test
alter table position modify test char(20) not null;
#表position 修改列test 預設值
alter table position alter test set default 'system';
#表position 去掉test 預設值
alter table position alter test drop default;
#表position 去掉列test
alter table position drop column test;
#表depart_pos 删除主鍵
alter table depart_pos drop primary key;
#表depart_pos 增加主鍵
alter table depart_pos add primary key PK_depart_pos
(department_id,position_id);
#用文本方式将資料裝入資料庫表中(例如D:/mysql.txt)
load data local infile "D:/mysql.txt" into table MYTABLE;
#導入.sql檔案指令(例如D:/mysql.sql)
source d:/mysql.sql; #或者 mysql -uroot daname<mysql.sql
二進制日志的用法:
備份二進制日志可以使用:
mysqlbinlog mysql-bin.xxx > xx.sql(起始點還原)
或者根據時間點:
mysqlbinlog --start-datetime="2014-12-13 17:00:00" --stop-datetime="2014-12-13 19:20:00" /home/mysql/data/mysql-bin.000008 >/home/test/uclog2.sql
當主從同步出現時間偏移報錯可以根據上面步驟,找出出錯點再同步:
MASTER_HOST='192.168.1.8',MASTER_USER='rsync',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000008',MASTER_LOG_POS=272267720(找出錯的時間點cat uclog2.sql)
#
#mysqlbinlog
--start-position 起始位置
--stop-position 結束位置
--start-datetim 起始時間
--stop-datetim 結束時間
mysql被設定無密碼登入(find / -name my.cnf):
第一:配置檔案或者啟動檔案裡面有:--skip-grant-tables 選項。
第二:或者在配置檔案裡面加入了密碼:
[client]
root=xiaoluo
password=xsdasy892
mysql用戶端程式使用:
mysqladmin -uroot -p -h192.168.10.1 ping #測試對方是否線上
mysqladmin processlist #檢視使用者并發線程
mysqladmin status #檢視資料庫狀态,包括滿查詢
#shell 允許mysql語句的時候直接 mysql -e "xxx(mysql語句)" 就可以編寫腳本。
事物:mysql有事務隔離級别,根據資料的一緻性要求。可以對隔離級别優化,事物預設送出:
START TRANSACTION; #啟動事物;
ROLLBACK ;#出錯的時候事物復原:已經送出了的事物是不能復原的
COMMIT ; #事務送出
變量檢視事務是否預設送出,1是預設送出,為了減少iO性能的消耗,建議關閉自動送出。
set autocomit=0
mysql> select @@autocommit;
+--------------+
| @@autocommit |
| 1 |
1 row in set (0.00 sec)
鎖機制和檢視:
mysql> show status like 'Table%'
+----------------------------+-------+
| Variable_name | Value |
| Table_locks_immediate | 1147 |
| Table_locks_waited | 0 |
| Table_open_cache_hits | 0 |
| Table_open_cache_misses | 0 |
| Table_open_cache_overflows | 0 |
5 rows in set (0.00 sec)
如果Table_locks_waited值比較高,說明是已經有鎖表。
二、通過show processlist檢視,要是存在比較多的鎖,臨時的解決方案是直接把idkill掉。但是不是最終辦法,要查找到是什麼原因引起的鎖。是否是硬體問題。或者是程式本身問題,還有就是那條語句滿查詢引起的鎖。然後慢慢優化。可以加緩存。
本文轉自 小羅ge11 51CTO部落格,原文連結:http://blog.51cto.com/xiaoluoge/1589793,如需轉載請自行聯系原作者