MySQL数据库基础操作

本文介绍了如何解决MySQL登录提示预读表信息导致的延迟问题,包括如何在安全模式下重置root密码。此外,还讲解了如何开启Tab补全功能、设置数据库支持简体中文,以及数据库的常用管理操作,如查看、创建、删除数据库和表,以及数据的插入、更新和删除。最后,提到了用户权限设置和数据库的备份恢复方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

忘记MySQL超户密码的解决方法 * MySQL数据库超户root 与centos超户root不是一个用户

登录数据库时出现以下提示时:

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

  意思是 预读这个库中表以及表列信息,一般原因是当库中表很多,表中数据很大时,就会出现执行use <库名>后半天没反应,连接很慢的情况,解决办法就是 -A 方式登录数据库,不会预读库中表信息。

解决办法:[root@ns1 ~]# mysql -uroot -p -A

1】先暂停mysql服务

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

Shutting down MySQL.                                       [确定]

2】跳过grant表授权,计入安全模式,并在后台运行。

[root@ns1 ~]# mysqld_safe --skip-grant-tables & //skip(跳过)grant(授权)&后台

[root@ns1 ~]# jobs    //查询后台运行程序

  1. +  Running                 mysqld_safe --skip-grant-tables &

3】进入安全模式修改密码

[root@ns1 ~]# mysql    //登录数据库

...............................

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>  use mysql;  //进入MySQL数据库

Database changed

mysql>  update user set Password=password('123321') where user='root';

//update(修改数据)set(查看)password('123321') 调取函数必须添加() where(查询定位用户)

Query OK, 4 rows affected (0.01 sec)

Rows matched: 4  Changed: 4  Warnings: 0

mysql> flush privileges;   // flush(刷新) privileges(权限)

Query OK, 0 rows affected (0.00 sec)

4】重启MySQL服务用新密码登录。

[root@ns1 ~]# /etc/init.d/mysqld restart   //重启MySQL服务

Shutting down MySQL.190509 06:20:15 mysqld_safe mysqld from pid file /usr/local/mysql/data/ns1.kgc.cn.pid ended

                                                           [确定]

Starting MySQL..                                           [确定]

  1. +  Done                    mysqld_safe --skip-grant-tables

[root@ns1 ~]# mysql -uroot -p123321   //新密码登录

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

Your MySQL connection id is 1

...................................

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

如何在MySQL数据库中开启使用tab键补全命令功能

1】修改配置文件/etc/my.cnf

vim /etc/my.cnf

132 [mysql]

133 #no-auto-rehash

134 auto-rehash

重启MySQL服务

/etc/init.d/mysqld restart

使MySQL数据库支持简体中文

1】临时修改

mysql> charset utf8;

Charset changed

2】永久支持简体中文 (yum方式安装的MySQL服务)

编辑配置文件/etc/my.cnf

[client]

Default-character-set=utf8

[mysql]

Default-character-set=utf8

重启MySQL服务生效

MySQL数据库表的管理操作

*大部分命令不区分大小写

1】查看数据库结构 查看数据库列表信息

格式:show databases

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

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

4 rows in set (0.01 sec)

查看数据库中的数据表信息

Use+数据库名   查看表:show tables

mysql> show tables;   

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

| Tables_in_mysql           |

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

| columns_priv              |

| db                        |

| time_zone_transition_type |

| user                      |

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

24 rows in set (0.00 sec)

显示数据表的结构 格式:describe +[数据库.]表名

mysql> describe mysql.user;  //在库外查看表结构

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

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

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

| Host                   | char(60)                          | NO   | PRI |         |       |

| plugin                 | char(64)                          | YES  |     |         |       |

| authentication_string  | text                              | YES  |     | NULL    |       |

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

42 rows in set (0.01 sec)

mysql> use mysql;   //进入库

Database changed

mysql> describe user;   //查看表结构

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

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

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

| Host                   | char(60)                          | NO   | PRI |         |       |
| plugin                 | char(64)                          | YES  |     |         |       |

| authentication_string  | text                              | YES  |     | NULL    |       |

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

Creat创建新库、创建新表

格式:create database +数据库名;   create table +表名

mysql> create database 中国;   //创建一个名为“中国”的新表

Query OK, 1 row affected (0.00 sec)

mysql> use 中国;    //进入这个库使用它

Database changed

mysql> create table 河北 (省会 char(4) not null, 其他城市 char(5) not null, 区号 char(10) not null, primary key(区号));

Query OK, 0 rows affected (0.02 sec)     //在“中国”库中 创建名为“河北”的表,表内三个字段(省会最多4个字节 不能为空 其他城市最多5个字节 区号最多10 个字节 索引关键字区号)

