linux下Mysql的简单操作

linux下Mysql的简单操作linux下Mysql的简单操作

大家好,又见面了,我是你们的朋友全栈君。

更改mysql数据库root的密码

首次进入数据库是不用密码的:

[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.40-log MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

退出的话,直接输入quit或者exit即可。细心的读者也许会发现,在上一条命令中,使用的是绝对路径,这样不方便,但是单独只是输入一个 “mysql” 命令是不行的,因为 “/usr/local/mysql/bin” 没有在 PATH 这个环境变量里。如何把它加入环境变量PATH中?之前阿铭介绍过:

[root@localhost ~]# PATH=$PATH:/usr/local/mysql/bin
这样就可以了,但重启Linux后还会失效,所以需要让它开机加载:
[root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.40-log MySQL Community Server (GPL)

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

mysql>

解释一下上一条命令 -u 的含义,它用来指定要登录的用户,后边可以有空格,也可以无空格,root用户是mysql自带的管理员账户,默认没有密码的,那么如何给root用户设定密码?按如下操作:

[root@localhost ~]# mysqladmin -uroot password 'yourpassword'

这样就设置了 ‘root’ 账号的密码了,不妨再来用上面的命令登陆一下试试看:

[root@localhost ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:NO)

报错了,这是在提示我们,root账号是需要密码登陆的。

[root@localhost ~]# mysql -uroot -p'yourpassword'
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.40-log MySQL Community Server (GPL)

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

mysql>

需要加一个 -p 选项,它后面可以直接跟密码,后面不可以有空格,不过密码最好用单引号括起来,不括也可以,但是密码中如果有特殊字符就会有问题了,所以最好是括起来吧。当然, -p后面也是可以不加密码,而是和用户交互的方式,让我们输入密码:

[root@localhost ~]# mysql -uroot -p
Enter password:

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

[root@localhost ~]# 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               |
+--------------------+
3 rows in set (0.06 sec)

mysql的命令,结尾处需要加一个分号。
2. 查询某个库的表
首先需要切换到某个库里去:

mysql> use mysql;
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.06 sec)
  1. 查看某个表的全部字段
mysql> desc slow_log;
+----------------+------------------+------+-----+-------------------+-----------------------------+
| Field          | Type             | Null | Key | Default           | Extra                       |
+----------------+------------------+------+-----+-------------------+-----------------------------+
| start_time     | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_host      | mediumtext       | NO   |     | NULL              |                             |
| query_time     | time             | NO   |     | NULL              |                             |
| lock_time      | time             | NO   |     | NULL              |                             |
| rows_sent      | int(11)          | NO   |     | NULL              |                             |
| rows_examined  | int(11)          | NO   |     | NULL              |                             |
| db             | varchar(512)     | NO   |     | NULL              |                             |
| last_insert_id | int(11)          | NO   |     | NULL              |                             |
| insert_id      | int(11)          | NO   |     | NULL              |                             |
| server_id      | int(10) unsigned | NO   |     | NULL              |                             |
| sql_text       | mediumtext       | NO   |     | NULL              |                             |
+----------------+------------------+------+-----+-------------------+-----------------------------+
11 rows in set (0.04 sec)

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

mysql> show create table slow_log\G;
*************************** 1. row ***************************
       Table: slow_log
Create Table: CREATE TABLE `slow_log` (
  `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP,
  `user_host` mediumtext NOT NULL,
  `query_time` time NOT NULL,
  `lock_time` time NOT NULL,
  `rows_sent` int(11) NOT NULL,
  `rows_examined` int(11) NOT NULL,
  `db` varchar(512) NOT NULL,
  `last_insert_id` int(11) NOT NULL,
  `insert_id` int(11) NOT NULL,
  `server_id` int(10) unsigned NOT NULL,
  `sql_text` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'
1 row in set (0.01 sec)
  1. 查看当前是哪个用户
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
  1. 查看当前所使用数据库
mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.01 sec)
  1. 创建一个新库
mysql> create database db1;
Query OK, 1 row affected (0.05 sec)
  1. 创建一个新表
mysql> use db1;
Database changed
mysql> create table t1 (`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.02 sec)

要注意的是,字段名需要用反引号括起来。

  1. 查看当前数据库版本
mysql> select version();
+------------+
| version()  |
+------------+
| 5.1.40-log |
+------------+
1 row in set (0.01 sec)
  1. 查看当前mysql状态
mysql> show status;
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| Aborted_clients                   | 0        |
| Aborted_connects                  | 5        |
| Binlog_cache_disk_use             | 0        |
| Binlog_cache_use                  | 0        |
| Bytes_received                    | 303      |
| Bytes_sent                        | 7001     |

由于内容太长,阿铭没有全部列出来,如果有兴趣可以网上找资料查一下每一行的含义。

  1. 查看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/   |

限于篇幅,阿铭省略了很多参数没有显示,其中很多参数都是可以在/etc/my.cnf中定义的,并且有部分参数是可以在线编辑的。

  1. 修改mysql的参数
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 10    |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

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

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

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

  1. 查看当前mysql服务器的队列
    这个在日常的管理工作中使用最为频繁,因为使用它可以查看当前mysql在干什么,可以发现是否有锁表:
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 13 | root | localhost | db1  | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.01 sec)
  1. 创建一个普通用户并授权
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)

