Linux常用运维命令笔记,


今天给大家整理一下Linux常用的命令,希望对大家能有所帮助!

一.MySQL相关

图片

1、查看mysql版本

  1. status; 
  2. select version() 

2、 mysql启动命令

  1. #01 使用 service 启动: 
  2.  service mysqld start (5.0版本) 
  3. service mysql start (5.5.7版本) 
  4. #02 使用 mysqld 脚本启动: 
  5. /etc/inint.d/mysqld start 
  6. #03 使用 safe_mysqld 启动 
  7. safe_mysqld& 

3、 mysql停止命令

  1. #01 使用service  
  2. service mysqld stop 
  3. #02 使用 mysqld 脚本 
  4. /etc/inint.d/mysqld stop 
  5. #03 mysqladmin命令 
  6. mysqladmin shutdown 

4、 mysql重启命令

  1. #01 使用 service 启动 
  2. service mysqld restart 
  3. service mysql restart #(5.5.7版本) 
  4. #02 使用 mysqld 脚本启动: 
  5. /etc/init.d/mysqld restart 

5、 修改密码

  1. update user set password='root' where user='root'; 
  2. flush privileges; 

6、执行sql文件

  1. #首先要把sql文件放在服务器上然后执行 
  2. source /usr/local/init.sql; 

7、设置防火墙,让 3306 端口对外可访问

  1. iptables -I INPUT -p tcp -m state --state  
  2. NEW -m tcp --dport 3306 -j ACCEPT 
  3. iptables -nL  
  4. service iptables save  

8、导出表结构 -d 表示导出表结构

mysqldump -uroot -proot -d

  1. mysqldump -uroot -proot -d dbname > test.sql 

二、Redis相关

1.Linux安装redis

  1. wget http://download.redis.io/releases/redis-2.8.17.tar.gz 
  2. tar xzf redis-2.8.17.tar.gz 
  3. cd redis-2.8.17 
  4. make 

2、启动redis

  1. #后台启动 末尾加 & 符号  
  2. nohup redis-server & 
  3. #指定redis配置文件启动 
  4. ./redis-server /etc/redis/6379.conf 
  5. #查看redis进程 
  6. ps -ef |grep redis 

3、停止redis

  1. #01 采用apt-get或者yum install安装的redis 
  2. /etc/init.d/redis-server stop #停止 
  3. /etc/init.d/redis-server restart #重启 
  4. #02 采用源码安装的方式,执行如下命令 
  5. redis-cli -h 127.0.0.1 -p 6379 shutdow 
  6. #03 采用kill进程的方式 
  7. kill -9 pid 

4、redis设置开机自动启动脚本

4.1、/etc/init.d/ 下创建 startRedis.sh 文件,内容如下:

  1. #!/bin/sh   
  2. #chkconfig: 2345 80 90   
  3. # Simple Redis init.d script conceived to work on Linux systems   
  4. # as it does use of the /proc filesystem.   
  5. REDISPORT=6379                          #端口号,这是默认的,如果你安装的时候不是默认端口号,则需要修改 
  6. REDISPATH=/usr/local/bin/                #redis-server启动脚本的所在目录,你如果忘了可以用find / -name redis-server 或whereis redis-server找到  
  7. EXEC=${REDISPATH}/redis-server             
  8. CLIEXEC=${REDISPATH}/redis-cli   
  9. PIDFILE=/var/run/redis_${REDISPORT}.pid  #在redis.conf中可找到该路径 
  10. CONF="${REDISPATH}/redis.conf"           #redis.conf的位置, 如果不和redis-server在同一目录要修改成你的redis.conf所在目录 
  11. case "$1" in   
  12.   start)   
  13.     if [ -f $PIDFILE ]   
  14.     then   
  15.         echo "$PIDFILE exists, process is already running or crashed"   
  16.     else   
  17.         echo "Starting Redis server..."   
  18.         $EXEC $CONF   
  19.     fi   
  20.     ;;   
  21.   stop)   
  22.     if [ ! -f $PIDFILE ]   
  23.     then   
  24.         echo "$PIDFILE does not exist, process is not running"   
  25.     else   
  26.         PID=$(cat $PIDFILE)   
  27.         echo "Stopping ..."   
  28.         $CLIEXEC -p $REDISPORT shutdown   
  29.         while [ -x /proc/${PID} ]   
  30.         do   
  31.           echo "Waiting for Redis to shutdown ..."   
  32.           sleep 1   
  33.         done   
  34.         echo "Redis stopped"   
  35.     fi   
  36.     ;;   
  37.   *)   
  38.     echo "Please use start or stop as first argument"   
  39.     ;;   
  40. esac   

