【无标题】

2024-01-10 11:59:22
yum -y install mysql-server mysql
systemctl start mysqld.service
ps -C mysqld
ss -ntulp | grep 3306
systemctl status mysqld.service
systemctl enable mysqld
# 主配置文件
[root@mysql50 ~]# cat /etc/my.cnf.d/mysql-server.cnf 
[mysqld]                                #MySQL服务进程
datadir=/var/lib/mysql                  #数据文件的存储目录(数据库目录)
socket=/var/lib/mysql/mysql.sock        #本地连接MySQL服务器需要sock文件
log-error=/var/log/mysql/mysqld.log     #错误日志文件位置
pid-file=/run/mysqld/mysqld.pid         #服务进程ID文件位置

# 端口号
3306

# 传输协议
TCP

# 进程所有者、所属组
mysql 

命令格式

mysql -h -P -u -p

选项:

-h 数据库服务器ip地址

-P?数据库服务端口

-u 用户名

-p?密码

基本操作

mysql> select version();? ? ? ? #查看服务软件版本
mysql> select user();? ? ? ? ? ? ?#查看登陆用户
mysql> show databases;? ? ? ?#查看已有库
mysql> use 库名;? ? ? ? ? ? ? ? ? ?#切换库
mysql> select database();? ? ? #查看当前所在库
mysql> show tables;? ? ? ? ? ? ? ?#查看库里已有表
mysql> exit;? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#断开连接

密码管理

密码的设置与修改

操作系统管理员有此权限

命令格式

设置密码

]# mysqladmin -uroot -hlocalhost -p password "新密码"(端口默认3306,-P这里忽略)

修改密码

]# mysqladmin -uroot -hlocalhost -p旧密码 password "新密码"(端口默认3306,-P这里忽略)

隐藏密码修改

]# mysqladmin -uroot -hlocalhost -P3306 -p password

Enter password:? ? ? ? ? ? ? ? ? ? ? ? ?#输入旧密码
New password:? ? ? ? ? ? ? ? ? ? ? ? ? #输入新密码
Confirm new password:? ? ? ? ? ? ?#再次输入新密码

密码破解

配置服务可以无密码登录

[root@mysql50 ~]# vim /etc/my.cnf.d/mysql-server.cnf 
[mysqld]
......
skip-grant-tables    #添加这行(跳过授权表)
......

删除不知道的密码

mysql> desc mysql.user;    #查看表头名
mysql> select host,user,authentication_string from mysql.user;    #查看表头的值
mysql> update mysql.user set authentication_string="" where host="localhost" and user="root";    #删除密码
mysql> select host,user,authentication_string from mysql.user;
mysql> flush privileges;    #刷新权限(确保删除生效)

配置服务需要密码登录

[root@mysql50 ~]# vim /etc/my.cnf.d/mysql-server.cnf 
[mysqld]
......
skip-grant-tables    #删除此行 需要密码登录
......

登录后设置新密码

mysql> alter user root@"localhost" identified by "123456";    #设置新密码

使用新密码登录

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

配置步骤

准备phpmyadmin软件运行环境(运行nginx服务,且配置服务可以解释php脚本)

安装依赖的软件包

[root@mysql50 ~]# yum -y install gcc make pcre-devel 

安装源码的nginx软件

[root@mysql50 ~]# tar -xf nginx-1.22.1.tar.gz
[root@mysql50 ~]# cd nginx-1.22.1/
[root@mysql50 nginx-1.22.1]# ./configure 
配置错误
./configure: error: the HTTP gzip module requires the zlib library.
[root@mysql50 nginx-1.22.1]# yum -y install zlib-devel
[root@mysql50 nginx-1.22.1]# ./configure 
[root@mysql50 nginx-1.22.1]# make && make install
[root@mysql50 nginx-1.22.1]# ls /usr/local/nginx/
conf  html  logs  sbin

修改配置文件实现动静分离

启动nginx服务

[root@mysql50 nginx]# /usr/local/nginx/sbin/nginx -t
[root@mysql50 nginx]# /usr/local/nginx/sbin/nginx 

安装解释php代码的php-fpm服务软件

[root@mysql50 nginx]# yum -y install php php-mysqlnd php-fpm php-devel php-json

