学习笔记mysql常用基本操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习笔记mysql常用基本操作相关的知识,希望对你有一定的参考价值。

mysql数据库root账号设定密码
[[email protected] ~]# mysqladmin -u root password ‘123456‘

连接数据库:
可以使用命令:mysql –u root –p连接数据库,但只是连接本地库,很多时间需要连接网络中的某台主机的数据库:
备注:-u 账号 -p 密码 -h 指定远程主机的ip
连接本地库:[[email protected] ~]# mysql -u root -p
Enter password:
连接网络中的库:[[email protected] ~]# mysql -uroot -p -h192.168.1.1 -P3306
Enter password:
-P:用来指定远程主机mysql的绑定端口,默认是3306

查询当前库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)

查询某个库中的表:
首先要切换到某个库中:
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
-A可以忽略在切换库时读取所有表的字段
mysql> use mysql -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 |
+---------------------------+
23 rows in set (0.00 sec)

查看表的全部字段:
mysql> desc db;

\G:以行的形式显示:
mysql> desc db\G;
1. row

查看当前是哪个用户
mysql> select user();
+----------------+
| user() |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)

查看当前数据库的命令:
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)

创建一个新库:
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
查看刚才创建的新库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)

查看当前数据库的版本:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.73 |
+-----------+
1 row in set (0.00 sec)

查看MySQL的当前状态:
mysql> show status;
+-----------------------------------+----------+
| Variable_name | Value |
+-----------------------------------+----------+
| Aborted_clients | 0 |
| Aborted_connects | 3 |

查看MySQL的参数:
mysql> show variables;
查看当前mysql服务器的队列:
mysql> show processlist;
+----+------+-----------+-------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+-------+---------+------+-------+------------------+
| 11 | root | localhost | mysql | Query | 0 | NULL | show processlist |
+----+------+-----------+-------+---------+------+-------+------------------+
1 row in set (0.00 sec)

创建一个新表:
mysql> use db1; #切换到库里面先
Database changed
mysql> create table test(id int(20),name char(40));
Query OK, 0 rows affected (0.03 sec)
查看刚才创建的表:
mysql> desc test;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | char(40) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

插入数据:
mysql> insert into test values (1,‘le‘);
Query OK, 1 row affected (0.00 sec)
查看表中的数据:
mysql> select * from test;
+------+------+
| id | name |
+------+------+
| 1 | le |
+------+------+
1 row in set (0.00 sec)

清空某个表中的数据:
mysql> truncate table test;
Query OK, 0 rows affected (0.00 sec)

查看表中的数据:
mysql> select * from test;
Empty set (0.00 sec)

创建一个普通用户并授权
mysql> grant all on . to user2 identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
All:表示有全部的权限
.:前者表示所有库,后者表示所有表
给网络的主机某个用户授权:
mysql> grant all on db1.
to ‘user2‘@‘192.168.159.136‘ identified by ‘1234567’;
Query OK, 0 rows affected (0.00 sec)

查询语句:
第一种方法:
mysql> select count() from mysql.user;
+----------+
| count(
) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
Mysql.user表示MySQL库中的表,count(*)表示表中有多少行

第二种方法:
mysql> select db from mysql.db;
+---------+
| db |
+---------+
| test |
| test_% |
| db1 |
+---------+
3 rows in set (0.00 sec)

小弟是初学者,有错的地方希望大佬们点出来!

以上是关于学习笔记mysql常用基本操作的主要内容,如果未能解决你的问题,请参考以下文章

mysql学习笔记--- 基本的SQL语句

2018-03-26MySQL启动关闭常用命令学习笔记

MySql学习笔记之DQL常用查询

MySQL学习笔记MySQL的常用命令

mysql学习笔记

MySQL学习笔记——计算字段及常用函数