zoukankan      html  css  js  c++  java
  • 学会使用简单的MySQL操作

    第十八章 学会使用简单的MySQL操作

    在前面两个章节中已经介绍过MySQL的安装了。可是光会安装还不够。还须要会一些主要的相关操作当然了,关于MySQL的内容也是非常多的。仅仅只是对于linux系统管理员来讲,一些主要的操作已经能够应付日常的管理工作了。至于更高深的那是DBA(专门管理数据库的技术人员)的事情了

    更改mysql数据库root的password

    首次进入数据库是不用password的

    /usr/local/mysql/bin/mysql -u root

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

    Your MySQL connection id is 2

    Server version: 5.0.86 MySQL Community Server (GPL)

    Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

    mysql>

    如今已经进入到了mysql 的操作界面了

    退出的话。直接输入exit就可以

    mysql> exit

    Bye

    先解释一下上面的命令的含义。-u 用来指定要登录的用户。root用户是mysql自带的管理员账户。默认没有password的,那么怎样给root用户设定password?按例如以下操作:

    /usr/local/mysql/bin/mysqladmin -u root password ‘123456’

    这样就能够设定root用户的password了当中mysqladmin就是用来设置password的工具,-u 指定用户,passwod 后跟要定义的password,password须要用单引號或者双引號括起来另外你或许发现了。敲命令时总在前面加/usr/local/mysql/bin/ 这样非常累可是直接打mysql 又不能用,这是由于在系统变量$PATH中没有/usr/local/mysql/bin/这个文件夹,所以须要这样操作(假设你的linux能够直接打出mysql这个命令,则不要做这个操作)

    vim /etc/profile

    在最后增加一行:

    export PATH=$PATH:/usr/local/mysql/bin/

    保存后执行

    source /etc/profile

    设定完password后,再来执行最開始进入mysql数据库操作界面的命令:

    mysql -u root

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

    就报错了,这是由于root用户有password

    mysql -u root -p

    Enter password:

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

    Your MySQL connection id is 5

    Server version: 5.0.86 MySQL Community Server (GPL)

    Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

    mysql>

    须要加-p选项指定password,这时就会提示你输入password了

    当设定password后,假设要想更改password怎样操作呢

    mysqladmin -u root -p password "123456789"

    Enter password:

    输入原来root的password就能够更改password了

    连接数据库

    刚刚讲过通过使用mysql -u root -p 就能够连接数据库了。但这仅仅是连接的本地的数据库’localhost’。然后有非常多时候都是去连接网络中的某一个主机上的mysql。

    mysql -u user1 -p –P 3306 -h 10.0.2.69

    当中-P(大写)指定远程主机mysql的绑定端口,默认都是3306-h指定远程主机的IP

    一些主要的MySQL操作命令

    1. 查询当前全部的库

    mysql> show databases;

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

    | Database |

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

    | information_schema |

    | mysql |

    | test |

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

    2. 查询某个库的表

    mysql> use mysql;

    Database changed

    mysql> show tables;

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

    | Tables_in_mysql |

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

    | columns_priv |

    | db |

    | func |

    | help_category |

    | help_keyword |

    | help_relation |

    | help_topic |

    | host |

    | proc |

    | procs_priv |

    | tables_priv |

    | time_zone |

    | time_zone_leap_second |

    | time_zone_name |

    | time_zone_transition |

    | time_zone_transition_type |

    | user |

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

    3. 查看某个表的字段

    mysql> desc func; //func 是表名

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

    | Field | Type | Null | Key | Default | Extra |

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

    | name | char(64) | NO | PRI | | |

    | ret | tinyint(1) | NO | | 0 | |

    | dl | char(128) | NO | | | |

    | type | enum('function','aggregate') | NO | | NULL | |

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

    4. 查看某个表的表结构(创建表时的具体结构)

    mysql> show create table func;

    |Table | CreateTable |

    | func | CREATE TABLE `func` (

    `name` char(64) collate utf8_bin NOT NULL default '',

    `ret` tinyint(1) NOT NULL default '0',

    `dl` char(128) collate utf8_bin NOT NULL default '',

    `type` enum('function','aggregate') character set utf8 NOT NULL,

    PRIMARY KEY (`name`)

    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='User defined functions' |

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

    5. 查看当前是哪个用户

    mysql> select user();

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

    | user() |

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

    | root@localhost |

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

    6. 查看当前所在数据库

    mysql> select database();

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

    | database() |

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

    | mysql |

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

    7. 创建一个新库

    mysql> create database db1;

    Query OK, 1 row affected (0.04 sec)

    8. 创建一个表

    mysql> create table t1 ( `id` int(4), `name` char(40));

    Query OK, 0 rows affected (0.02 sec)

    mysql> desc t1;

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

    | Field | Type | Null | Key | Default | Extra |

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

    | id | int(4) | YES | | NULL | |

    | name | char(40) | YES | | NULL | |

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

    9. 查看当前数据库版本号

    mysql> select version();

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

    | version() |

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

    | 5.0.86 |

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

    10. 查看当前系统时间

    mysql> select current_date, current_time;

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

    | current_date | current_time |

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

    | 2011-05-31 | 08:52:50 |

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

    11. 查看当前mysql的状态

    mysql> show status;

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

    | Variable_name | Value |

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

    | Aborted_clients | 0 |

    | Aborted_connects | 1 |

    | Binlog_cache_disk_use | 0 |

    | Binlog_cache_use | 0 |

    | Bytes_received | 664 |

    | Bytes_sent | 6703 |

    这个命令打出非常多东西,显示你的mysql状态

    12. 查看mysql的參数

    mysql> show variables;

    非常多參数都是能够在/etc/my.cnf中定义的

    13. 创建一个普通用户并授权

    mysql> grant all on *.* to user1 identified by '123456';

    Query OK, 0 rows affected (0.01 sec)

    all 表示全部的权限(读查询删除等等操作),*.*前面的*表示全部的数据库,后面的*表示全部的表,identified by 后面跟password,用单引號括起来这里的user1指的是localhost上的user1,假设是给网络上的其它机器上的某个用户授权则这样:

    mysql> grant all on db1.* to 'user2'@'10.0.2.100' identified by '123456';

    Query OK, 0 rows affected (0.00 sec)

    用户和主机的IP之间有一个@,另外主机IP那里能够用%替代,表示全部主机比如:

    mysql> grant all on db1.* to 'user3'@'%' identified by '123456';

    Query OK, 0 rows affected (0.00 sec)

    一些经常使用的sql

    1. 查询语句

    mysql> select count(*) from mysql.user;

    mysql.user表示mysql库的user表。count(*)表示表中共同拥有多少行

    mysql> select * from mysql.db;

    查询mysql库的db表中的全部数据

    mysql> select db from mysql.db;

    查询mysqldb表的db

    mysql> select * from mysql.db where host like '10.0.%';

    查询mysqldbhost字段like 10.0.% 的行,这里的%表示匹配全部,相似于前面介绍的通配符

    2. 插入一行

    mysql> insert into db1.t1 values (1, 'abc');

    Query OK, 1 row affected (0.00 sec)

    t1表在前面已经创建过

    mysql> select * from db1.t1;

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

    | id | name |

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

    | 1 | abc |

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

    3. 更改某一行

    mysql> update db1.t1 set name='aaa' where id=1;

    Query OK, 1 row affected (0.02 sec)

    Rows matched: 1 Changed: 1 Warnings: 0

    这样就把原来id1的那行中的name改成’aaa’

    4. 删除表

    mysql> drop table db1.t1;

    Query OK, 0 rows affected (0.01 sec)

    5. 删除数据库

    mysql> drop database db1;

    Query OK, 0 rows affected (0.07 sec)

    6. 备份与恢复库

    mysqldump -uroot -p mysql >mysql.sql

    这里的mysqldump 就是备份的工具了,-p后面的mysql指的是mysql库,把备份的文件重定向到mysql.sql。

    假设恢复的话,仅仅要:

    mysql -uroot -p mysql < mysql.sql

    关于MySQL的基本操作笔者就介绍这么多。当然学会了这些还远远不够,希望你能够在你的工作中学习到很多其它的知识。假设你对MySQL有非常大兴趣,最好还是深入研究一下,毕竟多学点总没有坏处假设想学跟多的东西请去查看MySQL官方中文參考手冊5.1

  • 相关阅读:
    oracle取字符串长度的函数length()和hengthb()
    文件操作
    numpy 库使用
    numpy 与 matplotlib 的应用过程
    使用numpy与matplotlib.pyplot画图
    面向对象的解读
    Python PIL
    Note of Jieba
    python 游戏 —— 汉诺塔(Hanoita)
    有进度条圆周率Π计算
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7290787.html
Copyright © 2011-2022 走看看