修改php-fpm服务软件

[root@mysql50 nginx]# vim /etc/php-fpm.d/www.conf 
;listen = /run/php-fpm/www.sock
listen = 127.0.0.1:9000

启动php-fpm服务软件

[root@mysql50 nginx]# systemctl start php-fpm

编写php代码,测试环境

[root@mysql50 nginx]# vim html/a.php
<?php
$i = 100 ;
echo $i ;
echo "\n" ;
?>
[root@mysql50 nginx]# curl http://localhost/a.php
100

安装phpmyadmin软件

解压软件

[root@mysql50 ~]# yum -y install unzip
[root@mysql50 ~]# unzip phpMyAdmin-5.2.1-all-languages.zip 

把解压目录拷贝到网页目录下

[root@mysql50 ~]# mv phpMyAdmin-5.2.1-all-languages /usr/local/nginx/html/phpmyadmin
[root@mysql50 ~]# cd /usr/local/nginx/html/phpmyadmin/
[root@mysql50 phpmyadmin]# cp config.sample.inc.php config.inc.php

创建软件的主配置文件并定义管理的数据库服务器

[root@mysql50 phpmyadmin]# vim config.inc.php
$cfg['Servers'][$i]['host'] = 'localhost';

客户端打开浏览器访问软件,对数据库服务器做管理

基础查询

[root@mysql50 ~]# mysql -uroot -p123456 < /root/tarena.sql?
mysql> show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql ? ? ? ? ? ? ?|
| performance_schema |
| sys ? ? ? ? ? ? ? ?|
| tarena ? ? ? ? ? ? |
+--------------------+
5 rows in set (0.00 sec)
mysql> use tarena;
mysql> show tables;
+------------------+
| Tables_in_tarena |
+------------------+
| departments ? ? ?|
| employees ? ? ? ?|
| salary ? ? ? ? ? |
| user ? ? ? ? ? ? |
+------------------+
4 rows in set (0.00 sec)

select语法

语法格式1:select 字段列表 from 库名.表名;

语法格式2:select 字段列表 from 库名.表名 where 筛选条件;

mysql> select name from tarena.user;? ? ? ? #查看一个表头

mysql> select name,uid from tarena.user;? ? ? ? #查看多个表头

mysql> select * from tarena.user;? ? ? ? ? ? ? ? #查看所有表头

加条件查看

mysql> select * from tarena.user where name="root";
mysql> select name from tarena.user where uid=2;

语法规范

必须遵守,命令才会执行

\c终止sql命令

默认命令不支持tab键补全

每条SQL命令以;或\G结束

每条命令可以根据需要进行缩进或换行

SQL命令不区分字母大小写(密码、变量值除外)

筛选条件

数值比较

符号两边必须是数字或数值类型的表头

比较符号说明????????例子
=相等uid=3
>大于uid>3
>=大于或等于uid>=3
<小于uid<3
<=小于或等于uid<=3
!=不相等uid != 3
# 查看第3行的行号、用户名、uid、gid  四个表头的值
mysql> select id,name,uid,gid from user where id=3;
+----+--------+------+------+
| id | name   | uid  | gid  |
+----+--------+------+------+
|  3 | daemon |    2 |    2 |
+----+--------+------+------+
1 row in set (0.00 sec)

# 查看前2行的行号用户名、uid、gid 四个表头的值
mysql> select id,name,uid,gid from user where id<3;
+----+------+------+------+
| id | name | uid  | gid  |
+----+------+------+------+
|  1 | root |    0 |    0 |
|  2 | bin  |    1 |    1 |
+----+------+------+------+
2 rows in set (0.00 sec)

# 查看前3行的行号、用户名、uid、gid  四个表头的值
mysql> select id,name,uid,gid from user where id<=3;
+----+--------+------+------+
| id | name   | uid  | gid  |
+----+--------+------+------+
|  1 | root   |    0 |    0 |
|  2 | bin    |    1 |    1 |
|  3 | daemon |    2 |    2 |
+----+--------+------+------+
3 rows in set (0.00 sec)

# 查看前uid号大于6000的行号、用户名、uid、gid  四个表头的值
mysql> select id,name,uid,gid from user where uid>6000;
+----+-----------+-------+-------+
| id | name      | uid   | gid   |
+----+-----------+-------+-------+
| 22 | nfsnobody | 65534 | 65534 |
+----+-----------+-------+-------+
1 row in set (0.00 sec)

