mysql 基本操作

这篇博客详细介绍了MySQL的基本操作,包括更改密码、连接数据库、常用命令、创建用户与授权、SQL语句以及数据库的备份与恢复。内容涵盖多种密码更改方法、数据库连接方式、查看系统信息及时间、用户权限设置等,是MySQL入门学习的良好参考资料。
摘要由CSDN通过智能技术生成

更改MySQL的密码

① 更改root密码
/usr/local/mysql/bin/mysql -uroot
②修改密码
方法一(内):
set password for 用户名@localhost = password('新密码’);

MySQL [(none)]> set password for root@localhost = password('123456');

Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> Ctrl-C – exit!
Aborted

[root@localhost ~]# mysql -uroot -p123456

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.47 MySQL Community Server (GPL)
Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MySQL [(none)]>
方法二(外):
mysqladmin -u用户名 -p旧密码 password 新密码

[root@localhost ~]# mysqladmin -uroot -p123456 password 000000
[root@localhost ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.47 MySQL Community Server (GPL)
Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MySQL [(none)]>

③密码重置

[root@localhost ~]# vi /etc/my.cnf

增加[mysqld] 下 skip-grant

[root@localhost ~]# service mysqld restart

Shutting down MySQL… SUCCESS!
Starting MySQL. SUCCESS!

[root@localhost ~]# mysql -uroot

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.47 MySQL Community Server (GPL)

Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]> 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 [mysql]> update user set password=password('000000') where user='root';

Query OK, 3 rows affected (0.00 sec)
Rows matched: 4 Changed: 3 Warnings: 0

MySQL [mysql]> Ctrl-C – exit!
Aborted

[root@localhost ~]# vi /etc/my.cnf

删掉[mysqld]下的skip-grant

[root@localhost ~]# service mysqld restart

Shutting down MySQL… SUCCESS!
Starting MySQL. SUCCESS!

[root@localhost ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.47 MySQL Community Server (GPL)

Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]>

连接MySQL

本地登录

mysql -uroot -p123456

远程登录

 mysql -uroot -p123456 -h127.0.0.1 -P3306

指定到/tmp/mysql.sock目录下

 mysql -uroot -p123456 -S/tmp/mysql.sock

不进入数据库,执行命令

 mysql -uroot -p123456 -e “show databases”

MySQL常用命令

查看当前数据库版本

MySQL [(none)]> select version();

±----------+
| version() |
±----------+
| 5.6.47 |
±----------+
1 row in set (0.00 sec)

查看数据库状态

MySQL [(none)]> show status;

查看当前系统时间

MySQL [(none)]> select sysdate();

±--------------------+
| sysdate() |
±--------------------+
| 2020-11-10 02:51:52 |
±--------------------+
1 row in set (0.00 sec)
查看当前用户

MySQL [(none)]> select user();

±---------------+
| user() |
±---------------+
| root@localhost |
±---------------+
1 row in set (0.00 sec)
查看当前日期

MySQL [(none)]> select current_date();

±---------------+
| current_date() |
±---------------+
| 2020-11-10 |
±---------------+
1 row in set (0.00 sec)
查看当前时间

MySQL [(none)]> select current_time();

±---------------+
| current_time() |
±---------------+
| 02:52:21 |
±---------------+
1 row in set (0.00 sec)
查看队列

MySQL [(none)]> show processlist;
MySQL [(none)]> show full processlist;

查询库

MySQL [(none)]> show databases;

±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
4 rows in set (0.00 sec)
切换库

MySQL [(none)]> 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 [mysql]> show tables;

±--------------------------+
| Tables_in_mysql |
±--------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
±--------------------------+
28 rows in set (0.00 sec)

查看表里的字段

MySQL [mysql]> desc tb_name;

查看建表语句

MySQL [mysql]> show create table tb_name\G;

查看当前使用的数据库

MySQL [mysql]>  select database(); 

±-----------+
| database() |
±-----------+
| mysql |
±-----------+
1 row in set (0.00 sec)

创建库

MySQL [xiyue]> create database xiyue;

创建表

MySQL [xiyue]> use xiyue;
MySQL [xiyue]>  create table t1(`id` int(4), `name` char(40));

查看各参数

show variables; (有点儿多)
MySQL [xiyue]> show variables like 'max_connect%';

±-------------------±------+
| Variable_name | Value |
±-------------------±------+
| max_connect_errors | 100 |
| max_connections | 151 |
±-------------------±------+
2 rows in set (0.00 sec)

修改参数

MySQL [xiyue]> set global max_connect_errors=1000;

Query OK, 0 rows affected (0.00 sec)

MySQL [xiyue]> show variables like 'max_connect%';

±-------------------±------+
| Variable_name | Value |
±-------------------±------+
| max_connect_errors | 1000 |
| max_connections | 151 |
±-------------------±------+
2 rows in set (0.00 sec)

MySQL创建用户以及授权

MySQL [(none)]> grant all privileges on *.* to 'user1'@'localhost' identified by '000000';

Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> grant SELECT,UPDATE,INSERT on xiyue.* to 'user2'@'192.168.133.1' identified by '000000';

Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]>  grant all privileges on xiyue.* to 'user3'@'%' identified by '000000';

