1. Database related concepts
1. Two hardware expansion methods
a . Vertical expansion: for one computer
b . Horizontal expansion: multiple ordinary computers
2. Database related concepts
Database server (essentially a computer on which a server of database management software is installed) -- referred to as database (operation and maintenance perspective)
Database management system RDBMS (essentially a socket software with a C/S architecture) -- referred to as database (operation and maintenance perspective)
Note: The server side of the general database management system is installed on the linux operating system
mysqld—— server; mysql—— client ( python can't use the server that comes with mysql , you need to use pymysql )
a , support concurrency
b , lock problem
c . Authenticate client requests
d . Access efficiency (reduce the number of IOs )
Libraries (folders) -- simply database (programmer's perspective)
Table (file): fields to be set (header)
Record: extract all typical features / data of a thing (a collection of a bunch of data)
egon, 18,180, male, True
data:
name='egon' age=18 height=180 sex="male" beutiful=True
SQL statement: The SQL statements used / defined by different database software are not exactly the same, but the difference is not big
3. Database management system / software classification DBMS :
Relational RDBMS :
There is a table structure, the table structure must be defined before accessing data, and the data must be stored according to the type or constraint of the field
Typical representatives: MySQL , Oracle , DB2 , SQL server (commonly used in the banking industry)
Non-relational (commonly used by reptiles) :
Accessing data is in the form of key:value
Non-relational: Mongodb , redis , memcache (less commonly used)
2. Basic management ( windows system)
1. Installation ( installation is completed after the windows system decompresses the file)
2. Add environment variables
3. Make mysqld software a system service
Note: Turn off mysqld before making
C:\Windows\system32>tasklist |findstr mysqld
mysqld.exe 8372 Console 2 454,916 K
C:\Windows\system32>taskkill /F /PID 8372
Success : Process with PID 8372 terminated .
Make:
mysqld --install
Check:
windows + r
Enter services.msc
4. Start:
Unmade system service startup method:
1. Start the server
mysqld
2. Start the client
mysql -uroot -p -h127.0.0.1 -P 3306 #3306 is the default port for mysql
如果是在本机登录mysqld服务端可以简写:mysql -uroot -p #本机IP已知,端口默认
制作完系统服务后就可以鼠标点击启动或关闭mysql:
windows+r
输入services.msc
找到mysql,启动或关闭
5、破解管理员密码
1、先关闭mysqld服务端
2、以跳过授权表的方式在命令行中启动mysqld服务端
mysqld --skip-grant-tables
3、客户端直接以无密码的方式登录root用户,修改密码
mysql -uroot -p
mysql> update mysql.user set password=password("123") where user="root" and host="localhost";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges; #每次改完后强制刷新下
Query OK, 0 rows affected (0.00 sec)
4、在命令行中用taskkill杀死mysqld服务,然后正常启动mysqld
taskkill /F /PID 131312
def password(mingwen): #password模块可帮忙加密明文,直接调取即可
import hashlib
m=hashlib.md5()
m.update(mimgwen)
return m.hexdigest()
6、统一字符编码
1、在mysql安装目录下新建my.ini文件
2、修改my.ini
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
3、重启mysqld
4、客户端登录,输入\s查看结果
三、基本管理(mac系统)
1、安装 (mysql-5.6.35-macos10.12-x86_64.dmg)
2、加环境变量
https://blog.csdn.net/con_heart/article/details/53766931
其实和windows一样 在高级属性里加入环境变量是为了在任何一个路径下输入命令的时候都能找到这个命令,就不用每次要输入某个命令(如登录mysql)都要先进入命令所在的文件夹(一般挺长的)。下面就是把mysql的命令的路径加入环境变量:
1.打开终端,输入: cd ~
会进入~文件夹
2.然后输入:touch .bash_profile
回车执行后,
3.再输入:open -e .bash_profile
会在TextEdit中打开这个文件(如果以前没有配置过环境变量,那么这应该是一个空白文档)。如果有内容,请在结束符前输入,如果没有内容,请直接输入如下语句:export PATH=${PATH}:/usr/local/mysql/bin
然后,保存,退出TextEdit(一定是退出),关闭终端并退出。
4.再次打开终端,直接输入 【 mysql -u "你的用户名" -p 】 回车 再输入密码 这时候就能登入mysql了
3、查看mysql文件夹
前往文件夹 -》 /usr/local/mysql
4、启动:
访达-》系统偏好设置
PATH="$PATH":/usr/local/mysql/bin
mysql -u root -p #-p后面不加空格,直接接密码
5、破解管理员密码
https://blog.csdn.net/lishaojun0115/article/details/52805563
step1:苹果->系统偏好设置->最下边点MySQL 在弹出页面中 关闭mysql服务(点击stop mysql server)
step2:进入终端输入:cd /usr/local/mysql/bin/
回车后 登录管理员权限 sudo su
回车后输入以下命令来禁止mysql验证功能 ./mysqld_safe --skip-grant-tables &
回车后mysql会自动重启(偏好设置中mysql的状态会变成running)
step3:
输入命令 ./mysql
回车后,输入命令 FLUSH PRIVILEGES;
回车后,输入命令 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('你的新密码');
至此,密码修改完成,可以成功登陆。
6、统一字符编码(防止乱码,统一成utf-8)
1、复制my.cnf文件,并在其中添加以下内容
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
#basedir=/usr/local/mysql/
#datadir=/usr/local/mysql/data/
[client]
default-character-set=utf8
user=root
password=******
[mysql]
default-character-set=utf8
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
2、关闭mysql服务端
3、在\etc下添加my.cnf
4、重启mysql服务端,在客户端中登陆并查看(\s)
7、其他:
MAC电脑安装Mysql服务器和Navicat for mysql客户端
https://blog.csdn.net/kuangdacaikuang/article/details/76515985
四、基本的SQL语句
#SQL语句的主要操作对象为库,表,记录;其中对记录的操作最为常见
1、文件夹(库)#库名可按照python的命名规则来
增
create database db1 charset utf8;
改
alter database db1 charset gbk; #库名修改不了
查
查看所有库的库名
show databases; #其中information_scheme是内存中的表
单独查看某一个库的信息
show create database db1;
删
drop database db1;
2、文件(表)#表名可按照python的命名规则来
首先切换文件夹:
use db1; #也可后续db1.t1调用
select database(); #查看当前所在的文件夹
增
create table t1(id int,name char); #char即sql中的str
改
alter table t1 modify name char(16); #16为存取限制,最多存取16个字符
查
查看当前库下所有的表名
show tables;
查看t1表的详细信息
show create table t1;
查看表结构
desc t1;
删
drop table t1;
3、文件的一行内容(记录)
增
insert into db1.t1 values
(1,'egon'), #增加前最好检查下之前表的charset
(2,'alex'),
(3,'lxx');
改
update db1.t1 set name='sb' where id > 1;
查
select id,name from db1.t1;
select * from db1.t1 where age >20;
删
delete from db1.t1 where name = "SB" ;
4、;为SQL语句的语句结束提示符
\c 若语句错误,想退出每次输入并不影响结果时使用
五、存储引擎
#Mysql使用的线程池
create table t1(id int)engine=innodb;
create table t2(id int)engine=myisam;
create table t3(id int)engine=blackhole;
create table t4(id int)engine=memory;
常见innodb,myisam,memory,blackhole几种存储引擎,最常用的为innodb innodb:亦为默认存储引擎,支持事务,行锁设计,外键
t1.frm (frame) t1.ibd (innodb data); 存入硬盘
myisam:t2.MYD (myisam data) t2.MYI (myisam index) t2.frm (frame); 存入硬盘
blackhole: t3.frm
memory: t4.frm; 存入内存,关闭服务端后,数据清空