# 查看前uid号大于等于1000的行号、用户名、uid、gid  四个表头的值
mysql> select id,name,uid,gid from user where uid>=1000;
+----+-----------+-------+-------+
| id | name      | uid   | gid   |
+----+-----------+-------+-------+
| 22 | nfsnobody | 65534 | 65534 |
| 24 | plj       |  1000 |  1000 |
+----+-----------+-------+-------+
2 rows in set (0.00 sec)

# 查看uid号和gid号相同的行 仅显示行号、用户名、uid、gid  四个表头的值
mysql> select id,name,uid,gid from user where uid=gid;
+----+-----------------+-------+-------+
| id | name            | uid   | gid   |
+----+-----------------+-------+-------+
|  1 | root            |     0 |     0 |
|  2 | bin             |     1 |     1 |
|  3 | daemon          |     2 |     2 |
| 13 | nobody          |    99 |    99 |
| 14 | systemd-network |   192 |   192 |
| 15 | dbus            |    81 |    81 |
| 17 | sshd            |    74 |    74 |
| 18 | postfix         |    89 |    89 |
| 20 | rpc             |    32 |    32 |
| 21 | rpcuser         |    29 |    29 |
| 22 | nfsnobody       | 65534 | 65534 |
| 23 | haproxy         |   188 |   188 |
| 24 | plj             |  1000 |  1000 |
| 25 | apache          |    48 |    48 |
| 26 | mysql           |    27 |    27 |
+----+-----------------+-------+-------+
15 rows in set (0.00 sec)

# 查看uid号和gid号不一样的行 仅显示行号、用户名、uid、gid  四个表头的值
mysql> select id,name,uid,gid from user where uid != gid;
+----+----------+------+------+
| id | name     | uid  | gid  |
+----+----------+------+------+
|  4 | adm      |    3 |    4 |
|  5 | lp       |    4 |    7 |
|  6 | sync     |    5 |    0 |
|  7 | shutdown |    6 |    0 |
|  8 | halt     |    7 |    0 |
|  9 | mail     |    8 |   12 |
| 10 | operator |   11 |    0 |
| 11 | games    |   12 |  100 |
| 12 | ftp      |   14 |   50 |
| 16 | polkitd  |  999 |  998 |
| 19 | chrony   |  998 |  996 |
+----+----------+------+------+
11 rows in set (0.00 sec)

范围匹配

匹配范围内的任意一个值即可

比较符号说明
in(值列表)在...里
not in(值列表)不在...里
between 数字 and 数字在...和...之间
# uid号表头的值 是 (1 , 3 , 5 , 7) 中的任意一个即可
mysql> select name,uid from user where uid in (1,3,5,7);
+------+------+
| name | uid  |
+------+------+
| bin  |    1 |
| adm  |    3 |
| sync |    5 |
| halt |    7 |
+------+------+
4 rows in set (0.00 sec)

# shell 表头的的值 不是 "/bin/bash"或"/sbin/nologin" 即可
mysql> select name,shell from user where shell not in ("/bin/bash","/sbin/nologin")
    -> ;
+----------+----------------+
| name     | shell          |
+----------+----------------+
| sync     | /bin/sync      |
| shutdown | /sbin/shutdown |
| halt     | /sbin/halt     |
| mysql    | /bin/false     |
+----------+----------------+
4 rows in set (0.00 sec)

# id表头的值 在 10  到  20 之间即可 包括 10 和  20  本身
mysql> select id,name,uid from user where id between 10 and 20;
+----+-----------------+------+
| id | name            | uid  |
+----+-----------------+------+
| 10 | operator        |   11 |
| 11 | games           |   12 |
| 12 | ftp             |   14 |
| 13 | nobody          |   99 |
| 14 | systemd-network |  192 |
| 15 | dbus            |   81 |
| 16 | polkitd         |  999 |
| 17 | sshd            |   74 |
| 18 | postfix         |   89 |
| 19 | chrony          |  998 |
| 20 | rpc             |   32 |
+----+-----------------+------+
11 rows in set (0.00 sec)