4.2、设置可执行权限

  1. chmod 777 /etc/init.d/redis 

4.3、启动redis

  1. /etc/init.d/startRedis start 

4.4、设置开机启动

  1. chkconfig redis on 

三、mongodb相关

1、Linux下安装mongodb

1.1 下载安装包

https://www.mongodb.com/ 下载安装包

1.2 解压文件

  1. tar xzvf mongodb-linux-x86_64-4.0.6.tgz 

1.3 移动目录到/usr/local/mongodb

  1. mv mongodb-linux-x86_64-4.0.6 /usr/local/mongodb 

1.4 创建mongodb配置文件

/usr/local/mongodb/bin 目录下创建mongodb.conf文件,内容如下:

  1. dbpath=/usr/local/mongodb/data/db                 # 数据目录 
  2. logpath=/usr/local/mongodb/data/logs/mongodb.log  # 日志目录 
  3. port=27017 
  4. fork=true 
  5. auth=true 
  6. bind_ip=0.0.0.0 

2、启动mongodb

  1. cd /usr/local/mongodb/bin 
  2. mongod -f mongodb.conf 

3、设置mongodb开机自启

3.1 创建配置文件

创建 /etc/init.d/mongod 文件

  1. #!/bin/bash 
  2.  
  3. MONGO_HOME=/usr/local/mongodb 
  4. #chkconfig:2345 20 90 
  5. #description:mongod 
  6. #processname:mongod 
  7. case $1 in 
  8.     start) 
  9.         $MONGO_HOME/bin/mongod --config $MONGO_HOME/bin/mongodb.conf 
  10.         ;; 
  11.     stop) 
  12.         $MONGO_HOME/bin/mongod --shutdown --config $MONGO_HOME/bin/mongodb.conf\ 
  13.         ;; 
  14.     status) 
  15.         ps -ef | grep mongod 
  16.         ;; 
  17.     restart) 
  18.         $MONGO_HOME/bin/mongod --shutdown --config $MONGO_HOME/bin/mongodb.conf 
  19.         $MONGO_HOME/bin/mongod --config $MONGO_HOME/bin/mongodb.conf 
  20.         ;; 
  21.     *) 
  22.         echo "require start|stop|status|restart" 
  23.         ;; 
  24. esac 

3.2 添加服务然后设置开机自启

  1. #添加可执行权限 
  2. chmod 755 /etc/init.d/mongod 
  3. #添加MongoDB服务 
  4. chkconfig --add mongod 
  5. #设置MongoDB开机自启 
  6. chkconfig mongod on 

3.3 添加mongodb环境变量

  1. vim /etc/profile #追加如下内容 
  2. MONGO_HOME=/usr/local/mongodb 
  3. PATH=$MONGO_HOME/bin:$PATH 
  4. # 然后保存退出,执行如下命令立即生效 
  5. source /etc/profile 

4、关闭mongodb命令

mongod -f mongodb.conf --shutdown # 关闭服务器service stop mongod # 关闭服务器(需要添加服务)

四、nginx相关

1、Centos7环境下安装nginx

  1. # 创建nginx目录 
  2. mkdir /usr/local/nginx  
  3. #切换到nginx目录 
  4. cd /usr/local/nginx/  
  5. #下载nginx包 
  6. wget http://nginx.org/download/nginx-1.17.3.tar.gz 
  7. #解压nginx压缩包 
  8. tar -zxvf nginx-1.17.3.tar.gz 
  9. #切换到解压目录 
  10. cd nginx-1.17.3/ 
  11. #指定nginx安装路径 
  12. # ./configure --prefix=/usr/local/nginx 
  13. # 注意:出现错误执行命令: 
  14. yum -y install gcc gcc-c++ autoconf automake make 
  15. #出现"./configure: error: the HTTP rewrite module requires the PCRE "的错误,需要安装openssl 
  16. yum -y install openssl openssl-devel 
  17. #编译nginx安装包 
  18. make 
  19. #安装nginx 
  20. make install  

2、nginx常用启动/停止命令

  1. #启动  
  2. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
  3. #重启nginx 
  4. nginx -s reload 
  5. #重新打开日志文件 
  6. nginx -s reopen 
  7. #检查nginx配置文件是否正确  
  8. nginx -t -c /usr/local/nginx/conf/nginx.conf 
  9. #快速停止nginx 
  10. nginx -s stop 
  11. #平稳停止nginx 
  12. nginx -s quit 

相关内容