CentOS8配置mysql

安装MySQL8.0

使用最新的包管理器安装MySQL

1
sudo dnf intall @mysql

开机启动

运行以下命令来启动MySQL服务并使用它在启动时自动启动

1
sudo systemctl enable --now mysqld

检查MySQL是否正在运行

1
sudo systemctl status mysqld

添加密码及安全设置

运行mysql_secure_installation脚本

1
sudo mysql_secure_installation

步骤如下:

1.要求你配置VALIDATE PASSWORD component(验证密码组件): 输入y ,回车进入该配置

  • 选择密码验证策略等级, 我这里选择0 (low),回车
  • 输入新密码两次
  • 确认是否继续使用提供的密码?输入y ,回车
  • 移除匿名用户? 输入y ,回车
  • 不允许root远程登陆? 我这里需要远程登陆,所以输入n ,回车

2.移除test数据库? 输入y ,回车

3.重新载入权限表? 输入y ,回车

配置远程登录

如果需要设置root账户远程登陆,上一步骤中,不允许root远程登陆?这一步需要设为n。
接下来本机登录MySQL,将root用户的host字段设为’%’,意为接受root所有IP地址的登录请求:
本机登录MySQL

1
mysql -uroot -p<上面步骤中设置的密码>

接着继续执行mysql语句,将将root用户的host字段设为’%’:

1
2
3
use mysql;
update user set host='%' where user='root';
flush privileges;

设置完成后输入exit退出mysql,回到终端shell界面,接着开启系统防火墙的3306端口:

1
2
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload

关闭MySQL主机查询dns

MySQL会反向解析远程连接地址的dns记录,如果MySQL主机无法连接外网,则dns可能无法解析成功,导致第一次连接MySQL速度很慢,所以在配置中可以关闭该功能。

打开/etc/my.cnf文件,添加以下配置:

1
2
[mysqld]
skip-name-resolve

重启服务

1
sudo systemctl restart mysqld
e>select age from person group by age;
select name,age from person group by name,age;
select age,count(1) from person group by age;

分组统计

1
select count(1) from (select age from person group by age) temp

分组获取最大值

1
2
## 统计各年龄的最大id
select max(id) ,age from person group by age;

having的使用

1
2
3
4
## where不能和group by一起使用,所以此时出现了having
select age from person group by age having age > 2;
select count(1),age from person group by age having count(1) > 1;
select age from person group by age having count(1) > 1;

having后面并不是所有字段都可以作为判断条件,必须是可以放在select 和 from之间的字段才能作为判断条件。

多表查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
## student表
create table student(
id int(11) primary key auto_increment;
name varchar(100) default '',
age int(11) default 0
);

## score表
create table score(
id int(11) primary key auto_increment,
student_id int(11),
type varchar(100) default '',
score int(11) default 0
);

交叉查询

1
2
3
4
5
## 笛卡尔积运算
select * from student cross join score
select * from student st cross join score sc where st.id = sc.student_id;
## 开发中最常用的语句
select st.name st,age,sc.type,sc.score from student st,score sc where st.id = sc.student_id;

内连接查询

1
2
## 这种方式和交叉连接得到的结果相同,所以这种方式很少使用
select * from student st inner join score sc on st.id = sc.student_id;

外连接查询

1
2
3
4
## 左外连接(会获取到左边的所有数据)
select * from student st left outer join score sc on st.id = sc.student_id;
## 右外连接
select * from student st right outer join score sc on st.id = sc.student_id;

嵌套查询

1
select * from score where student_id in (select id from student where naem = 'zhangsan');

组合查询

