您的位置:首页 > 数据库 > MySQL

mysql常用基础命令

2018-02-16 14:57 501 查看
mysql常用基础命令
1、启动mysql服务
[root@c601 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.                                       [  OK  ]
[root@c601 ~]# /etc/init.d/mysqld start
Starting MySQL.                                            [  OK  ]
提示:
2、登录mysql
mysql #无密码登录
[root@c601 ~]# mysql -uroot -p'zxin10'
3、相关基础命令
3.1查看数据库:
mysql> show databases like '%mysql%';
+--------------------+
| Database (%mysql%) |
+--------------------+
| mysql              |
+--------------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| wordpress          |
+--------------------+
4 rows in set (0.00 sec)

3.2授权数据操作:
CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
GRANT ALL ON db1.* TO 'jeffrey'@'localhost';
GRANT SELECT ON db2.invoice TO 'jeffrey'@'localhost';
GRANT USAGE ON *.* TO 'jeffrey'@'localhost' WITH MAX_QUERI
4000
ES_PER_HOUR 90;
3.3退出数据库
exit
4.设置数据库登录密码
设置密码方法:
mysqladmin -u root password 'zxin10'
mysqladmin -u root password 'zxin10' -S /data/3306/mysql.sock
修改root密码:
mysqladmin -u root -p 'zxin10' password 'zxin10'
mysqladmin -u root -p 'zxin10' password 'zxin10' -S /data/3006/mysql.sock
5.如何找回丢失的mysql root用户密码
#忽略授权表启动
killall mysqld
mysql_safe --skip-grant-tables &
mysql -uroot -p
#登录进mysql后,更新用户密码。
 UPDATE mysql.user SET password=PASSWORD("zxin11") WHERE user='root';
flush privleges; 
#重启登录测试
killall mysqld
/etc/init.d/mysql start
mysql -uroot -p'zxin11'

二、数据库操作
2.1创建数据库
mysql> create database zxin10_default;
Query OK, 1 row affected (0.00 sec)

mysql> show create database zxin10_default;
+----------------+---------------------------------------------------------------------------+
| Database       | Create Database                                                           |
+----------------+---------------------------------------------------------------------------+
| zxin10_default | CREATE DATABASE `zxin10_default` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
2.2创建一个gbk/utf8的数据库
mysql> create database zxin10_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
mysql> create database zxin10_utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_chinese_ci;
mysql> show create database zxin10_utf8
    -> ;
+------------+---------------------------------------------------------------------+
| Database   | Create Database                                                     |
+------------+---------------------------------------------------------------------+
| zxin10_gbk | CREATE DATABASE `zxin10_gbk` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+------------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
2.3显示数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| wordpress          |
| zxin10_default     |
| zxin10_gbk         |
+--------------------+
6 rows in set (0.00 sec)

mysql> show databases like 'zxin10%';
+--------------------+
| Database (zxin10%) |
+--------------------+
| zxin10_default     |
| zxin10_gbk         |
+--------------------+
2 rows in set (0.00 sec)
2.4删除数据库
mysql> drop database zxin10_gbk;
Query OK, 0 rows affected (0.02 sec)
2.5查看数据
mysql> use test;
Database changed
#查看数据库
mysql> select database();
+------------+
| database() |
+------------+
| test       |
+------------+
1 row in set (0.00 sec)
#查看版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.62    |
+-----------+
1 row in set (0.00 sec)
#查看用户
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)

2.6删除系统多余账号
mysql> drop user 'zxin10'@'localhost

2.7查看系统账号
mysql> select user,host from mysql.user;
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| root      | 127.0.0.1 |
| root      | localhost |
| wordpress | localhost |
+-----------+-----------+
3 rows in set (0.00 sec)

2.8授权用户

