Process, the virtual environment, Mysql master-slave

process

View the process ps (similar windows task manager)

man 1 ps # 查看命令文档
ps[options]

1   UNIX options, which may be grouped and must be
           preceded by a dash.
2   BSD options, which may be grouped and must not be used
           with a dash.
3   GNU long options, which are preceded by two dashes

Supported command format

  • unix format: -h -e
  • BSD format: axu
  • GNU long form: - help

Options

[root@taosiyu~]ps
 `
a  # 所有的终端 
ps a

x # 包括不连接终端的进程
u # 显示进程详细信息 
f # 显示进程树,进程的从属关系
k             # 指定排序方式,默认升序k后加-降序
ps aux k%cpu  # 默认按照cpu升序排列
ps aux k-%cpu # 降序排列

o             # 显示指定字段,不能和u同时使用
ps o pid,%cpu,cmd
ps ax o pid,%cpu,cmd

L 
# 获取o支持的选项,nice字段代表执行顺序,数值越小执行越先,
# psr cpu编号 STAT状态 
# %cpu cpu的占用率 %men内存占用率

-L # 显示线程 LWP

-e # 相当于ax
-f # 显示详细信息,相当于u
-F # 显示更详细的信息
-H # 显示树状结构
-p # 根据pid寻找该进程状态
-U # 根据用户查找,可以监控用户

常用选项: aux -ef -eFH

ps -ef|grep sshd # 查找关于某项的进程

ps default display format

PID TTY TIME CMD
45246 pts/0 00:00:00 bash
50768 pts/0 00:00:00 ps
pid terminal The current command occupancy cpu time command

u show details format

USER PID %CPU %MEN VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 193816 5288 ? Ss Aug29 0:20 / usr / lib / systemd / systemd -
root 2 0.0 0.0 0 0 ? S Aug29 0:00 [
user pid Users occupancy Memory Usage Virtual Memory (program considers available) Permanent memory (real memory) terminal status Start Time Time Command

According to query the name of the process

pidof name # 根据名称

pidof python
1169   825
-s: 仅返回一个进程号;
-c: 仅显示具有相同"root"目录的进程;
-x: 显示由脚本开启的进程;
-o: 指定不显示的进程ID

System Tool

uptime

 # 可以查看系统是否健康
 09:18:18    up 15:14,       2 users,       load average: 0.00, 0.01, 0.05
 当前时间  当前服务器运行时长  当前的在线用户数    cpu的负载:1分钟负载,5分钟负载,15分钟负载 
 cpu平均负载:在特定时间之内cpu运行的平均进程数,不超过cpu核心数的2倍认为良好

top (real-time)

top- 14:26:49 up 15:14 2users load average: 0.00, 0.01,0.05 l show and hide the header information
Header information current time The current long-running server The current number of online users cpu load: 1min load, 5min load, 15min load
Tasks: 217 total 1running, 216sleeping, 0stoped, 0zombie
task Total number of processes Number of runs The number of sleep Stop number Zombie process
%Cpu(s): 0.0us, 0.0sy, 0.0ni 99.9 id,0.0wa,0.0si,0.0st cpu show a (number)
cpu usage User space System space nice value Idle, wait time, hard interrupt soft interrupt, the virtual machine stolen time
KIB Mem 1865308 total, 146820 free, 812016 used, 906472 buff/cache m switch the display memory
Memory Information Total Memory Free memory Used Memory Buffer cache / Page cache
buffer cache and page cache
Sort: default cpu occupancy of the memory is switched to the P M is switched to the CPU occupation time T
KiB Swap: 2097148 total, 2088180 free, 8968 used 761920 avail Mem
Virtual Memory (divided from the hard drive, memory is not enough to call time) when orcle installation, the system requirements must swap space m View memory information
# 操作,显示信息操作详见表格最后一栏
退出:q
修改刷新频率: s time # 默认是三秒
杀死进程: k          #  默认是杀死第一个
保存文件: W          # 写入 .toprc
选项:-d time         # 指定刷新时间
        -b          # 显示所有信息
        -n num      # 刷新num次退出**

htop

epel源
# 下载
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 安装
yum install -y htop
# 调整样式
F2
# 杀掉进程
k

Performance Analysis

free memory

-b 以字节形式
-k kb
-m mb
-h 人类易读方式
-c num 刷新num次

vmstat

vmstat [options] [delay [count]]
vmstat 1 3 # 每秒刷新一次,刷新三次退出
[root@taosiyu test] # vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0   8968 144792     76 909228    0    0    12     1   23   22  0  0 100  0  0
 
procs:
r:正在运行的进程个数
b:阻塞队列的长度

memory:
swap:虚拟内存大小
free:空闲物理内存大小
buff:用于缓存大小
cache:用于chache的大小

swap:
si:从磁盘交换到内存的数据速率(kb/s) # 读取速度
so:从内存到磁盘的数据速率(kb/s)     # 写入速度

io:
bi:从磁盘读取到系统的速率(kb/s)     # 读取速度    
bo:从系统写入到磁盘的速率(kb/s)     # 写入速度

system:
in:中断频率
cs:进程之前切换的频率


cpu:
us:用户空间
sy:系统空间
id:cpu空闲时间
wa:等待时间
st:虚拟机偷走的时间

iostat

View disk read and write speed

iostat num

View all dstat

yum instattl dstat
# dsk 磁盘 paging 配置页

dstat # 显示所有部分
-c  # CPU
-d  # 硬盘
-n  # 网络
-m  # 内存 
-p  # 进程
-r  # IO
-swap # swao空间

--top-cpu  # 显示占用cpu最多的进程
--top-io   # 显示占用io最多的进程
--top-mem  # 显示占用内存最多的进程
--tcp      # 显示tcp的信息
--udp      # 显示udp的信息

iftop flow display card

Process management tools

kill (64 commands)

# 向进程发送信号,实现对进程的管理,每个信号,对应不同的值也对应不同的含义.不区分大小写

# 查看可用信号kill -l
1)sighub  不需要关闭程序,重新加载配置文件
2)signint 终止进程,相当于ctrl+c
9)sigkill 强制杀死进程 
15)SIGTERM 终止正在运行的进程
18)SIGCONT 继续  # 可以和-19配合使用继续后台休眠的程序
19)SIGSTOP 后台休眠
# 使用方法
1.ps -ef|grep '进程名称' # 搜索想知道进程的id号
2.kill -信号序号 进程号

按照pid查找: pid -n pid
按照名称: killall -n name   pkill -n name # 用名称执行的操作范围比较大,方便但小心使用
killall -9 sshd 

Job Management

  • Foreground job: the job has been occupied by the terminal
  • Background job: do not take up the job of the current terminal
让作业运行于后台:
对于启动中     # ctrl + z 
对于没有启动的  # command & 
# ping www.baidu.com & 也会输出到终端
  • From the terminal
1.nohup
nohup command &>/dev/null &
# 此条会多显示一条信息,会生成一个文件写入文件中
nohup ping www.baidu.com &>/dev/null &
# 不显示提示信息,会生成一个文件写入文件中
tail -f nohup.out # 追踪显示新追加到nohup里的内容
# 终止进程时依然是杀死command

2.screen
yum install screen

screen ping www.baidu.com
# 此时关掉终端仍在运行
screen -list
# 查看所有的screen窗口
-r 进入窗口
# 如果有多个可以输入pid选择进入哪个
screen -r pid

Safety

Firewall

公司一般采用硬件防火墙,几乎不用该防火墙
硬件防火墙:1.绿盟2.深信服3.启明星辰4.飞塔5.思科6.华为7.华三
4表5链

iptables    # 防火墙
iptables -L # 查看防火墙信息,默认查看链 input output
iptables -F # 清空防火墙
systemctl disabled friewalld # 开机不启动
systmectl stop friewalld     # 关闭防火墙

selinux

美国国家安全局

配置文件下 vim /etc/selinux/config
SELINUX=disabled
# 重启服务,永久生效

setenforce 0 # 临时生效,改完状态为第二个等级
getenforce   # 查看selinux状态

红帽证书: 1.rhcea 2.rhce 3.rhca
思科证书: 1.ccnp 2.ccie 3.ccia

Software compiled If you want to delete, delete directory can be compiled

Virtual Environment

python3默认支持虚拟环境

pip3 list 查看安装包

1.安装
pip3 install virtualenv -i https://pypi.douban.com/simple

2.创建虚拟环境
virtualenv --help   # 查看虚拟环境的操作
 --no-site-packages DEPRECATED. Retained only for backward compatib
 # --不推荐使用任何网站包  已弃用。仅为向后兼容保留
 # 解释:创建一个全新的python环境,不放在第三方包里 
                    # lib > site-packpages 第三方包
 --python           # 指定以哪个python虚拟环境进行创建
 操作:
 virtualenv --no-site-packages django_tao
 
3.进入虚拟环境
  source django_tao/bin/activate # 进入虚拟环境
  # source envdir/bin/activate
  echo $PATH # 此时打印环境就在djngo_tao里 
  pip3 install django==1.11 -i https://pypi.douban.com/simple
  
4.退出虚拟环境
  deactivate
  
5.确保环境一致 # 将windows安装的东西导入linux系统中
  # 在windows的cmd上执行如下命令:将windows上安装的包做快照
  pip freeze > requirement.txt
  将requirement.txt传到linux中,切换虚拟机
6.安装
  # 在虚拟机内
  pip install -r requirement.txt -i https://pypi.douban.com/simple
  # 逐行安装

Virtual Environment Management Tool

virtualenvwrapper

# 为了解决虚拟环境管理的问题,因为虚拟环境可以在任何目录下创建
1.安装
pip3 install virtualenvwrapper -i https://pypi.douban.com/simple
vim ~/.bashrc
2.修改文件
vim ~/.bashrc
export WORKON_HOME=/envdir   
# 设置virtualenv的同一管理目录,以后虚拟环境只能正在envdir目录下
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages' 
# 添加virtualenvwraaper的参数,生成干净隔绝的环境 
export VIRTUALENVWRAPPER_PYTHON=/opt/python36/bin/python3
# 指定python解释器
source /opt/python36/bin/virtualenvwrapper.sh
# 执行virtualenvwrapper安装脚本
3.加载~/.bashrc
source ~/.bashrc
4.创建环境
mkvirtualenv django_tao 创建并切换
5.进入虚拟环境
workon name
6.切换当前虚拟环境的文件夹
cdvirtualenv
7.切换到当前虚拟环境的第三方包的文件夹
cdsitepackages
8.退出
deacticate
9.列出当前管理的虚拟环境
lsvirtualenv
10.列出当前虚拟环境的第三方包
lssitepackages
11.删除虚拟环境
rmvirtualenv django20^C # 必须要退出才能删除

Mysql

mariadb is a branch of mysql

Can be directly downloaded from the official website

download

wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar

Decompression

tar xf MySQL-5.6.44-1.el7.x86_64.rpm-bundle.tar

installation

yum install -y *.rpm  # 下载5.7
# 默认安装目录:/var/lib/mysql

# 配置文件
vim /etc/my.cnf
# 文件下配置
datadir=/mydata/mysql # 数据库文件夹
socket=/mydata/mysql/mysql/mysql.sock
设置
[mysql]
socket=/mydata/mysql/mysql/mysql.sock

# 创建刚才配置的目录
mkdir -p /mydata/mysql  
# 把 mysql账号 设置有读写权限
chown mysql -R /mydata/mysql

# 日志文件
/var/log/mysqld.log

Error message:

2019-08-30T11:18:22.976635Z 0 [Warning] Can't create test file /mydata/mysql/localhost.lower-test
2019-08-30T11:18:22.976687Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.27) starting as process 2788 ...
2019-08-30T11:18:22.980289Z 0 [Warning] Can't create test file /mydata/mysql/localhost.lower-test
2019-08-30T11:18:22.980338Z 0 [Warning] Can't create test file /mydata/mysql/localhost.lower-test

# 解决办法:
setenforce 0

reset Password

默认密码:
grep 'pass' /var/log/mysqld.log
# cat .mysql_secret现在版本不在这里面 
# 这个密码,登陆一次后必须重新设置密码
mysql_secure_installation

输入root密码
是否要修改密码
是否要修改root密码(大小写、数字、特殊字符)
是否要删除匿名用户
是否禁止root远程登录
是否要删除test数据库
是否要刷新表的权限
systemctl start mysqld
# 这里可以用ss查看有没有3306端口号

setenforce 0 
# 如果起服务起不来 可以用ss -anlp查看是否端口冲突,mysql默认端口3306

Password validation rules

# 设置密码的校验规则
mysql > set global validate_password_policy=0;
0 校验级别最低,只校验密码的长度,长度可以设定.
1 必须包裹大写字母,小写字母,数字,特殊字符.
2 必须满足上面两条,并且对于密码中任意连续的4个(或者4个以上) 字符不能是字典中的单词.

# 修改密码的最短长度
mysql > set global validate_password_length=3; 修改密码的最短长度

Create a user

create user 'username'@'ip' identified by 'password'
全部ip则是%

View Permissions

show grants;
# 如果格式不会 help grants

User Authorization

# 给账号授权
mysql > grant all on *.* to 'eva'@'%' identified by 'passwd';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql > flush privileges;   # 刷新使授权立即生效
Query OK, 0 rows affected (0.00 sec)

Mysql master-slave

zc

The master server is configured as follows

# 纯净干净的情况下做主从
# 配置文件
vim /etc/my.cnf
server-id=1
log-bin=/mydata/log/master-bin        # 启动binlog日志
sync_binlog=1                         # 确保主从复制事务的安全
# 常见配置的文件夹
mkdir /mydata/log
# 给文件夹设置权限
chown mysql -R /mydata/
systemctl restart mysqld              # 重启服务

# 有数据的情况下可以用数据备份
#语法:
# mysqldump -h 服务器 -u用户名 -p密码 数据库名 > 备份文件.sql
#示例:
#单库备份
mysqldump -uroot -p123 db1 > db1.sql
mysqldump -uroot -p123 db1 table1 table2 > db1-table1-table2.sql
#多库备份
mysqldump -uroot -p123 --databases db1 db2 mysql db3 > db1_db2_mysql_db3.sql
#备份所有库
mysqldump -uroot -p123 --all-databases > all.sql

# 从服务器在此练习中为了方便可以复制一个服务器暂做练习

The primary server to execute a sql

mysql > grant replication slave on *.* to 'slave'@'192.168.21.131' identified by 'password';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

show master status\G
# grant replication '用户名' on *.* to 'slave'@identified by '密码'

From the service configuration is as follows:

vim /etc/my.cnf

server-id =12
relay_log =/mydata/log/slave-log
sync_binlog = 1
# 只读
read-only=ON

Execute the following sql from the server

CHANGE MASTER TO
  MASTER_HOST='master2.example.com',  # 主服务器ip 
  MASTER_USER='replication',          # 用户名
  MASTER_PASSWORD='password',         # 密码
  MASTER_PORT=3306,                   # 端口
  MASTER_LOG_FILE='master2-bin.001',  # 此处填写主服务器的log文件
  MASTER_LOG_POS=4,                   # 此处写主服务器的Position
  MASTER_CONNECT_RETRY=10;            # 监控主服务器的时间
# 连接主库
change master to master_host='192.168.21.128',master_user='slave',master_password='1234';
# 启动进程
start slave;
# 查看状态
show slave status\G

# Slave_SQL_Running: Yes 
# Slave_IO_Running: Yes
# 看到这两条状态即代表成功

CHANGE MASTER TO
  MASTER_HOST='192.168.26.128',  
  MASTER_USER='slave',          
  MASTER_PASSWORD='1234',         
  MASTER_PORT=3306,                   
  MASTER_LOG_FILE='master-bin.000002',  
  MASTER_LOG_POS=603,                   
  MASTER_CONNECT_RETRY=10;            

problem:

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
# 查看uuid
[root@taosiyu ~]#cat /mydata/mysql/auto.cnf
[auto]
server-uuid=23204da4-cbcb-11e9-b26b-000c29397cf8

# 解决办法:
rm -rf /mydata/mysql/auto.cnf
systemctl restart mysqld

Guess you like

Origin www.cnblogs.com/taosiyu/p/11440554.html