模糊匹配

用法

where 字段名 like "通配符";

_ 表示1个字符

% 表示0到多个字符

# 找名字必须是3个字符的(没有空格挨着敲)
mysql> select name from user where name like "___";
+------+
| name |
+------+
| bin  |
| adm  |
| ftp  |
| rpc  |
| plj  |
| bob  |
+------+
6 rows in set (0.00 sec)

# 找名字必须是4个字符的(没有空格挨着敲)
mysql> select name from user where name like "____";
+------+
| name |
+------+
| root |
| sync |
| halt |
| mail |
| dbus |
| sshd |
+------+
6 rows in set (0.00 sec)

# 找名字以字母a开头的(没有空格挨着敲)
mysql> select name from user where name like "a%"
    -> ;
+--------+
| name   |
+--------+
| adm    |
| apache |
+--------+
2 rows in set (0.00 sec)

# 查找名字至少是4个字符的表达式
mysql> select name from user where name like "%____%";
mysql> select name from user where name like "__%__";
mysql> select name from user where name like "____%";
mysql> select name from user where name like "%____";

正则匹配

用法:

where 字段名 regexp "正则表达式";

元字符:

^ 行首

$ 行尾

. 1个字符

[ ] 范围内

* 前边表达式出现零次或多次

| 或

# 插入数据
mysql> insert into user(name) values("yaya");
mysql> insert into user(name) values("yaya1");
mysql> insert into user(name) values("6yaya");

# 查看名字里有数字的
mysql> select name from user where name regexp "[0-9]";
+-------+
| name  |
+-------+
| yaya1 |
| 6yaya |
+-------+
2 rows in set (0.00 sec)

# 查看名字以数字开头
mysql> select name from user where name regexp "^[0-9]";
+-------+
| name  |
+-------+
| 6yaya |
+-------+
1 row in set (0.00 sec)

# 查看名字以数字结尾
mysql> select name from user where name regexp "[0-9]$";
+-------+
| name  |
+-------+
| yaya1 |
+-------+
1 row in set (0.00 sec)

# 查看名字以r开头
mysql> select name from user where name regexp "^r";
+---------+
| name    |
+---------+
| root    |
| rpc     |
| rpcuser |
+---------+
3 rows in set (0.00 sec)

# 查看名字以t结尾
mysql> select name from user where name regexp "t$";
+------+
| name |
+------+
| root |
| halt |
+------+
2 rows in set (0.00 sec)

# 查看名字以r开头或t结尾
mysql> select name from user where name regexp "^r|t$";
+---------+
| name    |
+---------+
| root    |
| halt    |
| rpc     |
| rpcuser |
+---------+
4 rows in set (0.00 sec)

# 名字r开头t结尾
mysql> select name from user where name regexp "^r.*t$";
+------+
| name |
+------+
| root |
+------+
1 row in set (0.00 sec)

别名/去重/合并

给查看的列定义新的名字

多行数据相同时仅显示一遍

逻辑匹配

有多个筛选条件

多个筛选条件 必须同时成立 逻辑与 and &&

多个筛选条件 某个条件成立即可 逻辑或 or ||

逻辑非 取反 not !

# 逻辑非例子,查看解释器不是/bin/bash 的
mysql> select name,shell from user where shell != "/bin/bash";
# not 也是取反  要放在表达式的前边
mysql> select name,shell from user where not shell="/bin/bash";
# id值不在 10 到 20 之间 
mysql> select id,name from user where not id between 10 and 20;

# 逻辑与 例子
mysql> select name,uid from user where name="root" and uid=1;
mysql> select name,uid from user where name="root" and uid=0;
+------+------+
| name | uid  |
+------+------+
| root |    0 |
+------+------+
1 row in set (0.00 sec)

# 逻辑或 例子
mysql> select name,uid from user where name="root" or name="bin" or uid=1;
+------+------+
| name | uid  |
+------+------+
| root |    0 |
| bin  |    1 |
+------+------+
2 rows in set (0.00 sec)

提高优先级 ()? ? ? ? 多个判断条件时使用and的优先级高于or

mysql> select 2+3*5;        # 使用默认计算顺序 先乘除后加减
+-------+
| 2+3*5 |
+-------+
|    17 |
+-------+
1 row in set (0.00 sec)

