MySQL第一天
DATABASE day1:
一、搭建數(shù)據(jù)庫服務(wù)器
二、數(shù)據(jù)庫服務(wù)的基本使用
三、MySQL數(shù)據(jù)類型
四、管理記錄
五、用戶授權(quán)與權(quán)限撤銷
六、數(shù)據(jù)備份與恢復(fù)
七、MySQL主從同步
八、數(shù)據(jù)讀寫分離
九、MySQL優(yōu)化
十、MySQL 集群
#######################
一、搭建數(shù)據(jù)庫服務(wù)器
數(shù)據(jù)庫服務(wù)器是用來存儲數(shù)據(jù)
1、購買服務(wù)器的硬件配置:存儲 CPU 內(nèi)存
2、安裝操作系統(tǒng):Linux UNIX Windows
3、安裝提供數(shù)據(jù)庫服務(wù)的軟件包:
3.1有哪些類型的軟件:mysql mariadb sql-server db2
3.2來源:官網(wǎng)下載 系統(tǒng)安裝光盤自帶的
3.3類型:RPM(不可修改) 源碼包(可自定義)
3.4軟件包的開源是否跨平臺
安裝前準(zhǔn)備
基本需要
-1.采用RHEL7.2系統(tǒng)搭建MySQL服務(wù)器
-2.關(guān)閉防火墻 systemctl stop firewalld
-3.關(guān)閉selinux sed 's/=enforcing/=permissive/g' /etc/selinux/config
-4.軟件mysql-5.7.17-1
準(zhǔn)備工作
停止mariadb服務(wù)
刪除文件
1.安裝軟件包
[root@db1 09.mysql]# rm -rf mysql-community-server-minimal-5.7.17-1.el7.x86_64.rpm
[root@db1 09.mysql]# ls mysql-*.rpm
[root@db1 09.mysql]# rpm -Uvh mysql-community-*.rpm 安裝,如果存在舊版本 就升級 ,也可以查看需要哪些依賴包
警告:mysql-community-client-5.7.17-1.el7.x86_64.rpm: 頭V3 DSA/SHA1 Signature, 密鑰 ID 5072e1f5: NOKEY
錯誤:依賴檢測失敗:
perl(Data::Dumper) 被 mysql-community-test-5.7.17-1.el7.x86_64 需要
perl(JSON) 被 mysql-community-test-5.7.17-1.el7.x86_64 需要
[root@db1 09.mysql]# yum list |grep "perl-Data-Dumper"
perl-Data-Dumper.x86_64 2.145-3.el7 192.168.4.254_rhel7
[root@db1 09.mysql]# yum list |grep "perl-JSON"
perl-JSON.noarch 2.59-2.el7 192.168.4.254_rhel7
perl-JSON-PP.noarch 2.27202-2.el7 192.168.4.254_rhel7
[root@db1 09.mysql]# yum -y install perl-Data-Dumper.x86_64 perl-JSON.noarch perl-JSON-PP.noarch
[root@db2 09.mysql]# rpm -Uvh mysql-community-*.rpm
2.啟動服務(wù)
systemctl start mysqld
systemctl enable mysqld
3.與MySQL數(shù)據(jù)庫服務(wù)相關(guān)信息
進(jìn)程名 mysqld
進(jìn)程所有者和所屬組 mysql mysql
默認(rèn)端口號 3306
傳輸協(xié)議 TCP
配置文件 /etc/my.cnf
數(shù)據(jù)庫目錄 /var/lib/mysql
日志文件 /var/log/mysqld.log (存放初始密碼)
使用數(shù)據(jù)庫管理員root用戶初始密碼連接數(shù)據(jù)庫服務(wù)
[root@db1 09.mysql]# grep -i password /var/log/mysqld.log
2018-02-22T03:21:32.096060Z 1 [Note] A temporary password is generated for root@localhost: ;Go-_qk(2Jon
[root@db1 09.mysql]# which mysql
[root@db1 09.mysql]# mysql -uroot -p";Go-_qk(2Jon"
mysql>
mysql> quit //斷開連接
設(shè)置數(shù)據(jù)庫管理員root用戶本機(jī)登錄密碼
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@localhost identified by "123456";
Query OK, 0 rows affected (0.00 sec)
mysql> quit
vim /etc/my.cnf #永久生效密碼
[mysqld]
validate_password_policy=0
validate_password_length=6
[root@db1 09.mysql]# systemctl stop mysqld.service
[root@db1 09.mysql]# systemctl start mysqld.service
[root@db1 09.mysql]# mysql -uroot -p123456
mysql> show databases;
二、數(shù)據(jù)庫服務(wù)的基本使用
把數(shù)據(jù)存儲到數(shù)據(jù)庫服務(wù)器上過程
1.連接數(shù)據(jù)庫服務(wù)器
不作授權(quán)的時候只允許數(shù)據(jù)庫管理員root用戶在數(shù)據(jù)庫本機(jī)連接數(shù)據(jù)庫服務(wù)
mysql -hlocalhost -uroot -p123456
2.創(chuàng)建庫(文件夾)
mysql> create database 庫名; ##等于在/var/lib/mysql創(chuàng)建了一個目錄
mysql> system ls /var/lib/mysql ##可以用linux命令,不用分號
mysql> select database(); ##類似linux中的pwd
mysql> use 庫名; ##進(jìn)入庫
mysql> drop database 庫名; ##刪除庫
mysql> drop database 庫名\c ##終止繼續(xù)打命令
3.建表(系統(tǒng)文件)
表必須保存在庫里。
mysql> use mysql; ##進(jìn)入庫
mysql> show tables; ##查看庫里面所有表
mysql> select * from user; ##查看表中所有列的內(nèi)容
mysql> select user,host from user; 查看表中user,host列內(nèi)容
mysql> desc user; ##查看表中有什么列,什么類型
mysql> select user,host,select_priv from user; 查看表中select_priv,user,host列內(nèi)容
###############################################################3
建表命令
行(記錄)
列(字段名)
把學(xué)生信息保存到gamedb庫里
name age
tom 19
jim 21
mysql> create table gamedb.stu( name char(10),age int(2)); ##創(chuàng)建表中的列、類型,列為name,age,類型為char,int。
Query OK, 0 rows affected (0.19 sec)
mysql> use gamedb; ##進(jìn)入庫
mysql> show tables; ##查看表庫中所有表
+------------------+
| Tables_in_gamedb |
+------------------+
| stu |
+------------------+
1 row in set (0.00 sec)
mysql> desc stu;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
mysql> select * from stu;
Empty set (0.00 sec)
mysql> insert into stu values("jim",21); ##插入表中列內(nèi)的內(nèi)容,name為jim,age為21
mysql> insert into stu values("tom",19);
mysql> select * from stu; ##查看stu表中所有列的內(nèi)容(值)
+------+------+
| name | age |
+------+------+
| jim | 21 |
| tom | 19 |
+------+------+
2 rows in set (0.00 sec)
mysql> select name from stu; ##查看stu表中name列的內(nèi)容(值)
+------+
| name |
+------+
| jim |
| tom |
+------+
2 rows in set (0.00 sec)
mysql> delete from stu; ##刪除表中所有列內(nèi)的內(nèi)容(值)
Query OK, 2 rows affected (0.06 sec)
mysql> select * from stu; ##查看表在所有列中的內(nèi)容(值)
Empty set (0.00 sec)
mysql> drop table stu; ##刪除表
Query OK, 0 rows affected (0.12 sec)
mysql> drop database gamedb; ##刪除庫
Query OK, 0 rows affected (0.00 sec)
管理數(shù)據(jù)庫服務(wù)使用的時sql命令;
sql命令使用規(guī)則?
sql命令類型
三、MySQL數(shù)據(jù)類型
1.整數(shù)型 (數(shù)值類型的寬度只是顯示幾位數(shù),不能限制數(shù)值的大小,如果不指定寬度默認(rèn)位11位,所以盡量指明寬度(節(jié)省空間),不然會空格補(bǔ)位)
數(shù)值不夠指定寬度時,左邊空格補(bǔ)位
寬度僅顯示寬度,存數(shù)值的大小由類型決定
使用關(guān)鍵字ZEROFILL時,填0代替空格補(bǔ)位
類型 大小 范圍(有符合) 范圍(無符合) 用途
tinyint 1字節(jié) -128~127 255 微小整數(shù)
smallint 2字節(jié) -32768~32767 0~65535 小整數(shù)
mediumint 3字節(jié) -2^23~2^23-1 0~2^24-1 中整數(shù)
int 4字節(jié) -2^31~2^31-1 0~2^32-1 大整數(shù)
bigint 8字節(jié) -2^63~2^63-1 0~2^64-1 極大整數(shù)
float 4字節(jié) 單精度浮點數(shù)
double 8字節(jié) 雙精度浮點數(shù)
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> use db1;
Database changed
mysql> create table db1.t2(name char(4) ,age tinyint); ##無符號表示 age tinyint unsigned
Query OK, 0 rows affected (0.41 sec)
mysql> desc t2;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | char(4) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into t2 values("tom",127);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t2;
+------+------+
| name | age |
+------+------+
| tom | 127 |
+------+------+
1 row in set (0.00 sec)
輸入表的values不夠指定的寬度時左邊用0填充
mysql> create table db.t2(id int(3),level int(5) zerofill);
Query OK, 0 rows affected (0.43 sec)
2.浮點數(shù)
float(n,m) n總寬度,m小數(shù)位數(shù)
mysql> create table db1.t3( age tinyint unsigned,rust float(5,2),pay float(7,2));
Query OK, 0 rows affected (0.40 sec)
mysql> desc t3;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| age | tinyint(3) unsigned | YES | | NULL | |
| rust | float(5,2) | YES | | NULL | |
| pay | float(7,2) | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t3 values(21,59.9,18000.23);
Query OK, 1 row affected (0.08 sec)
mysql> select * from t3;
+------+-------+----------+
| age| rust| pay |
+------+-------+----------+
| 21 | 59.90 | 18000.23 |
+------+-------+----------+
1 row in set (0.00 sec)
3.字符類型 1個字節(jié)一個字符
定長:char(字符數(shù)) 最大長度255字符 ##不夠指定字符長度在右邊用空格補(bǔ)齊(空間浪費,常用)
變長:varchar(字符數(shù)) 最大65532個字符 ##按實際大小分配空間(系統(tǒng)幫忙計算空間,但影響處理速度)
大文本類型:text/blob 字符數(shù)大于65535存儲時使用
mysql> create table db1.t8(name char(5),age tinyint,rust float(5,2),pay float(7,2));
Query OK, 0 rows affected (0.39 sec)
mysql> desc t8;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | char(5) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| rust | float(5,2) | YES | | NULL | |
| pay | float(7,2) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into t8 values("tom",21,159.92,18000.23);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t8;
+------+------+--------+----------+
| name | age | rust | pay |
+------+------+--------+----------+
| tom | 21 | 159.92 | 18000.23 |
+------+------+--------+----------+
1 row in set (0.00 sec)
4.日期時間類型
類型 占位(位數(shù)) 空間
year YYYY 1字節(jié)
date YYYYMMDD 4字節(jié)
time HHMMSS 3字節(jié)
datetime YYYYMMDDHHMMSS 8字節(jié)
timestamp YYYYMMDDHHMMSS 4字節(jié)
year() 獲取指定時間中的年
date() 獲取指定時間中的年月日
month() 獲取指定時間中的月
day() 獲取指定時間中的日期
time() 獲取指定時間中的時間(小時分鐘秒)
now() 獲取當(dāng)前時間(年月日小時分鐘秒)
mysql> create table t3(name char(8),up_class year,birthdate date,meetting time);
Query OK, 0 rows affected (0.49 sec)
mysql> insert into t3 values("tom",2018,19910709,083000);
Query OK, 1 row affected (0.05 sec)
mysql> select * from t3;
+------+----------+------------+----------+
| name | up_class | birthdate | meetting |
+------+----------+------------+----------+
| tom | 2018 | 1991-07-09 | 08:30:00 |
+------+----------+------------+----------+
1 row in set (0.00 sec)
使用兩位數(shù)給year字段賦值
01-69 20XX
70-99 19XX
00 0000
使用時間函數(shù)獲取時間給字段賦值
year() month() date() day() time() ##都需要在括號里填寫信息,可以填函數(shù),也可以填數(shù)字(自己指定)
now() ##系統(tǒng)當(dāng)前時間
datetime 與 timestamp 區(qū)別?(不給datetime和timestamp賦值時,datetime值為空,timestamp為系統(tǒng)當(dāng)前時間)
mysql> create table t6(party datetime,meetting timestamp);
Query OK, 0 rows affected (0.27 sec)
mysql> insert into t6 values(now(),now());
Query OK, 1 row affected (0.03 sec)
mysql> insert into t6 values(19910709095928,19910709095928);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t6(party) values(19910709095928);
Query OK, 1 row affected (0.05 sec)
mysql> insert into t6(meetting) values(19910709095928);
Query OK, 1 row affected (0.03 sec)
mysql> select * from t6;
+---------------------+---------------------+
| party | meetting |
+---------------------+---------------------+
| 2018-02-22 20:58:29 | 2018-02-22 20:58:29 |
| 1991-07-09 09:59:28 | 1991-07-09 09:59:28 |
| 1991-07-09 09:59:28 | 2018-02-22 21:01:45 |
| NULL | 1991-07-09 09:59:28 |
+---------------------+---------------------+
4 rows in set (0.00 sec)
5.枚舉 (字段值在規(guī)定的范圍內(nèi)選擇)
單選 enum(值列表)
多選 set(值列表)
mysql> create table t7(name char(10),sex enum("boy","girl","secret"),love set("IT","film","game","music"));
Query OK, 0 rows affected (0.17 sec)
mysql> insert into t7 values("bob","boy","IT,game");
Query OK, 1 row affected (0.02 sec)
ysql> insert into t7 values("lucy","girl","IT,film,music");
Query OK, 1 row affected (0.07 sec)
mysql> select * from t7;
+------+------+---------------+
| name | sex | love |
+------+------+---------------+
| bob | boy | IT,game |
| lucy | girl | IT,film,music |
+------+------+---------------+
2 rows in set (0.00 sec)
6.約束條件
null 允許位空,默認(rèn)
not null 不允許位空
key 索引類型
default 設(shè)置默認(rèn)值,默認(rèn)位null
mysql> create table t9(
-> name char(10) not null default "",
-> age tinyint(2) unsigned not null default 19,
-> sex enum("boy","girl","no") not null default "no",
-> likes set("it","film","game","music") not null default "film,game");
Query OK, 0 rows affected (0.15 sec)
mysql> describe t9;
+-------+---------------------------------+------+-----+-----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------------+------+-----+-----------+-------+
| name | char(10) | NO | | | |
| age | tinyint(2) unsigned | NO | | 19 | |
| sex | enum('boy','girl','no') | NO | | no | |
| likes | set('it','film','game','music') | NO | | film,game | |
+-------+---------------------------------+------+-----+-----------+-------+
4 rows in set (0.01 sec)
7.修改表結(jié)構(gòu)
alter table 庫.表 執(zhí)行動作;
添加新字段add (after,first)
原來樣子:
mysql> select * from t7;
+------+------+---------------+
| name | sex | love |
+------+------+---------------+
| bob | boy | IT,game |
| lucy | girl | IT,film,music |
+------+------+---------------+
2 rows in set (0.00 sec)
添加最后:
mysql> alter table t7 add email varchar(30) not null default "stu@tedu.cn";
Query OK, 0 rows affected (0.81 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加到sex列之后:
mysql> alter table t7 add age tinyint(2) unsigned default 21 after sex;
Query OK, 0 rows affected (0.97 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加到首:
mysql> alter table t7 add stu_id char(9) first;
Query OK, 0 rows affected (0.92 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看結(jié)構(gòu):
mysql> desc t7;
+--------+---------------------------------+------+-----+-------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------------------+------+-----+-------------+-------+
| stu_id | char(9) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| love | set('IT','film','game','music') | YES | | NULL | |
| email | varchar(30) | NO | | stu@tedu.cn | |
+--------+---------------------------------+------+-----+-------------+-------+
6 rows in set (0.00 sec)
mysql> select * from t7;
+--------+------+------+------+---------------+-------------+
| stu_id | name | sex | age | love | email |
+--------+------+------+------+---------------+-------------+
| NULL | bob | boy | 21 | IT,game | stu@tedu.cn |
| NULL | lucy | girl | 21 | IT,film,music | stu@tedu.cn |
+--------+------+------+------+---------------+-------------+
2 rows in set (0.00 sec)
修改字段類型modify
mysql> alter table t7 modify email char(50) not null default "yaya@163.com";
Query OK, 2 rows affected (0.42 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc t7;
+--------+---------------------------------+------+-----+--------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------------------+------+-----+--------------+-------+
| stu_id | char(9) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| love | set('IT','film','game','music') | YES | | NULL | |
| email | char(50) | NO | | yaya@163.com | |
+--------+---------------------------------+------+-----+--------------+-------+
修改字段位置modify
mysql> alter table t7 modify age tinyint(2) unsigned default 21 after sex;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t7;
+--------+---------------------------------+------+-----+--------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------------------+------+-----+--------------+-------+
| stu_id | char(9) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| love | set('IT','film','game','music') | YES | | NULL | |
| email | char(50) | NO | | yaya@163.com | |
+--------+---------------------------------+------+-----+--------------+-------+
6 rows in set (0.00 sec)
刪除字段drop
mysql> alter table t7 drop love,drop stu_id;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from t7;
+------+------+------+-------------+
| name | sex | age | email |
+------+------+------+-------------+
| bob | boy | 21 | stu@tedu.cn |
| lucy | girl | 21 | stu@tedu.cn |
+------+------+------+-------------+
2 rows in set (0.00 sec)
修改字段名change
mysql> alter table t7 change email mail char(30); ##類型也可以改變,也可以改變
Query OK, 2 rows affected (0.53 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t7;
+------+------+------+-------------+
| name | sex | age | mail |
+------+------+------+-------------+
| bob | boy | 21 | stu@tedu.cn |
| lucy | girl | 21 | stu@tedu.cn |
+------+------+------+-------------+
2 rows in set (0.00 sec)
mysql> desc t7;
+-------+-----------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| mail | char(30) | YES | | NULL | |
+-------+-----------------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
修改表名
mysql> alter table t7 rename studentinfo;
Query OK, 0 rows affected (0.07 sec)
mysql> desc studentinfo;
+-------+-----------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| mail | char(30) | YES | | NULL | |
+-------+-----------------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> show tables;
+--------------+
| Tables_in_db |
+--------------+
| studentinfo |
| t1 |
| t2 |
| t3 |
| t5 |
| t9 |
+--------------+
6 rows in set (0.00 sec)
轉(zhuǎn)載于:https://blog.51cto.com/13587169/2072216
總結(jié)
- 上一篇: 《Scikit-Learn与Tensor
- 下一篇: mysql 分库分表架构与方案