Query OK, 0 rows affected (0.00 sec)

列出权限
MySQL [(none)]> show grants;

±---------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
±---------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON . TO ‘root’@‘localhost’ IDENTIFIED BY PASSWORD ‘*032197AE5731D4664921A6CCAC7CFCE6A0698693’ WITH GRANT OPTION |
| GRANT PROXY ON ‘’@’’ TO ‘root’@‘localhost’ WITH GRANT OPTION |
±---------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

查看用户权限
show grants for 你的用户
MySQL [(none)]> show grants for user2@192.168.133.1;

±-----------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.133.1 |
±-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON . TO ‘user2’@‘192.168.133.1’ IDENTIFIED BY PASSWORD '032197AE5731D4664921A6CCAC7CFCE6A0698693’ |
| GRANT SELECT, INSERT, UPDATE ON xiyue.
TO ‘user2’@‘192.168.133.1’ |
±-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

撤销权限
revoke select  on wordpress.* from 'user'@localhost identified by '123456';    

常用SQL语句

查询user表的行数
MySQL [(none)]> select count(*) from mysql.user;

±---------+
| count(*) |
±---------+
| 9 |
±---------+
1 row in set (0.00 sec)

select * from mysql.db;
 例:MySQL [(none)]> select * from xiyue.t1;

±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
±-----±-----+
1 row in set (0.01 sec)

MySQL [(none)]> select db from mysql.db;

±--------+
| db |
±--------+
| test |
| test_% |
| xiyue |
| xiyue |
±--------+
4 rows in set (0.00 sec)

 MySQL [(none)]> select db,user from mysql.db;

±--------±------+
| db | user |
±--------±------+
| test | |
| test_% | |
| xiyue | user3 |
| xiyue | user2 |
±--------±------+
4 rows in set (0.00 sec)

 select * from mysql.db where host like '192.168.%';

使用表

MySQL [(none)]> use xiyue;

插入表的内容

 MySQL [xiyue]> insert into xiyue.t1 values (1, 'abc');

Query OK, 1 row affected (0.02 sec)
修改表的内容

MySQL [xiyue]> update xiyue.t1 set name='aaa' where id=1; 

Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

清空表内容

MySQL [xiyue]> truncate table xiyue .t1;

查询一下(空的)

MySQL [xiyue]> select * from xiyue.t1;
Empty set (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

删除表

MySQL [xiyue]>  drop table xiyue.t1;

Query OK, 0 rows affected (0.02 sec)
删除数据库

MySQL [xiyue]>  drop database xiyue;
Query OK, 0 rows affected (0.01 sec)
MySQL [(none)]>  show databases;

±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
4 rows in set (0.00 sec)

MySQL数据库的备份与恢复

备份库

[root@localhost ~]# mysqldump -uroot -p000000 mysql > /tmp/mysql.sql
[root@localhost ~]# ll /tmp
total 688
-rwx------. 1 root root    827 Nov  9 22:48 ks-script-P6U_wl
-rw-r--r--. 1 root root 699534 Nov 10 05:26 mysql.sql
-rw-------. 1 root root      0 Nov  9 22:42 yum.log
[root@localhost ~]# cd /tmp
[root@localhost tmp]# du -h mysql.sql
684K    mysql.sql
**创建库**

[root@localhost tmp]# mysql -uroot -p000000 -e "create database mysqltest;"
MySQL [mysqltest]> show tables;

备份表

 [root@localhost ~]# mysqldump -uroot -p000000 mysqltest user > /tmp/user.sql
 [root@localhost ~]# mysql -uroot -p000000 -e "create database mysqltestaaa;"
[root@localhost ~]# mysqldump -uroot -p000000 mysqltestaaa < /tmp/user.sql

–MySQL dump 10.14 Distrib 5.5.68-MariaDB, for Linux (x86_64)

– Host: localhost Database: mysqltestaaa


– Server version 5.6.47

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/
!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/
!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/
!40101 SET NAMES utf8 /;
/
!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE /;
/
!40103 SET TIME_ZONE=’+00:00’ /;
/
!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 /;
/
!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 /;
/
!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO’ /;
/
!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 /;
/
!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE /;
/
!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS /;
/
!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS /;
/
!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/
!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/
!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION /;
/
!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

– Dump completed on 2020-11-10 5:45:48

恢复表

 [root@localhost ~]#  mysql -uroot -p000000 mysqltest < /tmp/user.sql

备份所有库

[root@localhost ~]# cd /tmp
[root@localhost tmp]# mysqldump -uroot -p000000 -A >/tmp/123.sql
[root@localhost tmp]# du -h 123.sql
1.4M    123.sql

只备份表结构

[root@localhost tmp]# mysqldump -uroot -p000000 -d mysql > /tmp/xxx.sql
[root@localhost tmp]# mysql -uroot -p000000 -e "create database xxx;"
[root@localhost tmp]# mysql -uroot -p000000 xxx < /tmp/xxx.sql
[root@localhost tmp]# mysql -uroot -p000000

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.6.47 MySQL Community Server (GPL)

Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]> use xxx

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 [xxx]> show tables;

±--------------------------+
| Tables_in_xxx |
±--------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
±--------------------------+
28 rows in set (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值