mysql> select (2+3)*5;      # 先加法再乘法
+---------+
| (2+3)*5 |
+---------+
|      25 |
+---------+
1 row in set (0.00 sec)

逻辑匹配什么时候需要加()
逻辑与and 优先级高于逻辑或 or
如果在筛选条件里既有and 又有 or 默认先判断and 再判断or
# 没加() 的查询结果
mysql> select name,uid from user
    -> where name="root" or name="bin" and uid=1;
+------+------+
| name | uid  |
+------+------+
| root |    0 |
| bin  |    1 |
+------+------+
2 rows in set (0.00 sec)

# 加()的查询结果
mysql> select name,uid from user where (name="root" or name="bin") and uid=1;
+------+------+
| name | uid  |
+------+------+
| bin  |    1 |
+------+------+
1 row in set (0.00 sec)

字符比较

= 相等比较? ? ? ?!= 不相等比较

# 查看表里是否有名字叫apache的用户
mysql> select name from user where name="apache";
+--------+
| name   |
+--------+
| apache |
+--------+
1 row in set (0.00 sec)

# 输出解释器不是/bin/bash的用户名 及使用的解释器
mysql> select name,shell from user where shell != "/bin/bash";
+-----------------+----------------+
| name            | shell          |
+-----------------+----------------+
| bin             | /sbin/nologin  |
| daemon          | /sbin/nologin  |
| adm             | /sbin/nologin  |
| lp              | /sbin/nologin  |
| sync            | /bin/sync      |
| shutdown        | /sbin/shutdown |
| halt            | /sbin/halt     |
| mail            | /sbin/nologin  |
| operator        | /sbin/nologin  |
| games           | /sbin/nologin  |
| ftp             | /sbin/nologin  |
| nobody          | /sbin/nologin  |
| systemd-network | /sbin/nologin  |
| dbus            | /sbin/nologin  |
| polkitd         | /sbin/nologin  |
| sshd            | /sbin/nologin  |
| postfix         | /sbin/nologin  |
| chrony          | /sbin/nologin  |
| rpc             | /sbin/nologin  |
| rpcuser         | /sbin/nologin  |
| nfsnobody       | /sbin/nologin  |
| haproxy         | /sbin/nologin  |
| apache          | /sbin/nologin  |
| mysql           | /bin/false     |
+-----------------+----------------+
24 rows in set (0.00 sec)

空 表头下没有数据 使用 is null

非空 表头下有数据 使用 is not null

mysql服务 使用关键字null或NULL表示表头没有数据

# 添加新行 仅给行中的id 表头和name表头赋值
mysql> insert into user(id,name) values(71,"");        # 零个字符
mysql> insert into user(id,name) values(72,"null");    # 普通字母
mysql> insert into user(id,name) values(73,NULL);      # 表示空
mysql> insert into user(id,name) values(74,null);      # 表示空

# 查看id表头值大于等于71  的行 仅显示行中 id表头 和  name 表头的值
mysql> select id,name from user where id>=71;
+----+------+
| id | name |
+----+------+
| 71 |      |
| 72 | null |
| 73 | NULL |
| 74 | NULL |
+----+------+
4 rows in set (0.00 sec)

# 查看name 表头没有数据的行 仅显示行中id表头 和  naeme 表头的值
mysql> select id,name from user where name is null;
+----+------+
| id | name |
+----+------+
| 73 | NULL |
| 74 | NULL |
+----+------+
2 rows in set (0.00 sec)

# 查看name 表头是0个字符的行, 仅显示行中id表头 和  naeme 表头的值
mysql> select id,name from user where name="";
+----+------+
| id | name |
+----+------+
| 71 |      |
+----+------+
1 row in set (0.00 sec)

# 查看name 表头值是null的行, 仅显示行中id表头 和  naeme 表头的值
mysql> select id,name from user where name="null";
+----+------+
| id | name |
+----+------+
| 72 | null |
+----+------+
1 row in set (0.01 sec)

# 查看name 表头有数据的行, 仅显示行中id表头 和  name 表头的值
mysql> select id,name from user where name is not null;

文章来源:https://blog.csdn.net/it_zhenxiaobai/article/details/135474209
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。