Table of contents
1. centos6.5 install mysql5 version
1.1 Install Mysql database as su super user
1.5 Set password 123456 to display all databases
1.8 Re-enter the password 123456 to enter the database
3. Solve the problem of installing MYSQL
3.1 Use su super user to check the running status of mysql
3.4 Check the process and close
4.7 Save the table structure and data of student1 to the new table student
4.8 Add id primary key to table structure student
4.9 Table student1 deletes the row with id=1
4.10 table student1 delete all data
5.1 Create user (@local user, %network user)
5.6 To go back and use the root account to delete the mysql account
1. centos6.5 install mysql5 version
1.1 Install Mysql database as su super user
[st01@master ~]$ rpm -ivh MySQL-server-5.5.48-1.linux2.6.i386.rpm --force --nodeps
1.2 Start Mysql database
[root@master st01]# service mysql start
1.3. Install Mysql client
[root@master st01]# rpm -ivh MySQL-client-5.5.48-1.linux2.6.i386.rpm --force --nodeps
1.4 Enter Mysql
[root@master st01]# mysql
1.5 Set password 123456 to display all databases
mysql> set password for 'root'@'localhost' = password('123456');
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
1.6 Enter the database test
mysql> use test
Database changed
1.7 Create database tables
mysql> create table hello(id int);
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| hello |
+----------------+
1 row in set (0.00 sec)
mysql> exit
Bye
1.8 Re-enter the password 123456 to enter the database
[root@master hive]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.5.48 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hive |
| mysql |
| performance_schema |
| test |
+--------------------+
2. Basic operation of data
[stu@localhost ~]$ su
password:
[root@localhost stu]# mysql -u root -p
Enter password: 123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.48 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2.1 View database
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.02 sec)
2.2 Create a database
mysql> create database mbdb1;
Query OK, 1 row affected (0.00 sec)
Delete the database:
mysql> drop database mbdb1;
Query OK, 0 rows affected (0.03 sec)
mysql> create database mydb1;
Query OK, 1 row affected (0.01 sec)
Create a utf8-encoded database:
mysql> create database mydb2 character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show create database mydb2;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| mydb2 | CREATE DATABASE `mydb2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
2.3 Enter the database
mysql> use mydb2;
Database changed
2.4 Create table
create table student (
id int,
name varchar(20),
chinese double,
english double,
math double);
mysql> create table student(
-> id int,
-> name varchar(20),
-> chinese double,
-> english double,
-> math double
-> );
Query OK, 0 rows affected (0.04 sec)
2.5 Write table data
insert into student(id,name,chinese,english,math)
values(1,'Zhang San',78.8,98,66);
insert into student(id,name,chinese,english,math)
values(2,'Lisi',88.5,68,96);
mysql> insert into student(id,name,chinese,english,math)
-> values(1,'Zhang San',78.8,98,66);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student(id,name,chinese,english,math)
-> values(2,'Lisi',88.5,68,96);
Query OK, 1 row affected (0.03 sec)
mysql> select * from student;
+------+--------+---------+---------+------+
| id | name | chinese | english | math |
+------+--------+---------+---------+------+
| 1 | Zhang San | 78.8 | 98 | 66 |
| 2 | Lee Si | 88.5 | 68 | 96 |
+------+--------+---------+---------+------+
2 rows in set (0.01 sec)
3. Solve the problem of installing MYSQL
3.1 Use su super user to check the running status of mysql
[root@localhost desktop]# service mysql status
SUCCESS! MySQL running (2389)
3.2 Check mysql installation
[root@localhost desktop]# rpm -qa|grep mysql
mysql-libs-5.1.73-5.el6_6.i686
3.3 Uninstall
[root@localhost desktop]# rpm -e mysql-libs-5.1.73-5.el6_6.i686
3.4 Check the process and close
[root@localhost desktop]# ps -aux|grep mysql
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 2189 0.0 0.1 5124 1396 ? S 17:24 0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid
mysql 2389 0.1 3.1 332068 32420 ? Sl 17:24 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/lib/mysql/localhost.localdomain.pid --socket=/var/lib/mysql/mysql.sock
root 3066 0.0 0.0 6056 796 pts/0 S+ 17:28 0:00 grep mysql
[root@localhost desktop]# kill -9 2389
[root@localhost desktop]# service mysql status
SUCCESS! MySQL running (3112)
Clean up mysql -> Uncheck -> Apply
rm -rf /var/lib/mysql
4. Database DML operation
[stu@localhost ~]$ su
password:
[root@localhost stu]# mysql -u root -p
Enter password: 123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.48 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
4.1 View database
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.02 sec)
4.2 Create a database
mysql> use mydb1;
Database changed
mysql> show tables;
Empty set (0.00 sec)
create table student (
id int,
name varchar(20),
chinese double,
english double,
math double);
mysql> create table student(
-> id int,
-> name varchar(20),
-> chinese double,
-> english double,
-> math double
-> );
Query OK, 0 rows affected (0.04 sec)
insert into student(id,name,chinese,english,math)
values(1,'Zhang San',78.8,98,66);
insert into student(id,name,chinese,english,math)
values(2,'Lisi',88.5,68,96);
4.3 Add table columns
mysql> alter table student add age varchar(5);
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+------+--------+---------+---------+------+------+
| id | name | chinese | english | math | age |
+------+--------+---------+---------+------+------+
| 1 | Zhang San | 78.8 | 98 | 66 | NULL |
| 2 | Lee Si | 88.5 | 68 | 96 | NULL |
+------+--------+---------+---------+------+------+
2 rows in set (0.00 sec)
4.4 Modify column type
Change varhcar(5) to int type
mysql> alter table student modify age int;
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
4.5 Modify column names
mysql> alter table student change age username varchar(20);
Query OK, 2 rows affected (0.23 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+------+--------+---------+---------+------+----------+
| id | name | chinese | english | math | username |
+------+--------+---------+---------+------+----------+
| 1 | Zhang San | 78.8 | 98 | 66 | NULL |
| 2 | Lee Si | 88.5 | 68 | 96 | NULL |
+------+--------+---------+---------+------+----------+
4.6 Modify table name
mysql> rename table student to student1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student1;
+------+--------+---------+---------+------+----------+
| id | name | chinese | english | math | username |
+------+--------+---------+---------+------+----------+
| 1 | Zhang San | 78.8 | 98 | 66 | NULL |
| 2 | Lee Si | 88.5 | 68 | 96 | NULL |
+------+--------+---------+---------+------+----------+
2 rows in set (0.00 sec)
4.7 Save the table structure and data of student1 to the new table student
mysql> create table student as select * from student1;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+------+--------+---------+---------+------+----------+
| id | name | chinese | english | math | username |
+------+--------+---------+---------+------+----------+
| 1 | Zhang San | 78.8 | 98 | 66 | NULL |
| 2 | Lee Si | 88.5 | 68 | 96 | NULL |
+------+--------+---------+---------+------+----------+
2 rows in set (0.00 sec)
4.8 Add id primary key to table structure student
mysql> alter table student add constraint primary key(id);
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
4.9 Table student1 deletes the row with id=1
mysql> delete from student1 where id=1;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student1;
+------+--------+---------+---------+------+----------+
| id | name | chinese | english | math | username |
+------+--------+---------+---------+------+----------+
| 2 | Lee Si | 88.5 | 68 | 96 | NULL |
+------+--------+---------+---------+------+----------+
1 row in set (0.00 sec)
4.10 table student1 delete all data
mysql> truncate table student1;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from student1;
Empty set (0.00 sec)
4.11 Delete table student1
mysql> drop table student1;
5. Database user operations
[stu@localhost ~]$ su
password:
[root@localhost stu]# mysql -u root -p
Enter password: 123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.48 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
5.1 Create user (@local user, %network user)
mysql> create user mysql@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant select on *.* to mysql@localhost;
Query OK, 0 rows affected (0.00 sec)
5.2 View user permissions
mysql> show grants for mysql@localhost;
+---------------------------------------------------------------------------------------------------------------+
| Grants for mysql@localhost |
+---------------------------------------------------------------------------------------------------------------+
| GRANT SELECT ON *.* TO 'mysql'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
5.3 Revoking user rights
mysql> revoke select on *.* from mysql@localhost;
Query OK, 0 rows affected (0.02 sec)
5.4 Change password
mysql> update mysql.user set password=password('234567') where user='mysql';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5.5 Open a new terminal
[root@localhost mysql]# mysql -u mysql -p
Enter password: 123456
ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: YES)
[root@localhost mysql]# mysql -u mysql -p
Enter password: 234567
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.48 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
5.6 To go back and use the root account to delete the mysql account
mysql> drop user mysql;
6. Database query operation
6.1 Enter the database
[root@localhost mysql]# mysql -u root -p
mysql> user mydb1;
mysql> select * from student where chinese<80;
+----+--------+---------+---------+------+----------+
| id | name | chinese | english | math | username |
+----+--------+---------+---------+------+----------+
| 1 | Zhang San | 78.8 | 98 | 66 | NULL |
+----+--------+---------+---------+------+----------+
mysql> select * from student where chinese>80 and chinese<90;
+----+--------+---------+---------+------+----------+
| id | name | chinese | english | math | username |
+----+--------+---------+---------+------+----------+
| 2 | Lee Si | 88.5 | 68 | 96 | NULL |
+----+--------+---------+---------+------+----------+
1 row in set (0.00 sec)
mysql> select * from student where chinese between 80 and 90;
+----+--------+---------+---------+------+----------+
| id | name | chinese | english | math | username |
+----+--------+---------+---------+------+----------+
| 2 | Lee Si | 88.5 | 68 | 96 | NULL |
| 3 | Li | 90 | 90 | 47 | NULL |
+----+--------+---------+---------+------+----------+
2 rows in set (0.00 sec)
mysql> select * from student order by chinese desc;
+----+--------+---------+---------+------+----------+
| id | name | chinese | english | math | username |
+----+--------+---------+---------+------+----------+
| 3 | Li | 90 | 90 | 47 | NULL |
| 2 | Lee Si | 88.5 | 68 | 96 | NULL |
| 1 | Zhang San | 78.8 | 98 | 66 | NULL |
+----+--------+---------+---------+------+----------+
3 rows in set (0.00 sec)
mysql> select id,name,(chinese+english+math) score from student;
+----+--------+-------+
| id | name | score |
+----+--------+-------+
| 1 | Zhang San | 242.8 |
| 2 | Li Si | 252.5 |
| 3 | Li | 227 |
+----+--------+-------+
3 rows in set (0.00 sec)
mysql> select * from (select id,name,(chinese+english+math) score from student) vw order by vw.score;
+----+--------+-------+
| id | name | score |
+----+--------+-------+
| 3 | Li | 227 |
| 1 | Zhang San | 242.8 |
| 2 | Li Si | 252.5 |
+----+--------+-------+
3 rows in set (0.00 sec)
One-click three-in-one!
One-click three-in-one!
One-click three-in-one!