• 基于Centos7.6

1.修改密码

1
2
3
4
5
6
方法1:
[root@localhost ~]# mysqladmin -uroot -p password 新密码
Enter password:当前密码
方法2:
mysql> SET password=PASSWORD('新密码');
mysql> flush privileges;

2.指定IP访问数据库

1
2
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION; //任何远程主机都可以访问数据库
FLUSH PRIVILEGES;

3.查看连接线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
processlist命令的输出结果显示了有哪些线程在运行,不仅可以查看当前所有的连接数,还可以查看当前的连接状态帮助识别出有问题的查询语句等。
mysql> show processlist;
+----+------+-----------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+----------+------------------+
| 1 | root | localhost | NULL | Sleep | 81 | | NULL |
| 2 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+------+-----------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec)

各个列的含义:
1>id列,用户登录mysql时,系统分配的"connection_id",可以使用函数connection_id()查看
2>user列,显示当前用户。如果不是root,这个命令就只显示用户权限范围的sql语句
3>host列,显示这个语句是从哪个ip的哪个端口上发的,可以用来跟踪出现问题语句的用户
4>db列,显示这个进程目前连接的是哪个数据库
5>command列,显示当前连接的执行的命令,一般取值为休眠(sleep),查询(query),连接(connect)等
6>time列,显示这个状态持续的时间,单位是秒
7>state列,显示使用当前连接的sql语句的状态,很重要的列。state描述的是语句执行中的某一个状态。一个sql语句,以查询为例,可能需要经过copying to tmp table、sorting result、sending data等状态才可以完成
8>info列,显示这个sql语句,是判断问题语句的一个重要依据

4.查看MySQL版本

1
2
3
4
5
6
7
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.28 |
+-----------+
1 row in set (0.00 sec)

5.创建数据库实例

1
create database 数据库名称 charset utf8mb4;

6.查看数据库监听端口

1
netstat -antp|grep mysql|grep LISTEN

7.查看用户信息

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| app | 10.0.0.% |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.01 sec)

mysql>
#可以查看用户和允许连接的网段

8.指定用户从特定网段访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
mysql> grant all on *.* to test@'192.168.3.%' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select user,host from mysql.user;
+---------------+-------------+
| user | host |
+---------------+-------------+
| app | 10.0.0.% |
| test | 192.168.3.% |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-------------+
5 rows in set (0.00 sec)

mysql>exit
Bye
[root@localhost ~]# mysql -utest -p -h 192.168.3.187 -P3306
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

9.创建用户

1
2
create user testuser@localhost identified by 'testuser';
flush privileges;

10.查看表总行数

1
select count(*) from table_name;

11.查看表的前几行数据

1
select * from table_name limit 10;			#查看前10行数据