目錄
1.數據庫的操作
1.1鏈接數據庫
mysql -uroot -p
1.2退出數據庫
exit/quit/ctrl+d
1.3查看所有數據庫
show databases;
1.4顯示數據庫版本
select version();
1.5顯示數據庫時間
select now();
1.6創建數據庫
create database python05 charset=utf8;
1.7查看創建數據庫的語句
show create database python05;
1.8刪除數據庫
drop database python04;
1.9查看當前使用的數據庫
select database();
1.10使用數據庫
use python05;
2.數據表的操作
2.1顯示當前數據庫的表
show tables;
2.2創建表
create table xxxxx(id int,name varchar(30));
create table yyyyy(
id int primary key not null auto_increment,
name varchar(30)
);
2.3查看錶結構
desc xxxxx;
示例:
create table students(
id int unsigned not null primary key auto_increment,
name varchar(30),
age tinyint unsigned,
high decimal(5,2),
gender enum("男","女","中性","保密") default "保密",
cls_id int unsigned
);
插入數據
insert into students values(0,"老王",18,188.88,"男",0);
查詢數據
select * from students;
2.4數據類型
-
數值類型(常用)
類型 | 字節大小 | 有符號範圍(signed) | 無符號範圍(unsigned) |
---|---|---|---|
TINYINT | 1 | -128~127 | 0~255 |
SMALLINT | 2 | -32768~32767 | 0~65535 |
MEDIUMINT | 3 | -8388608~8388607 | 0~16777215 |
INT/INTEGER | 4 | -2147483648~2147483647 | 0~42294967295 |
BIGINT | 8 | -9223372036854775808~<br />9223372036854775807 | 0~18446744073709551615 |
-
字符串
類型 | 字節大小 | 示例 |
---|---|---|
CHAR | 0~255 | 類型char(3)輸入‘ab’,實際存儲爲'ab ',輸入‘abcd’,實際存儲爲'abc' |
VARCHAR | 0~255 | 類型char(3)輸入‘ab’,實際存儲爲'ab',輸入‘abcd’,實際存儲爲'abc' |
TEXT | 0~65535 | 大文本(一般大於4000字採用該類型) |
-
日期時間類型
類型 | 字節大小 | 示例 |
---|---|---|
DATE | 4 | '2020-01-01' |
TIME | 3 | '12:29:59' |
DATETIME | 8 | '2020-01-01 12:29:59' |
YEAR | 1 | '2017' |
TIMESTAMP | 4 | '1970-01-01 00:00:01' UTC~'2038-01-01 00:00:01' UTC |
2.5修改表-添加字段
alter table students add birthday datetime;
2.6修改表-修改字段(不重命名字段)
alter table students modify birthday date;
2.7修改表-修改字段(重命名字段)
alter table students change birthday birth date default "2000-01-01";
2.8修改表-刪除字段
alter table students drop birth;
3.數據的增刪改查
3.1增加記錄
insert into students values(default,"小李飛刀",25,178.88,"女",0);
insert into students values(default,"貂蟬",28,178.88,"女",0),(default,"老李",26,178.88,3,1);
注:枚舉中的下標從1開始
insert into students(name,age) values("小喬",29);
3.2修改記錄
update students set age=22 where id=3;
3.3查找記錄
select * from students where name='小李飛刀';
select id as 編號,name as 姓名 from students where name='小李飛刀';
3.4邏輯刪除
alter table students add is_delete bit default 0;
update students set is_delete=1 where id=3;
3.5刪除記錄
delete from students where id=3;
4.數據表查詢
4.1一般查詢
select * from students where name ="小王";
select * from students where name like "%王";
select * from students where name like "__";
select name as 姓名 from students where name like "__";
select id as 編號,name as 姓名 from students where name like "__";
4.2正則表達式
select * from students where name rlike "^小.*刀$";
4.3範圍查詢
select * from students where age in(19,20,38);
select * from students where age not in (19,20,38);
select * from students where age between 18 and 38;
select * from students where age not between 18 and 38;
select * from students where not age between 18 and 38;
4.4判斷是否爲空
select * from students where name is null;
select * from students where name is not null;
4.5排序
select * from students order by high;
select * from students order by high desc;
select * from students order by high desc,age desc;
4.6聚合函數
select count(*) from students;
select sum(age) from students;
select max(age) from students;
select min(age) from students;
select avg(age) from students;
select round(sum(age)/avg(age),2) from students;
4.7分組
select sex,count(*) from students group by sex;
select sex,sum(age) as sumage from students group by sex having sumage>50;
select sex,group_concat(name,"_",age) from students group by sex;
4.8分頁
--select * from students limit start,count;
select * from students limit 2;
select * from students limit 2,2;
select * from students limit 4,2;
4.9內聯查詢
select a.*,b.name as classname from students as a inner join class as b on a.cls_id=b.id;
4.10左鏈接
select a.*,b.name as classname from students as a left join class as b on a.cls_id=b.id;
4.11右鏈接
select a.*,b.name as classname from students as a right join class as b on a.cls_id=b.id;
4.12自查詢
select * from shengshi as a inner join shengshi as b on a.pro_id=b.id;
4.13子查詢
select * from shengshi where pid=(select * from shengshi where proname='浙江省');
4.14設置外鍵
select * from students;
select * from class;
alter table students add foreign key (cls_id) references class(id);
5.mysql的高級用法
5.1視圖的創建
--create view 視圖名 as select 語句;
create view v_student_name as select id as 編號,name as 姓名 from students;
5.2事務
start transaction;
update checking set balance=balance-200.00 where customer_id =10233276;
update saving set balance=balance+200 where customer_id =10233276;
commit;
begin;也可以開啓事務 rollback;撤銷所有的修改
5.3索引
create index 索引名 on 表(索引字段(長度));
create index title_index on test_index(title(10));
5.4賬戶管理
創建賬戶並授權
grant select,update,insert,delete on jingdong.* to 'admin'@'%' identified by 'admin';
查看當前賬戶權限
show grants;
查看admin賬戶權限
show grants for 'admin'@'%';
修改密碼
update user set password = password('123456') where user='admin';
刷新賬戶權限
FLUSH PRIVILEGES;
5.5遠程連接數據庫
mysql -uadmin -p -h 39.100.54.235
5.6備份數據庫
mysqldump -uroot -p jingdong >jd.sql
5.7還原數據庫
先創建新的數據庫,在導入數據
mysql -uroot -p jingdong2<jd.sql
6建立主從服務器
6.1.備份主服務器上的所有數據庫
mysqldump -uroot -p --all-databases --lock-all-tables >all_db.sql
6.2.在從服務器上還原所有數據庫
mysql -uroot -p <all_db.sql
6.3.配置主服務器
#主庫配置
server_id=187
log_bin = mysql-bin
binlog_format = Row
show variables like 'log_bin';--value爲on纔是開啓日誌
show master status;
6.4.配置從服務器ubuntu路徑:
sudo gedit ./etc/mysql/mysql.conf.d/mysqld.cnf
#從庫配置
server_id=128
6.5.從服務器去連接讀取主服務器數據,需要建立賬戶‘%’
grant all privileges on *.* to 'admin'@'%' identified by 'admin' with grant option;
flush privileges;
change master to master_host='39.100.54.235',master_user='admin',master_password='admin',master_log_file='mysql-bin.000002',master_log_pos=120;
6.6.開啓同步
start slave;
--stop slave;
6.7.查看同步狀態
show slave status \G;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 39.100.54.235
Master_User: admin
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 120
Relay_Log_File: MasService-relay-bin.000002
Relay_Log_Pos: 317
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...................................................
注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,否則都是錯誤的狀態(如:其中一個NO均屬錯誤)。
6.8.常見問題
--show slave status中Slave_IO_State: Waiting to reconnect after a failed registration on master
--在master上執行
grant replication slave on *.* to 'user'@'%' identified by 'password';
FLUSH PRIVILEGES;