在前面两个章节中已经介绍过MySQL的安装了,但是光会安装还不够,还需要会一些基本的相关操作。当然了,关于MySQL的内容也是非常多的,只不过对于linux系统管理员来讲,一些基本的操作已经可以应付日常的管理工作了,至于更高深的那是DBA(专门管理数据库的技术人员)的事情了。 【更改mysql数据库root的密码】 首次进入数据库是不用密码的 /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自带的管理员账户,默认没有密码的,那么如何给root用户设定密码?按如下操作: /usr/local/mysql/bin/mysqladmin -u root password ‘123456’ 这样就可以设定root用户的密码了。其中mysqladmin就是用来设置密码的工具,-u 指定用户,passwod 后跟要定义的密码,密码需要用单引号或者双引号括起来。另外你也许发现了,敲命令时总在前面加/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 设定完密码后,再来运行最开始进入mysql数据库操作界面的命令: mysql -u root ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 就报错了,这是因为root用户有密码。 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选项指定密码,这时就会提示你输入密码了。 当设定密码后,如果要想更改密码如何操作呢? mysqladmin -u root -p password "123456789" Enter password: 输入原来root的密码就可以更改密码了。 【连接数据库】 刚刚讲过通过使用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 后面跟密码,用单引号括起来。这里的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; 查询mysql库db表的db段。 mysql> select * from mysql.db where host like '10.0.%'; 查询mysql库db表host字段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 这样就把原来id为1的那行中的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)。 |
|
来自: 凌蓝苑 > 《linux入门教程》