如果MySQL是使用RPM包安装的就使用服务的启动方式
[root@bogon wens]# service mysqld start
[root@bogon wens]# service mysqld restart
service mysqld stop
[root@bogon wens]# ps -ef | grep mysqld
mysql 3261 1 0 20:48 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql 3427 3261 1 20:48 ? 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 3476 2798 0 20:48 pts/0 00:00:00 grep --color=auto mysqld
//代表启动成功
[root@bogon wens]# mysql -u root -pls
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.42 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> quit
Bye
Enter passoprd 下的提示:
CRECTE DATABASE dbname
mysql> create database msg;
Query OK, 1 row affected (0.00 sec)
--分号结尾
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| company1 |
| company2 |
| k1 |
| msg2 |
| mysql |
| performance_schema |
+--------------------+
7 rows in set (0.36 sec)
以下几个库是在安装MySQL时自动创建好的
选中一个要操作的数据库
USE dbname
mysql> use msg
Database changed
select database();
SHOW ENGKINES\G;
// myisam vs innodb
mysql> help create database;
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.
URL: http://dev.mysql.com/doc/refman/5.6/en/create-database.html
drop database dbname;
mysql> drop database k1;
Query OK, 1 row affected (0.09 sec)
CREATE TABLE tablename( )
create table teacher(
name varchar(32),--姓名
age int
)CHEAST utf8;
mysql> show tables;
+---------------+
| Tables_in_msg |
+---------------+
| teacher |
+---------------+
1 row in set (0.00 sec)
DROP TABLE tablename
mysql> drop table t1;
Query OK, 0 rows affected (0.06 sec)
ALTER TABLE tablename MONDIFY[CONLUMN] column_definition[FIRST | AFTER col_name]
mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> alter table teacher modify name varchar(20);
Query OK, 1 row affected (0.32 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST | AFTER col_name]
mysql> alter table teacher add column jobid int(4);
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| jobid | int(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
ALTER TABLE tablename DROP [COLUMN] col_name
mysql> alter table teacher drop age;
Query OK, 0 rows affected (0.40 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| jobid | int(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition
[FIRST|AFTER col_name]
//同时修改字段的类型
mysql> alter table teacher change name myname varchar(21);
Query OK, 1 row affected (0.11 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| myname | varchar(21) | YES | | NULL | |
| jobid | int(4) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
将新字段插在myname 后面
mysql> alter table teacher add newage int(5) after myname;
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| myname | varchar(21) | YES | | NULL | |
| newage | int(5) | YES | | NULL | |
| jobid | int(4) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
添加字段将其放在最前面
mysql> alter table teacher add test int(2) first;
Query OK, 0 rows affected (0.39 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| test | int(2) | YES | | NULL | |
| myname | varchar(21) | YES | | NULL | |
| newage | int(5) | YES | | NULL | |
| jobid | int(4) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
修改当前字段的位置
mysql> alter table teacher modify jobid int(4) first;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc teacher;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| jobid | int(4) | YES | | NULL | |
| test | int(2) | YES | | NULL | |
| myname | varchar(21) | YES | | NULL | |
| newage | int(5) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
ALTER TABLE tablename RENAME [TO] new_tablename
mysql> alter table teacher rename new_teacher;
Query OK, 0 rows affected (0.01 sec)
mysql> desc new_teacher;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| jobid | int(4) | YES | | NULL | |
| test | int(2) | YES | | NULL | |
| myname | varchar(21) | YES | | NULL | |
| newage | int(5) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn);
mysql> insert into new_teacher(jobid, test, myname, newage) values(3, 5, 'xiao hong', 9);
Query OK, 1 row affected (0.00 sec)
mysql> insert into new_teacher values(4, 2, 'xia ming', 9);
Query OK, 1 row affected (0.28 sec)
mysql> insert into new_teacher(jobid,myname) values(6, 'xiao zhang');
Query OK, 1 row affected (0.00 sec)
mysql> insert into new_teacher values(2, 5, 'haha', 8),
-> (4, 7, 'hehe', 9),
-> (9, 5, 'heihei', 3);
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from new_teacher;
+-------+------+------------+--------+
| jobid | test | myname | newage |
+-------+------+------------+--------+
| NULL | NULL | ahsj | NULL |
| 4 | 2 | xia ming | 9 |
| 3 | 5 | xiao hong | 9 |
| 6 | NULL | xiao zhang | NULL |
| 2 | 5 | haha | 8 |
| 4 | 7 | hehe | 9 |
| 9 | 5 | heihei | 3 |
+-------+------+------------+--------+
7 rows in set (0.00 sec)
使用UPDATE,命令更新记录值
UPDATE tablename SET field1=value1,field2.=value2,……fieldn=valuen [WHERE CONDITION]
mysql> update new_teacher set newage = 100 where myname = 'haha';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from new_teacher;
+-------+------+------------+--------+
| jobid | test | myname | newage |
+-------+------+------------+--------+
| NULL | NULL | ahsj | NULL |
| 4 | 2 | xia ming | 9 |
| 3 | 5 | xiao hong | 9 |
| 6 | NULL | xiao zhang | NULL |
| 2 | 5 | haha | 100 |
| 4 | 7 | hehe | 9 |
| 9 | 5 | heihei | 3 |
+-------+------+------------+--------+
7 rows in set (0.00 sec)
UPDATE t1,t2…tn set t1.field1=expr1,tn.fieldn=exprn [WHERE CONDITION]
mysql> select * from teacher1;
+------+------+-------+
| name | age | jobid |
+------+------+-------+
| haha | 14 | 124 |
| hehe | 21 | 6 |
+------+------+-------+
2 rows in set (0.00 sec)
mysql> select * from teacher2;
+------------+------+-------+
| name | age | jobid |
+------------+------+-------+
| xiao zhang | 13 | 4 |
| liu han | 100 | 6 |
+------------+------+-------+
2 rows in set (0.00 sec)
mysql> update teacher1, teacher2 set teacher1.name = 'liu han' , teacher2.age = teacher1.age where teacher1.jobid = teacher2.jobid;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from teacher1;
+---------+------+-------+
| name | age | jobid |
+---------+------+-------+
| haha | 14 | 124 |
| liu han | 21 | 6 |
+---------+------+-------+
2 rows in set (0.00 sec)
mysql> select * from teacher2;
+------------+------+-------+
| name | age | jobid |
+------------+------+-------+
| xiao zhang | 13 | 4 |
| liu han | 21 | 6 |
+------------+------+-------+
2 rows in set (0.00 sec)
DELETE FROM tablename [WHERE CONDITION]
mysql> delete from teacher1 where name = 'haha';
Query OK, 1 row affected (0.00 sec)
mysql> select * from teacher1;
+---------+------+-------+
| name | age | jobid |
+---------+------+-------+
| liu han | 21 | 6 |
+---------+------+-------+
1 row in set (0.00 sec)
DELETE t1,t2…tn FROM t1,t2…tn [WHERE CONDITION]
mysql> delete a, b from teacher1 a, teacher2 b where a.jobid = b.jobid ;
Query OK, 2 rows affected (0.09 sec)
mysql> select * from teacher1;
+---------+------+-------+
| name | age | jobid |
+---------+------+-------+
| liu han | 21 | 6 |
+---------+------+-------+
1 row in set (0.00 sec)
mysql> select * from teacher2;
+------------+------+-------+
| name | age | jobid |
+------------+------+-------+
| xiao zhang | 13 | 4 |
| liu han | 21 | 6 |
+------------+------+-------+
2 rows in set (0.28 sec)
mysql> select * from teacher1;
Empty set (0.00 sec)
mysql> select * from teacher2;
+------------+------+-------+
| name | age | jobid |
+------------+------+-------+
| xiao zhang | 13 | 4 |
+------------+------+-------+
1 row in set (0.00 sec)
[root@bogon mysql]# vim /etc/my.cnf
//查看MySQL创建的文件
datadir=/var/lib/mysql //目录文件