1
2
3
4
5
6
7
8
## union中,所有查询的列数和列顺序必须相同
select cust_name, cust_contact, cust_email
from customers
where cust_state in ('a,'b','c')
union
select cust_name, cust_contact, cust_email
from customers
where cust_name = 'test';

索引操作

1
2
3
4
5
6
## 创建索引
create index user_index on user(id);
## 创建唯一索引
create unique index user_index on user(id);
## 删除索引
alter table user drop index user_index;

创建索引能提高查找效率,但是索引也有弊端。创建索引系统会自动维护一个索引表,每当数据增加,更新,删除时都需要更改索引表,所以创建索引会降低增加,更新,删除的效率。

创建索引的建议:

  1. 定义主键的数据列一定要建立索引
  2. 定义有外键的数据列一定要建立索引
  3. 对于经常查询的数据列最好建立索引
  4. 对于需要在指定范围内的快速或频繁查询的数据列最好创建索引
  5. 经常用在where句中的数据列最好创建索引
  6. 经常出现在关键字order by,group by,distinct后面的字段,建立索引。如果建立的是复合索引,索引的字段顺序要和这些关键字后面的字段顺序一致,否则索引不会被使用。

不建议创建索引:

  1. 对于那些查询中很少设计的列,重复值比较多的列不要建立索引
  2. 对于定义为text,image和bit的数据类型的列不要建立索引
  3. 对于经常存取的列避免建立索引
  4. 限制表上的索引数目。对一个存在大量更新操作的表,所建索引的数目一般不要超过3个,最多不要超过5个,。索引虽说提高了访问速度,但是太多索引会比较影响数据的更新操作
  5. 对复合索引,按照字段在查询条件中出现的频度建立索引。在复合索引中,记录首先按照第一个字母排序。对于在第一个字段上取值相同的记录,系统再按照第二个字段的取值排序,以此类推。因此只有复合索引的第一个字段出现在查询条件中,该索引才可能被受用,因此将应用频度高的字段,放置在复合索引的前面,会使系统最大可能地使用此索引,发挥索引的作用。

sql数据类型

详细数据类型

数据类型 占用字节数 对比
整数
tinyint 1个字节(-128-127)
smallint 2个字节(-2的16次方~2的16次方-1)
mediumint 3个字节(-2的24次方~2的24次方-1)
int 4个字节(-2的32次方~2的32次方-1) 与java的int取值范围相同
bigint 8个字节(-2的64次方~2的64次方-1) 与java的long取值范围相同
小数
float(n,d)(n表示总位数,d表示小数位数) 单精度浮点型 4个字节 对应java的float
double(n,d)(n表示总位数,d表示小数位数) 双精度浮点型 8个字节 对应java的double
字符串
char(n) 固定长度,最多255个字符
varchar(n) 可变长度,最多65535个字符
tinytext 可变长度,最多255个字符
text 可变长度,最多65535个字符
mediumtext 可变长度,最多2的24次方-1个字符
longtext 可变长度,最多2的32次方-1个字符
时间
date 日期,只包含年月日
time 时间,只包含时分秒
datetime 日期和时间都包含
timestamp 时间戳
enum todo 待完善
set todo 待完善

数据库关系设计

关系类型

主键和外键


三大范式

事务处理

1
2
3
4
5
6
7
8
9
10
11
12
## 开始事务
start transaction;
## 插入操作 A
insert into 'user' values(xxx,xxx);
## 创建保留点
savepoint updateA;
## 插入操作 B
insert into 'user' values(yyy,yyy);
## 回滚到保留点updateA
rollback to updateA;
## 提交事务,只有操作 A 生效
commit;

权限控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
## 创建账户
create user myuser identified by 'mypassword';
## 修改账户
update user set user = 'newuser' where user='myuser';
flush privileges;
## 删除账户
drop user myuser;
## 查看权限
show grants for myuser;
## 授予权限
grant select,insert on *.* to myuser;
## 删除权限
revoke select ,insert on *.* from myuser;
## 更改密码
set passowrd for myuser = 'mypass';

存储过程(todo 待完善)

触发器(todo 待完善)

SQL优化

sql工具

python相关

sqlalchemy-engine

sqlalchemy-mysql

网上的sql资源

图解SQL
138张图带你MySQL入门

互联网公司常用分库分表方案汇总

数据库设计技巧

SQL试题

MySQL

SQL基础知识汇总

常用sql

复制表

1
create table tbA as select * from tbB;

删除表中重复数据

1
delete from vod where id not in (select count(t.id) from (select min(id) as id from vod group by vod_play_url) t)

查询是否有重复数据

1
select vod_play_url from vod group by vod_play_url having count(vod_play_url) > 1;

Linux常用命令

安装deb

1
sudo dpkg -i baidunetdisk_linux_2.0.2.deb

安全检测

查看恶意IP登录

1
lastb root | awk '{print $3}' | sort | uniq -c | sort -nr| more

查看是否有异常的系统用户

1
cat /etc/passwd

检查是否有新用户尤其是UID和GID为0的用户

1
awk -F":" '{if($3 == 0){print $1}}' /etc/passwd

检查是否存在空口令账户

1
awk -F: '{if(length($2)==0) {print $1}}' /etc/passwd

检查系统异常进程

使用ps -ef命令查看进程(尤其注意UID为root的进程

1
ps -ef

查看该进程所打开的端口和文件

1
2
netstat -ntlup
lsof -p 22

检查隐藏进程

1
ps -ef | awk '{print $2}'| sort -n | uniq >1; ls /proc |sort -n|uniq >2;diff -y -W 40 1 2

安装jdk

1
2
3
tar -zxvf jdk-8u171-linux-x64.tar.gz
sudo mv jdk1.8.0-171 /usr/local/jdk1.8
sudo vim /etc/profile

在文件末尾加入

1
2
3
4
export JAVA_HOME=/usr/local/jdk1.8
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=.:${JAVA_HOME}/bin:$PATH

更新生效

1
source /etc/profile

检查是否生效

1
2
java -version
javac

ubuntu安装mysql5.7

安装mysql

1
sudo apt-get install mysql-server

检查状态

1
sudo netstat -tap | grep mysql

设置root密码

1
2
3
4
5
6
7
8
9
sudo su
mysql
select user,plugin from mysql.user;
update mysql.user set authentication_string=PASSWORD('password'), plugin='mysql_native_password' where user='root';
flush privileges;
exit

sudo /etc/init.d/mysql restart
mysql -uroot -p

配置mysql远程登录
修改配置文件,bind-address = 0.0.0.0

1
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

保存退出,然后进入mysql服务,执行授权命令

1
2
3
4
5
mysql -u root -p
grant all on *.* to root@'%' identified by 'guqiao*2011_' with grant option;
flush privileges;
exit
sudo /etc/init.d/mysql restart

卸载mysql

安装nginx

1
sudo yum install -y nginx

设置开机启动

1
sudo systemctl enable nginx

关闭开机启动

1
sudo systemctl disable nginx

启动nginx

1
sudo systemctl start nginx

停止nginx

1
sudo systemctl stop nginx

查看nginx启动状态

1
sudo systemctl status nginx

重新加载nginx配置

1
sudo systemctl reload nginx

nginx默认的静态HTML文件位置

1
/usr/share/nginx/html

如果要使用NGINX作为默认的web服务器,则可以使用/var/www文件夹来存储不同的网站。但是如果将请求代理到Apache,则只需修改NGINX配置文件,就可以使用Apache网站文件的/var/www路径

安装redis

1
sudo apt-get install redis-server

查看tcp连接

1
netstat -ap | grep 6379

启动
服务:redis-server
客户端:redis-cli
修改配置文件允许局域网内的其他电脑连接redis
文件位置:/etc/redis/redis.conf
详细的使用:https://juejin.im/entry/5bac46dd5188255c960c3d27

开启SSH服务

查看ssh是否安装

1
sudo ps -e | grep ssh

安装ssh

1
2
3
4
5
sudo apt-get install openssh-server
sudo /etc/init.d/ssh start
sudo service ssh status
sudo service ssh stop
sudo service ssh restart

更改sshd_config配置

1
vim /etc/ssh/sshd_config 
1
PermitRootLogin yes
1
ssh root@192.168.1.115

配置说明

使用xshell进行文件上传和下载

1
apt-get install lrzsz

rz 上传文件

z file 下载文件

附件

linux查看公网ip

1
curl ipinfo.io/json

查看系统资源占用

查看ubuntu的资源占用

1
top

查看内存使用情况

1
free

更新系统运行时间,负载信息

1
uptime 

显示磁盘活动情况

1
vmstat 

统计CPU的使用情况及tty设备、硬盘和CD-ROM的I/O量

1
iostat

查看磁盘使用情况

1
2
df -h 
du -sh

查看mysql信息

1
ps -aux | grep mysql

安装htop工具查看

1
sudo apt-get install htop

安装seafile

https://cloud.seafile.com/published/seafile-manual-cn/deploy/using_mysql.md

安装ffmpeg

1
2
3
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
sudo apt-get update
sudo apt-get install ffmpeg

查看访问数

1
cat /var/log/auth.log  | grep "sshd" | awk '/Failed/{print $(NF-3)}' | sort | uniq -c 

centos安装mysql

https://www.centos.bz/2019/02/centos7-%E5%AE%89%E8%A3%85-mysql/

卸载腾讯云自带配置

1
2
3
4
5
6
/usr/local/qcloud/stargate/admin/uninstall.sh
/usr/local/qcloud/YunJing/uninst.sh
/usr/local/qcloud/monitor/barad/admin/uninstall.sh
sudo rm -rf /usr/local/qcloud/
sudo rm -rf /usr/local/sa/
sudo rm -rf /usr/local/agenttools

安装监控(主机面板)

宝塔linux:
https://www.bt.cn/bbs/thread-19376-1-1.html
Ajenti:
Feathur:
ISPConfig:
VestaCP:
Virtualmin:
ZPanel:
WDCP:
AMH:
WDCP:
LuManager:
VPSMate:
Oneinstack:
AppNode:
cpanel:

基本命令

1
2
3
4
5
ls
ls -al
cd
mkdir aaa
mkdir -p aaa/bbb/ccc

删除文件

1
rm -r  aaa

删除文件夹

1
rm -rf  aaa

修改文件夹名

1
mv aaa bbb

创建新文件

1
2
3
touch name.txt
echo "content" > a.txt
cat a.txt

权限管理

本组删除读写权限

1
chmod g-rw  name.txt

其他组删除读写权限

1
chmod o-rw name.txt 

个人添加可执行权限

1
chmod u+x name.txt

如果要将一个文件夹的所有内容权限统一修改,则可以-R参数

1
2
chmod -R 770 aaa
chown xxx:xxx aaa/

常用操作系统命令

1
2
useradd  username
passwd username

切换身份

1
su user 

执行这条指令用root身份,执行完成后用原来的身份

1
sudo useradd zhangsan

查看主机名

1
hostname

修改主机名(重启后无效)

1
hostname  hadoop

修改主机名(重启后有效)

1
vi /etc/sysconfig/network

修改ip(重启后无效)

1
ifconfig eth0 192.168.12.22

修改ip(重启后生效)

1
vi /etc/sysconfig/network-scripts/ifcfg-eth0

查看系统命令

1
2
uname -a
uname -r

查看日期

1
date
1
2
3
mount
umount
mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom

查看文件大小

1
2
du -sh
du -ah

查看分区

1
df -h

ssh命令

1
ssh root@192.168.2.11

关机

1
2
halt
shutdown -h now /init 0

重启

1
2
reboot
shutdown -r now /reboot

远程拷贝

1
scp install.log  root@192.168.1.2:/home

公钥 私钥生成

1
ssh-keygen

复制公钥到远程主机

1
ssh-copy-id 192.168.33.111

一次性将文件内容全部输出(控制台)

1
cat somefile 

可以翻页查看,下翻一页(空格),上翻一页(b),退出(q)

1
more somefile

可以翻页查看,下翻一页(空格) 上翻一页(b) 上翻一行(↑),下翻一行(↓) 可以搜索关键字(/keyword)

1
less somefile  

查看文件尾部的10行

1
tail -10 install.log

小f跟踪文件的唯一inode号,就算文件名改后,还是跟踪原来这个inode表示的文件

1
tail  -f install.log

大F按照文件名来跟踪

1
tail -F install.log

查看文件头部的10行

1
head -10 install.log

后台服务管理

查看指定服务的状态

1
service network status

停止指定服务

1
service network stop

启动指定服务

1
service network start

重启指定服务

1
service network restart

查看系统中所有的后台服务

1
service --status-all

设置后台服务的自启配置

chkconfig 查看所有服务器自启配置

chkconfig iptables off 关掉指定服务的自动启动

chkconfig iptables on 开启指定服务的自动启动

上传安装包到服务器

可以使用图形化工具,如:filezile
可以使用sftp工具, securecrt alt+p 调出后,用put命令上传
上传(如果不cd指定目录,则上传到当前用户的主目录)

1
2
sftp> cd /home/
sftp> put c:\xxx.xxx

下载

1
sftp> get xxxxx.xxxx

压缩和解压缩

1
gzip -d install.log

将1.xxx 2.xxx 343/文件夹打包到xxx.tar文件中

1
tar -cvf xxx.tar 1.xxx 2.xxx  343/
1
tar -xvf xxx.tar 

一次性压缩

1
tar -zcvf my.tar.gz  /somefile 

一次性解压缩

1
tar -zxvf my.tar.gz 

linux jdk环境变量设置

1
vi /etc/profile

大G 到最后一行

o 插入一行

1
2
3
export JAVA_HOME=/root/apps/jdkhome
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile

查询外网IP的办法

Curl纯文本格式输出

1
2
3
4
5
6
7
curl icanhazip.com
curl ifconfig.me
curl curlmyip.com
curl ip.appspot.com
curl ipinfo.io/ip
curl ipecho.net/plain
curl www.trackip.net/i

Curl JSON格式输出

1
2
3
curl ipinfo.io/json
curl ifconfig.me/all.json
curl www.trackip.net/ip?json

Curl XML格式输出

1
curl ifconfig.me/all.xml

Curl 得到所有IP细节 (挖掘机)

1
curl ifconfig.me/all

使用 DYDNS (当你使用 DYDNS 服务时有用)

1
2
curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'
curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"

使用 Wget 代替 Curl

1
2
wget http://ipecho.net/plain -O - -q ; echo
wget http://observebox.com/ip -O - -q ; echo

使用 host 和 dig 命令

1
2
host -t a dartsclink.com | sed 's/.*has address //'
dig +short myip.opendns.com @resolver1.opendns.com

bash 脚本示例

1
2
3
#!/bin/bash
PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
echo $PUBLIC_IP

安装ftp

1
yum -y install vsftpd

CentOS上用python调用FFMepg将m3u8合成MP4后显示异常的问题

(1)判断是否有中文包

1
locale -a

(2)如果没有中文包,则使用如下命令下载

1
yum install langpacks-zh_CN.noarch

multipass虚拟机安装

官网:https://multipass.run/

/proc/kcore 128T内存爆满问题处理

1
2
3
4
5
6
7
8
# 列出磁盘使用率
df -h
# 列出删除列表
lsof | grep delete
# 删除任务
kill XXXX
kill YYYY
....

查询大文件

1
find / -type f -size +1G

#(Centos7.9)端口号管理

开放端口

1
2
3
4
5
6
# 开放5672端口
firewall-cmd --zone=public --add-port=5672/tcp --permanent
# 关闭5672端口
firewall-cmd --zone=public --remove-port=5672/tcp --permanent
# 配置立即生效
firewall-cmd --reload

查看防火墙所有开放的端口

1
firewall-cmd --zone=public --list-ports

关闭/开启防火墙

1
2
systemctl stop firewalld.service
systemctl start firewalld.service

查看防火墙状态

1
firewall-cmd --state

查看监听的端口

1
netstat -lnpt

检查端口被哪个进程占用

1
netstat -lnpt | grep 18788