Insert插入新数据

格式 insert into 表名(字段1,字段2,.....) values(‘字段1,字段2的值........’)

mysql> use 中国;    //进入这个库

Database changed 

mysql> desc 河北;   //查看这个表

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

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

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

| 省会         | char(4)  | NO   |     | NULL    |       |

| 其他城市     | char(5)  | NO   |     | NULL    |       |

| 区号         | char(10) | NO   | PRI | NULL    |       |

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

3 rows in set (0.01 sec)

 

mysql>  insert into 河北(省会,其他城市,区号) values('石家庄','张家口','0313');  //(省会,其他城市,区号)字段内容可以省略

Query OK, 1 row affected (0.01 sec)

mysql>  select * from 河北;     //查看表详细信息

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

| 省会      | 其他城市     | 区号   |

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

| 石家庄    | 张家口       | 0313   |

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

1 row in set (0.00 sec)

Drop删除库、删除表 格式:drop table [数据库名.]表名;  drop databases 数据库名;

mysql> drop table auth.haha;  //删除哈哈这个表

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;  

Empty set (0.00 sec)

mysql> drop database auth;  //删除auth 这个库

Query OK, 0 rows affected (0.00 sec)

mysql> show databases;

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

| Database           |

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

| information_schema |

| 中国               |

| mysql              |

| performance_schema |

| test               |

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

5 rows in set (0.00 sec)

Updata 更改原有数据

格式:update 表名 set 字段 1=值 1 [,字段 2=值 2] where 条件表达式;

mysql> update 中国.河北 set 区号=('') where 其他城市='张家口';   //将张家口的区号删除

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> select * from 河北;

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

| 省会      | 其他城市     | 区号   |

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

| 石家庄    | 张家口       |        |

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

1 row in set (0.00 sec)

mysql> update mysql.user set password=password('123123') where user='root';   //修改MySQL登录root用户密码为123123

Query OK, 4 rows affected (0.00 sec)

Rows matched: 4  Changed: 4  Warnings: 0

Delete删除不需要的数据   格式:delete from 表名 where 条件表达式;

mysql> delete from 中国.河北 where 其他城市='张家口';    //删除中国库中7的河北表中的其他城市为张家口的记录

Query OK, 1 row affected (0.01 sec)

 

mysql> select * from 河北;

Empty set (0.00 sec)

Select 查询语句  格式:select 字段名 1 ,字段名2 ,.........;   vselect 字段名 1 ,字段名2 ,.........;where 条件表达式;

mysql> select * from 中国.各省;  //查看表中的所有内容

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

| 省份      | 省会         | 简称   |

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

| 河北      | 石家庄       | 冀     |

| 山东      | 济南         | 鲁     |

| 河南      | 郑州         | 豫     |

| 辽宁      | 沈阳         | 辽     |

| 内蒙古    | 呼和浩特     | 蒙     |

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

5 rows in set (0.00 sec)

 

mysql> select 省份 from 中国.各省;  //查看表中省份这一列内容

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

| 省份      |

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

| 河北      |

| 山东      |

| 河南      |

| 辽宁      |

| 内蒙古    |

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

5 rows in set (0.00 sec)

mysql> mysql> select 简称 from 中国.各省;     //查看表中简称这一列内容

 

+--------+

| 简称   |

+--------+

| 冀     |

| 鲁     |

| 豫     |

| 辽     |

| 蒙     |

+--------+

5 rows in set (0.00 sec)

mysql> select * from 中国.各省 where 省份='河北';    //查看表中河北的全部内容

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

| 省份   | 省会      | 简称   |

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

| 河北   | 石家庄    | 冀     |

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

1 row in set (0.00 sec)

用户权限设置grant

连接登录到MySQL操作环境

MySQL -u 指定用户名

-p指定密码

-h指定主机

-P指定端口

1】设置用户权限 (用户不存在则新建用户)

MySQL通配符:

_:任意单个字符

%:

[root@ns2 ~]# mysql -uroot -p123321 -h 192.168.1.11   //指定机器登录本机MySQL

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

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

刷新“flush privileges”

 mysql> select * from user\G  //查看所有数据库用户的权限

mysql> revoke select on imployee_salary.* from 'amber'@'localhost';    //撤销用户的权限

Query OK, 0 rows affected (0.00 sec)

数据库的备份与恢复

方法1:可直接备份目录 /var/local/mysql/var

方法2:使用专用备份工具 MySQLdump

备份操作

MySQLdump -u 用户名 -p [密码]  [选项]  [数据库名]  [表名] > /备份路径/备份文件名

常见选项  --all-databases  //备份所有的数据库   --opt  //加快备份速度

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值