一些常用的sql

  1. 查询语句
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|        8 |
+----------+
1 row in set (0.00 sec)

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

mysql> select * from mysql.db;

这个用来表示查询mysql库的db表中的所有数据,也可以查询单个字段或者多个字段:

mysql> select db from mysql.db;
mysql> select db,user  from mysql.db;

同样,在查询语句中可以使用万能匹配 “%”
mysql> select * from mysql.db where host like ‘10.0.%’;

  1. 插入一行
mysql> insert into db1.t1 values (1, 'abc');
Query OK, 1 row affected (0.02 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)
  1. 更改表的某一行
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)
  1. 清空表数据
mysql> truncate table db1.t1;
Query OK, 0 rows affected (0.01 sec)

mysql> select count(*) from db1.t1;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)
  1. 删除表
mysql> drop table db1.t1;
Query OK, 0 rows affected (0.00 sec)
  1. 删除数据库
mysql> drop database db1;
Query OK, 0 rows affected (0.02 sec)

mysql数据库的备份与恢复
备份:

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

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

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

退出mysql命令

mysql> quit
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106550.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python监控网页变化教程_Python实时监控网站浏览记录实现过程详解

    python监控网页变化教程_Python实时监控网站浏览记录实现过程详解需求:(1)获取你对象chrome前一天的浏览记录中的所有网址(url)和访问时间,并存在一个txt文件中(2)将这个txt文件发送给指定的邮箱地址(你的邮箱)(3)建立例行任务,每天定时自动完成这些操作,你就可以通过邮件查看你对象每天看啥了准备macOSSierraPython3.6Chrome发送邮件的qq邮箱地址qq邮箱授权码SMTP服务器地址:smtp.qq.com接受邮件的邮箱地…

    2022年7月16日
    18
  • 运行时错误10048,地址已在使用_无法指定被请求的地址

    运行时错误10048,地址已在使用_无法指定被请求的地址OSError:[WinError10049]是由于ip地址为空造成的设置端口验证的一行manager=QueueManager(address=(”,5000),authkey=b’abc’)中的地址添加127.0.0.1问题解决。

    2022年9月30日
    2
  • VS code安装和使用技巧

    VS code安装和使用技巧VSCode是微软提供的一款轻量级但功能十分强大的编辑器,内置了对JavaScript,TypeScript和Node.js语言的支持,并且为其他语言如C++,C#,Python,PHP等提供了丰富的扩展库和运行时。一:VSCode的安装(去下载),1.1:VSCode的当前版本为1.18,支持Windows,Ubuntu,Mac1.2:安装VS

    2022年5月22日
    90
  • iostat命令详解参数_iostat命令的dm

    iostat命令详解参数_iostat命令的dmiostat命令详解iostat用于输出CPU和磁盘输入输出、分区、网络文件系统NFS相关的统计信息。iostat命令用于监控系统设备的输入/输出情况,并生成报告,以便根据统计报告修改系统配置,获取更优的性能。命令格式:iostat[-c][-d][-N][-n][-h][-k|-m][-t][-V][-x][-z][device

    2022年10月6日
    4
  • mysql怎么加载数据库_如何导入mysql数据库

    mysql怎么加载数据库_如何导入mysql数据库展开全部方法一:1、首先我e68a84e8a2ad3231313335323631343130323136353331333363393134们使用MySQL提供的命令行界面来导入数据库,确保自己的电脑中安装了MySQL数据库,我们可以通过命令行来确认是否安装了MySQL数据库,当然,第一步是打开Mysql的数据库服务,我们使用命令行来打开,2、启动MySQL后,我们找到需要用到的脚本文件,也就是…

    2022年7月27日
    9
  • html5 sexteen,Teen guilty of rape and murder

    html5 sexteen,Teen guilty of rape and murderMANITOWOC,Wis.-Ajuryconvicteda17-year-oldboyWednesdayofrapingaphotographerandhelpinghisuncle,whosepreviousbrusheswiththelawbroughtattentiontothecase,killherandburnher…

    2022年5月10日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号