CentOS6.5下简单的MySQL数据库操作

时间:2022-01-25 02:19:38

1.登录成功之后退出的话,直接输入quit或者exit即可。

CentOS6.5下简单的MySQL数据库操作

2.使用.tar.gz的包安装MySQL时,进入MySQL需要使用绝对路径

 

[root@winner ~]# /usr/local/mysql/bin/mysql -uroot

 

单独只是输入一个"mysql" 命令是不行的,因为"/usr/local/mysql/bin" 没有在 PATH 这个环境变量里(使用yum安装不需要)。如何把它加入环境变量PATH中?

 

[root@winner ~]# PATH=$PATH:/usr/local/mysql/bin

 

这样就可以了,但重启Linux后还会失效,所以需要让它开机加载:

 

[root@winner ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@winner
~]# source /etc/profile
[root@localhost
~]# mysql -uroot

 

-u 用来指定要登录的用户,后边可以有空格,也可以无空格。-p 后面可以直接跟密码,后面不可以有空格,不过密码最好用单引号括起来,不括也可以,但是密码中如果有特殊字符就会有问题了,所以最好是括起来。

 

连接数据库

通过使用 mysql -u root -p 可以连接数据库,但这只是连接的本地的数据库"localhost", 可是有很多时候都是去连接网络中的某一个主机上的mysql。

 

[root@winner ~]# mysql -uroot -p -h192.168.137.10 -P3306
Enter password:

 

其中后边的 -P(大写) 用来指定远程主机MySQL的绑定端口,默认都是3306, -h 用来指定远程主机的IP.

 

一些基本的MySQL操作命令

1. 查询当前的库

 

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| winner |
+--------------------+
rows
in set (0.07 sec)

 

2. 查询某个库的表

 

首先需要切换到某个库中
mysql
> use mysql
Reading table information
for completion of table and column names
You can turn off
this feature to get a quicker startup with -A
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 |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
rows
in set (0.00 sec)

 

3. 查看某个表的全部字段

 

mysql> desc host;
+-----------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
+-----------------------+---------------+------+-----+---------+-------+
rows
in set (0.00 sec)

 

也可以使用另一条命令,显示比这个更详细,而且可以把建表语句全部列出来:

 

mysql> show create table host\G;
*************************** 1. row ***************************
Table: host
Create Table: CREATE TABLE `host` (
`Host`
char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`Db`
char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`Select_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv`
enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
PRIMARY KEY (`Host`,`Db`)
) ENGINE
=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Host privileges; Merged with database privileges'
row
in set (0.00 sec)

 

4. 查看当前是哪个用户

 

mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
row
in set (0.00 sec)

 

5. 查看当前所使用数据库

 

mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
row
in set (0.00 sec)

 

6. 创建一个新库

 

mysql> create database db1;
Query OK,
1 row affected (0.02 sec)

 

7. 创建一个新表

 

mysql> create table t1 (`id` int(4), `name` char(40));
Query OK,
0 rows affected (0.02 sec)

 

8. 查看当前数据库版本

 

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.73 |
+-----------+
row
in set (0.00 sec)

 

9. 查看当前MySQL状态

 

mysql> show status;
+-----------------------------------+----------+
| Variable_name | Value |
+-----------------------------------+----------+
| Aborted_clients | 1 |
| Aborted_connects | 2 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Bytes_received | 1194 |
| Bytes_sent | 21732 |
| Com_admin_commands | 0 |
| Com_assign_to_keycache | 0 |
| Com_alter_db | 0 |
| Com_alter_db_upgrade | 0 |
| Com_alter_event | 0 |

 

10. 查看MySQL的参数

 

mysql> show variables;
+-----------------------------------------+---------------------+
| Variable_name | Value |
+-----------------------------------------+---------------------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
| autocommit | ON |
| automatic_sp_privileges | ON |
| back_log | 50 |
| basedir | /usr/local/mysql/ |

 

11. 修改MySQL的参数

 

mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 10 |
| max_connections | 151 |
+--------------------+-------+
rows
in set (0.00 sec)

mysql
> set global max_connect_errors = 1000;
Query OK,
0 rows affected (0.00 sec)

mysql
> show variables like 'max_connect_errors';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
+--------------------+-------+
row
in set (0.00 sec)

 

在MySQL命令行, "%"类似于shell下的 *, 表示万能匹配。使用 "set global" 可以临时修改某些参数,但是重启mysqld服务后还会变为原来的,所以要想恒久生效,需要在配置文件 my.cnf 中定义。

12. 查看当前MySQL服务器的队列

这个在日常的管理工作中使用最为频繁,因为使用它可以查看当前mysql在干什么,可以发现是否有锁表:

 

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 9 | root | localhost | db1 | Query | 0 | NULL | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
row
in set (0.00 sec)

 

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 '111222';
Query OK,
0 rows affected (0.01 sec)

 

用户和主机的IP之间有一个@,另外主机IP那里可以用%替代,表示所有主机,例如:

 

mysql> grant all on db1.* to 'user3'@'%' identified by '231222';
Query OK,
0 rows affected (0.00 sec)

 

MySQL数据库的备份与恢复

备份:

[root@winner ~]# mysqldump  -uroot -p'root' mysql >/tmp/mysql.sql
不是在mysql
>环境

使用 mysqldump 命令备份数据库,-u 和 -p 两个选项使用方法和前面说的 mysql 同样,而后面的 “mysql” 指的是库名,然后重定向到一个文本文档里。备份完后,你可以查看 /tmp/mysql.sql 这个文件里的内容。

恢复和备份正好相反:

[root@localhost ~]# mysql -uroot -p'yourpassword' mysql </tmp/mysql.sql