mysql> help grant
Name: 'GRANT'
Description:
Syntax:
GRANT
    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    TO user_specification [, user_specification] ...
    [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
    [WITH with_option ...]

object_type:
    TABLE
  | FUNCTION
  | PROCEDURE

priv_level:
    *
  | *.*
  | db_name.*
  | db_name.tbl_name
  | tbl_name
  | db_name.routine_name

user_specification:
    user [IDENTIFIED BY [PASSWORD] 'password']

ssl_option:
    SSL
  | X509
  | CIPHER 'cipher'
  | ISSUER 'issuer'
  | SUBJECT 'subject'

with_option:
    GRANT OPTION
  | MAX_QUERIES_PER_HOUR count
  | MAX_UPDATES_PER_HOUR count
  | MAX_CONNECTIONS_PER_HOUR count
  | MAX_USER_CONNECTIONS count

The GRANT statement grants privileges to MySQL user accounts. GRANT
also serves to specify other account characteristics such as use of
secure connections and limits on access to server resources. To use
GRANT, you must have the GRANT OPTION privilege, and you must have the
privileges that you are granting.

Normally, a database administrator first uses CREATE USER to create an
account, then GRANT to define its privileges and characteristics. For
example:

CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
GRANT ALL ON db1.* TO 'jeffrey'@'localhost';
GRANT SELECT ON db2.invoice TO 'jeffrey'@'localhost';
GRANT USAGE ON *.* TO 'jeffrey'@'localhost' WITH MAX_QUERIES_PER_HOUR 90;

However, if an account named in a GRANT statement does not already
exist, GRANT may create it under the conditions described later in the
discussion of the NO_AUTO_CREATE_USER SQL mode.

The REVOKE statement is related to GRANT and enables administrators to
remove account privileges. See [HELP REVOKE].

To determine what privileges an account has, use SHOW GRANTS. See [HELP
SHOW GRANTS].

URL: http://dev.mysql.com/doc/refman/5.1/en/grant.html
2.9收回权限
mysql> help revoke
Name: 'REVOKE'
Description:
Syntax:
REVOKE
    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    FROM user [, user] ...

REVOKE ALL PRIVILEGES, GRANT OPTION
    FROM user [, user] ...

The REVOKE statement enables system administrators to revoke privileges
from MySQL accounts. Each account name uses the format described in http://dev.mysql.com/doc/refman/5.1/en/account-names.html. For example:

REVOKE INSERT ON *.* FROM 'jeffrey'@'localhost';

If you specify only the user name part of the account name, a host name
part of '%' is used.

For details on the levels at which privileges exist, the permissible
priv_type and priv_level values, and the syntax for specifying users
and passwords, see [HELP GRANT]

To use the first REVOKE syntax, you must have the GRANT OPTION
privilege, and you must have the privileges that you are revoking.

To revoke all privileges, use the second syntax, which drops all
global, database, table, column, and routine privileges for the named
user or users:

REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...

To use this REVOKE syntax, you must have the global CREATE USER
privilege or the UPDATE privilege for the mysql database.

URL: http://dev.mysql.com/doc/refman/5.1/en/revoke.html
三、表操作
3.1建表
mysql> use zxin10_default
Database changed
mysql> create table test(
    -> id int(4) not null primary key auto_increment,
    -> name char(20) not null
    -> );
Query OK, 0 rows affected (0.02 sec)
3.2查看表
mysql> desc test;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(4)   | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | NO   |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
3.3查看建表语句
mysql> show create table test \G
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE `test` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

3.4插入数据
mysql> show tables;
+--------------------------+
| Tables_in_zxin10_default |
+--------------------------+
| test                     |
+--------------------------+
1 row in set (0.00 sec)

mysql> insert into test(id,name) values(1,'Baizuo');
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | Baizuo |
+----+--------+
1 row in set (0.00 sec)

3.5数据备份
mysql> system mysqldump -uroot -p'zxin10' -A -B >/tmp/zxin10.sql
mysql> system ls -l /tmp/zxin10.sql
-rw-r--r-- 1 root root 836597 Feb 16 14:30 /tmp/zxin10.sql
3.6查询表数据
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | Baizuo |
+----+--------+
1 row in set (0.00 sec)

mysql> select * from test where name like 'B%' limit 2;
+----+--------+
| id | name   |
+----+--------+
|  1 | Baizuo |
+----+--------+
1 row in set (0.00 sec)
说明:limit查两行
3.7修改表中的数据
mysql> select * from test where name like 'B%' limit 2;
+----+--------+
| id | name   |
+----+--------+
|  1 | Baizuo |
+----+--------+
1 row in set (0.00 sec)

mysql> update test set name='oldboy' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | oldboy |
+----+--------+
1 row in set (0.00 sec)
3.8删除表中的数据
mysql> select * from test;
+----+------------+
| id | name       |
+----+------------+
|  1 | oldboy     |
|  2 | Baizuo     |
|  3 | duangduang |
+----+------------+
3 rows in set (0.00 sec)

mysql> delete from test where id = 1;
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+------------+
| id | name       |
+----+------------+
|  2 | Baizuo     |
|  3 | duangduang |
+----+------------+
2 rows in set (0.00 sec)
3.9清空表中的所有数据。
mysql> select * from test;
+----+------------+
| id | name       |
+----+------------+
|  2 | Baizuo     |
|  3 | duangduang |
+----+------------+
2 rows in set (0.00 sec)

mysql> truncate table test;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
Empty set (0.00 sec)
3.10在表中增加字段(db操作)
mysql> alter table test add sex char(4);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(4)   | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | NO   |     | NULL    |                |
| sex   | char(4)  | YES  |     | NULL    |                |
+-------+----------+-----

mysql> alter table test add age int(4) after name;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test \G
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE `test` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  `age` int(4) DEFAULT NULL,
  `sex` char(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

3.11在表中删除字段
mysql> desc test;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(4)   | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | NO   |     | NULL    |                |
| age   | int(4)   | YES  |     | NULL    |                |
| sex   | char(4)  | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> alter table test drop sex;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(4)   | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | NO   |     | NULL    |                |
| age   | int(4)   | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

3.12更改表名
mysql> RENAME table test TO zxin10_test
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+--------------------------+
| Tables_in_zxin10_default |
+--------------------------+
| zxin10_test              |
+--------------------------+
1 row in set (0.00 sec)

帮助:
mysql> help rename
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
   ALTER EVENT
   ALTER TABLE
   RENAME TABLE
   RENAME USER

3.13 删除表
#改回来
mysql> rename table zxin10_test to test;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+--------------------------+
| Tables_in_zxin10_default |
+--------------------------+
| test                     |
+--------------------------+
1 row in set (0.01 sec)
#删除test表
mysql> drop table test;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)

3.2
字段类型
1)INT
2)DOUBLE
3)DATE
4)CHA
9934
R
5)BLOB TEXT
6)VARCHAR
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息