MySQL database installation and use

Source: Internet
Author: User
Tags local time


Demand:

1: Install mysql 5.6 Database

2 : Build studb Library, and build stuinfo student table, required to build field stu_id  name  age  sex   Class  dizhi

3: Add a new field name ID in the table to the primary key index, and let the ID value automatically grow, modify Dizhi to home

4: Create a new Stuchengji table, set the foreign Key association when the table is built stuinfo

5: Insert data for Stuinfo table

6: Import file passwd content to new table passwd, export stuinfo table data to / Bak

Implementation steps:

1: Install the mysql program, start the service, set the boot to boot, backup the main configuration file

[Email protected] ~]# RPM-IVH mysql-server-5.6.15-1.el6.x86_64.rpm \

>mysql-client-5.6.15-1.el6.x86_64.rpm

>mysql-devel-5.6.15-1.el6.x86_64.rpm

[[Email protected] ~] #service mysql Start

[[Email protected] ~] #chkconfig MySQL on

[[Email protected] ~] #cp/etc/{my.cnf,my.cnf.bak}

2: Native login database, and change password

[email protected] ~]# cat. Mysql_secret

# The random password set for the root user (local time): W3gquyiv// randomly generated password during installation

[[Email protected] ~] #mysql-hlocalhost-uroot-pw3gquyiv//The first time you log in with root c3> Random Password Login

Mysql>set PASSWORD for ' root ' @ ' localhost ' =password (' 123456 '); You have to change the password for the first time.


3: Build studb Library

mysql> CREATE DATABASE Studb;


4: Enter studb Library to build stuinfo table

mysql> use studb;                                   //into STUDB database operation Mysql> create table stuinfo (                       //Build Stuinfo Table     -> stu_id int (,         )                                              -> name varchar ( not null,  )   -> age tinyint (2)  not null default 22,    - > sex enum ("Boy", "Girl")  not null default  "Boy",     -> class varchar (8)  default  "NSD1404",     -> dizhi varchar (,   )  -> index (name),                                             //to field name build normal index      -> unique (stu_id));                                          //a unique index to stu_id mysql> desc stuinfo;                                              View table structure +--------+--------------------+------+-----+---------+-------+| field  | type                | Null |  key | default | extra | +--------+--------------------+------+-----+---------+-- -----+| stu_id | int ()              | YES  | UNI | NULL    |        | |  name   | varchar (        | NO )   | mul | null    |       |  | age    | tinyint (2)           | no   |     | 22      |        | |  sex    | enum (' Boy ', ' Girl ')  | NO   |      | boy     |       | |  class  | varchar (8)          | YES   |     | nsd1404 |       | |  dizhi  | varchar ()         | YES   |     | NULL    |        |+--------+--------------------+------+-----+---------+-------+


5: Add a new field ID to the primary key index and let the ID value grow automatically and modify the field Dizhi to home

Mysql> ALTER TABLE stuinfo add ID int primary key auto_increment first;        Add a new field ID to the primary key and let the ID value grow automatically mysql> ALTER TABLE stuinfo change Dizhi home varchar (30); Modify field names


6: Create a new Stuchengji table, set the foreign Key association when the table is built stuinfo

Mysql> CREATE TABLE Stuchengji (stu_id int), Chengji int (3), foreign key (stu_id) references Stui    NFO (stu_id) on UPDATE cascade on DELETE cascade); stu_id FOREIGN key Stuinfo table


7: Insert data for Stuinfo

mysql>insert into stuinfo values (null,12014002, "SW", 30, "Boy", "NSD1404", " ");   //ID field insert null placeholder mysql> select * from stuinfo;                              //View Table +----+----------+------+-----+-----+---------+----- -+| id | stu_id   | name | age | sex | class    | home |+----+----------+------+-----+-----+---------+------+|  1 |  12014001 | sw   |  30 | boy | nsd1404 |       |+----+----------+------+-----+-----+---------+------+ 


8: Import passwd , then export data to /bak

MYSQL> CREATE TABLE PASSWD (                        //Creating a Template table for the data to be imported      ->name varchar ( primary key,     ->mina varchar) (1) ,      ->uid int (5)  not null,    -> gid  int (5)  not null,     ->miaoshu varchar (,   )   ->home varchar (),      ->shell varchar ( not)  null,    ->unique (UID)); mysql> desc passwd;+---------+-------------+ ------+-----+---------+-------+| field   | type         | null | key | default | extra |+---------+-------------+ + -----+-----+---------+-------+| name    | varchar ( | no   | pri |)  null    |       | |  mina    | varchar (1)   | YES  |      | null    |       | |  uid     | int (5)       | NO    | uni | null    |       | |  gid     | int (5)       | NO    |     | NULL    |        | |  miaoshu | varchar ( | yes  |     | null)     |       | | &nBsp;home    | varchar ( | yes  |     |)  null    |       | |  shell   | varchar ( | NO   |    )  | null    |       |+---------+-------------+ ------+-----+---------+-------+mysql> load data infile "/etc/passwd"  into table  passwd  //Import passwd to table passwd and use: delimited     -> fields terminated  by  ":";


[Email protected] ~]# Mkdir/bak

[[email protected] ~]# setfacl-m u:mysql:rwx/bak// Write permission to mydql

Mysql> SELECT * from Stuinfo to outfile "/bak/stuinfo"; Export


[Email protected] ~]# Cat/bak/stuinfo

1 12014001 SW - Boy NSD1404


This article is from the "Sleepcat" blog, make sure to keep this source http://sw5720.blog.51cto.com/8812314/1433269

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.