码迷,mamicode.com
首页 > 数据库 > 详细

MySql基本操作(一)

时间:2015-03-29 18:14:12      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:mysql基础命令

一、MySQL服务操作

1、启动与关闭Mysql

将源码support-files目录中的mysql.server复制到/etc/init.d/mysqld

[root@localhost ~]# /etc/init.d/mysqld start

Starting MySQL.....                                        [  OK  ]

[root@localhost ~]# netstat -lnt |grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN     

 

[root@localhost ~]# /etc/init.d/mysqld stop

Shutting down MySQL.                                       [  OK  ]

 

2、登陆MySQL方法

1mysql                                      ----刚装完系统无密码情况登录方式

2mysql -uroot                         ----刚装完系统无密码情况登录方式

3mysql -uroot -p                     ---标准的dba命令行登录命令

4mysql -uroot -p‘密码‘         --脚本中使用,密码明文会泄露密码

 

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g---mysql命令以分号;\g结束。

Your MySQL connection id is 4

Server version: 5.5.38-log Source distribution                                       ---版本信息

 

Copyright (c) 2000, 2013, 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>                                 --mysql的提示符

 

3mysql 的帮助

MySQL中的helplinux命令行的man相似。想查看MySQL中的命令语法,就需要用help,如help create,MySQL中不区分大小写。

 

mysql> help

 

For information about MySQL products and services, visit:

   http://www.mysql.com/

For developer information, including the MySQL Reference Manual, visit:

   http://dev.mysql.com/

To buy MySQL Enterprise support, training, or other products, visit:

   https://shop.mysql.com/

 

List of all MySQL commands:

Note that all text commands must be first on line and end with ‘;‘

?         (\?) Synonym for `help‘.

clear     (\c) Clear the current input statement.

connect   (\r) Reconnect to the server. Optional arguments are db and host.

delimiter (\d) Set statement delimiter.

edit      (\e) Edit command with $EDITOR.

ego       (\G) Send command to mysql server, display result vertically.

exit      (\q) Exit mysql. Same as quit.

go        (\g) Send command to mysql server.

help      (\h) Display this help.

nopager   (\n) Disable pager, print to stdout.

notee     (\t) Don‘t write into outfile.

pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.

print     (\p) Print current command.

prompt    (\R) Change your mysql prompt.

quit      (\q) Quit mysql.

rehash    (\#) Rebuild completion hash.

source    (\.) Execute an SQL script file. Takes a file name as an argument.

status    (\s) Get status information from the server.

system    (\!) Execute a system shell command.

tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.

use       (\u) Use another database. Takes database name as argument.

charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.

warnings  (\W) Show warnings after every statement.

nowarning (\w) Don‘t show warnings after every statement.

 

For server side help, type ‘help contents‘

 

mysql> help create

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:

   CREATE DATABASE

   CREATE EVENT

   CREATE FUNCTION

   CREATE FUNCTION UDF

   CREATE INDEX

   CREATE LOGFILE GROUP

   CREATE PROCEDURE

   CREATE SERVER

   CREATE TABLE

   CREATE TABLESPACE

   CREATE TRIGGER

   CREATE USER

   CREATE VIEW

   SHOW

   SHOW CREATE DATABASE

   SHOW CREATE EVENT

   SHOW CREATE FUNCTION

   SHOW CREATE PROCEDURE

   SHOW CREATE TABLE

   SPATIAL

 

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.5/en/create-database.html

 

4、退出MySQL方法

mysql> quit exit

 

 

二、设置及修改mysql root用户密码

1、设置密码的方法

安装mysql后,默认的管理员root密码为空,不安全,需要修改。

[root@localhost ~]# mysqladmin -uroot password ‘密码‘       --适合没有密码的情况,安装后

[root@localhost ~]# mysqladmin  -uroot -p ‘旧密码‘ password ‘新密码‘  --已知密码,改密码

 

2、修改root密码

1) [root@localhost ~]# mysqladmin -uroot -p‘pwd@123‘ password ‘123‘

2mysql> select user,host,password from mysql.user;

+------+-----------+-------------------------------------------+

| user | host      | password                                  |

+------+-----------+-------------------------------------------+

| root |localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

+------+-----------+-------------------------------------------+

1 row in set (0.01 sec)

 

mysql> update mysql.user

    -> set password=password(‘pwd@123‘)

    -> where user=‘root‘ and host=‘localhost‘;

Query OK, 1 row affected (0.22 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

---此方法适合--skip-grant-tables方式修改密码

 

3mysql> set password=password(‘123‘);

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

---此方法不适合--skip-grant-tables方式修改密码

 

mysql> set password=password(‘benet‘);

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

 

3、如何找回丢失的密码

1)先停止msyqld服务

[root@localhost ~]# /etc/init.d/mysqld stop

Shutting down MySQL.                                       [  OK  ]

2)使用忽略授权表的方式登录

[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &

[1] 50520

[root@localhost ~]# 140722 10:11:02 mysqld_safe Logging to ‘/usr/local/mysql/data/localhost.err‘.

140722 10:11:02 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

 

[root@localhost ~]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.38-log Source distribution

 

Copyright (c) 2000, 2013, 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> mysql> select user,host,password from mysql.user;

+------+-----------+-------------------------------------------+

| user | host      | password                                  |

+------+-----------+-------------------------------------------+

| root |localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

+------+-----------+-------------------------------------------+

1 row in set (0.01 sec)

 

mysql> update mysql.user

    -> set password=password(‘pwd@123‘)

    -> where user=‘root‘ and host=‘localhost‘;

Query OK, 1 row affected (0.22 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

3)重启用新密码登录

[root@localhost ~]# killall -9 mysqld

mysqld: no process killed

[root@localhost ~]# /etc/init.d/mysqld restart

MySQL server PID file could not be found!                  [FAILED]

Starting MySQL...                                          [  OK  ]

 

三、创建数据库

1、建立一个名为benet的默认数据库

mysql> create database benet_default;

Query OK, 1 row affected (0.00 sec)

 

mysql> show databases;              

+--------------------+

| Database           |

+--------------------+

| information_schema |

| benet_default      |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.00 sec)

 

mysql> show create database benet_default;

+---------------+--------------------------------------------------------------------------+

| Database      | Create Database                                                          |

+---------------+--------------------------------------------------------------------------+

| benet_default | CREATE DATABASE `benet_default` /*!40100 DEFAULT CHARACTER SET latin1 */ |

+---------------+--------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> create database benet_gbk default character set gbk collate gbk_chinese_ci;

Query OK, 1 row affected (0.01 sec)

 

mysql> show create database benet_gbk;

+-----------+-------------------------------------------------------------------+

| Database  | Create Database                                                   |

+-----------+-------------------------------------------------------------------+

| benet_gbk | CREATE DATABASE `benet_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ |

+-----------+-------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> create database benet_utf8 character set utf8 collate utf8_general_ci;

Query OK, 1 row affected (0.00 sec)

 

mysql> show create database benet_utf8;

+------------+---------------------------------------------------------------------+

| Database   | Create Database                                                     |

+------------+---------------------------------------------------------------------+

| benet_utf8 | CREATE DATABASE `benet_utf8` /*!40100 DEFAULT CHARACTER SET utf8 */ |

+------------+---------------------------------------------------------------------+

1 row in set (0.00 sec)

 

2、显示数据库

mysql> show databases like ‘%benet%‘;

+--------------------+

| Database (%benet%) |

+--------------------+

| benet_default      |

| benet_gbk          |

| benet_utf8         |

+--------------------+

3 rows in set (0.00 sec)

 

3、删除数据库

mysql> drop database if exists benet_default;

Query OK, 0 rows affected (0.01 sec)

 

mysql> show databases like ‘%benet%‘;       

+--------------------+

| Database (%benet%) |

+--------------------+

| benet_gbk          |

| benet_utf8         |

+--------------------+

2 rows in set (0.00 sec)

 

四、连接数据库

1mysql> help use

Name: ‘USE‘

Description:

Syntax:

USE db_name

 

The USE db_name statement tells MySQL to use the db_name database as

the default (current) database for subsequent statements. The database

remains the default until the end of the session or another USE

statement is issued:

 

USE db1;

SELECT COUNT(*) FROM mytable;   # selects from db1.mytable

USE db2;

SELECT COUNT(*) FROM mytable;   # selects from db2.mytable

 

URL: http://dev.mysql.com/doc/refman/5.5/en/use.html

 

mysql> select database();   ---查看当前连接的数据库

+------------+

| database() |

+------------+

| NULL       |

+------------+

1 row in set (0.00 sec)

 

mysql> system pwd;

/root

mysql> use benet_gbk;

Database changed

mysql> select database();

+------------+

| database() |

+------------+

| benet_gbk  |

+------------+

1 row in set (0.00 sec)

 

mysql> select version();            --查看当前版本

+------------+

| version()  |

+------------+

| 5.5.38-log |

+------------+

1 row in set (0.00 sec)

 

mysql> select user();                  ----查看当前用户

+----------------+

| user()         |

+----------------+

| root@localhost |

+----------------+

1 row in set (0.00 sec)

 

mysql> use mysql

Database changed

mysql> show tables;                  --查看表

+---------------------------+

| Tables_in_mysql           |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_category             |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| proc                      |

| procs_priv                |

| proxies_priv              |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

24 rows in set (0.00 sec)

 

五、删除系统多余帐号

mysql> drop user ‘root‘@‘127.0.0.1‘;

本文出自 “R博士的自由空间” 博客,请务必保留此出处http://zhangxiaoguang.blog.51cto.com/79856/1626185

MySql基本操作(一)

标签:mysql基础命令

原文地址:http://zhangxiaoguang.blog.51cto.com/79856/1626185

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!