MySql- database infrastructure

MySql- database infrastructure

The basic idea of ​​the database

What is the database

The database is used to store data in the warehouse, in essence, is a set of server-side and client-based program CS architecture will eventually disk data stored in the server. All relevant data related to the user, must be the place to find. Before the learned data storage methods are:

1. lists, dictionaries ...... and so on, the data in memory, the disadvantage is the loss of power failure, the advantage of speed

2. The file is stored, such as JSON, TXT ....... etc., advantage can be achieved permanent preservation, the disadvantage is slow.

Why use a database

1. Increase speed

2. to access data on different machines over the network: our future programs may be distributed on different machines, and each machine performance and must have an upper limit, if a machine is not able to meet, we need to use multiple machines to work together mission accomplished.

  • Distributed: each server to provide different services, sometimes a business process may involve multiple servers. So communication cumbersome and disaster recovery clusters is also no good, but the coupling is low, easy to maintain
  • Cluster: All services provided by the server is exactly the same, disaster recovery and strong, easy to spread, pluggable

3. User rights management

4. Multiple clients concurrent access, ensure data security.

Classification database

Relational databases: Can connection among data, relational databases are usually table structure, which means you're in a relational database, the first step is to determine the structure of the table. Database will help us to maintain this relationship. Usually disk as a storage medium. Common relational database are:

  • MySQL: the most popular relational database, free and open source
  • SQLserver: Microsoft's only run in the windows platform
  • oracle: the most powerful relational database, primarily in the cluster and user management is outstanding charges
  • db2: IBM's products, mainly for enterprise users

Non-relational databases: You can not help us to maintain relationships between data, usually memory as the storage medium. Typically k, v keys to store data in the form.

Commonly used non-relational databases have: MongoDB (document database, a very close relationship between the type of non-relational databases), Redis, memcahe

Database Key Concepts

Data (Column): file in a string

Record (Row): a line in the data file is called a section of the record

Table (Table): a file

Library (DataBase): it is a folder

Wherein the header is the first row of the table data, the field has a specific type, the field name + field is a field type, string name memory, digital storage password, birth with deposit date.

DBIMS: database management system (refers to database software)

Database server: computer running DSMS

MySQL uses a prelude

After installing Mysql, there are bin files used to store all executable files, bin file mysqld.exeis the server program, mysql.execlient program. You need to run mysqld.exe. data file is used to place the data stored.

When you run the client, if you simply double-click into the mode for tourists; proper operation is typing commands in a terminal specified user name and parameters, such as: mysql -h 127.0.0.1 -p 3306 -uroot -p

  • -h: Host name of the connection to the server, and if the machine can be ignored
  • -p: Specifies the port, Mysql default port number is 3306, you can not write
  • -u: Specifies the user name
  • -p: Specifies a password (password is not specified generally because the password is displayed in plain text Enter password again directly after the usual -p.)

Run as administrator terminal cmd:

  • mysqld-install # registration system services can be set to start automatically after registration, next time do not open their own server
  • sc delete mysql # remove services
  • net start mysql # Start Service
  • stop service net stop mysql #
  • tasklist | findstr mysqld # view the process
  • taskkill -f / pid 18536 # close the specified process
  • taskkiii / F / PID process ID

mysql 5.6 administrator password settings

You know the password:

1. Log on to perform an update statement to modify mysql

  • Entering mysql-> use mysql-> update user set password = password ( "123") where host = "localhost" and user = "root" -> flush privileges; MySQL or is restarted (password set for you Note 123)

2. Using a small tool to modify mysqladmin

  • mysqladmin -uroot -p123 password 321 (note 123 for the original password, 321 Set your new password)
  • mysqladmin -uroot -p password 123 (no password, the password setting 123)

A warning message will be reported later modified successfullyWarning: Using a password on the command line interface can be insecure.

When the command input errors, you can use \ c cancel the previous command, c mean cancel

When not know the password to modify (crack the code) by skipping authorization:

  • Run as administrator terminal -> net stop mysql (mysql stop service) -> mysqld --skip-grant-tables (skip authorize the opening server) -> Run ordinary Terminal -> mysql -uroot -p directly back car without entering the password -> use mysql (switch to the mysql database) -> update user set password = password ( "123") where host = "localhost" and user = "root"; (modify the administrator password corresponding to the user) -> restart normal server (net start mysql) -> normal login

Simple to use

Data must find a file that is kept up table, the table must exist in the library is a folder

Library Operation

Library similar to folders.

# 切换数据库
use 数据库名称



# 查看所有数据库
show databases;
# 查看单个数据库的详细信息
show create database 数据库名;
show create database db1;  查单个
# 查看当前库
select database();



# 创建数据库
create database 数据库名称;
create database db1;
# 创建数据库时指定字符编码,不能写utf-8
create database 数据库名称 charset utf8;



# 删除数据库
drop database 数据库名称;
drop database db1;  删库



# 修改数据库编码,可以进入到数据库文件修改db.opt。db.opt的第一行是编码,第二行是校对规则

# 修改数据库的字符编码
alter database 数据库名 charset utf-8;
alter database db1 charset='gbk';  修改编码
# 修改数据库名,可以直接修改对应的文件夹名称

Operating table

TABLE equivalent file, a record in the table is equivalent to the line of the content file, except that the table has a record corresponding to the title field of the table referred to.

Remember when we wrote before "employee information table work" it? Stores employee information file like this:

id,name,age,sex,phone,job
1,Tank,83,female,13651054608,IT
2,Nick,26,male,13304320533,Tearcher
3,Sean,25,male,13332353222,IT
4,Jason,40,male,13332353333,IT

If the above file into a table, it should be like the following

id name age sex phone job
1 Tank 83 female 13651054608 IT
2 Nick 26 male 13304320533 Tearcher
3 Sean 25 male 13332353222 IT
4 Jason 40 male 13332353333 IT

id, name, age, sex, phone, job called fields, the rest of the contents of a line is called a record.

When you create a table, you need to develop library. Create a table is to create a particular file.

# 指定库或切换数据库:
use 数据库名称
# 查看当当前所在库:
select databases();



# 创建表
create table 表名称(列名称 列的数据类型,列名称2 列的数据类型...);
create table userinfo(id int,name char);
# 创建表时指定编码方式
create table 表名称(列名称 列的数据类型,列名称2 列的数据类型...)charset gbk;



# 查看当前库下所有表
show tables;
# 查看表结构
desc 表名称;<===>describe 表名称;
desc userinfo;    <==> describe userinfo;
# 查看表的创建语句
show create table 表名称;
show create table userinfo;



# 修改表结构
# 添加字段
alter table 表名称 add 列名称 数据类型
# 删除字段
alter table 表名称 drop 列名称
# 修改数据类型
alter table 表名称 modify 列名称 新的数据类型;
alter table userinfo modify name char(32);
# 修改列名
alter table 表名称 change 旧的列名 新的列名 新的类型;
# 修改编码
alter table 表名称 charset utf8;



# 删除表
drop table 表名称;
drop table userinfo;
# 清空表
truncate table 表名称;

Guess you like

Origin www.cnblogs.com/zuihoudebieli/p/11375218.html