linux在DB运维时常用的命令有哪些

46次阅读
没有评论

共计 12131 个字符,预计需要花费 31 分钟才能阅读完成。

这篇文章主要介绍了 linux 在 DB 运维时常用的命令有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让丸趣 TV 小编带着大家一起了解一下。

# cat /etc/issue

CentOS release 6.5 (Final)

Kernel \r on an \m

我使用的测试机是 CentOS 6.5,目前比较流行的 linux 发行版本有:

Red Hat   http://www.redhat.com (目前企业版最佳版本,不过商业服务收费,对操作系统安全负责)

Fedora    http://fedoraproject.org (Red Hat 的开发版本,更新速度快)

Mandriva  http://mandriva.com

Novell SuSE     http://novell.com/linux

Debian          http://debian.org

Slackware       http://slackware.com

Gentoo          http://gentoo.org (性能较好)

Ubuntu          http://ubuntu.com (基于 Debian)

CentOS          http://centos.org (免费服务器版本,意思就是专业盗版 Red Hat,记住专业)

Ubuntu Kylin    http://www.ubuntukylin.com (国产版本, 基于 Ubuntu,现在 16.04 貌似很友好)

Deepin         https://www.deepin.org/ (国内第一个基于 Debian 的系统,社区支持相当友好发布了很多国内软件的 linux 版本,Ubuntu、Ubuntu Kylin 都是其受益者,这个情操给满分)

其次,我们肯定要看看 CPU、硬盘大小、内存这些硬件信息了啥

查看 CPU 信息(型号)

# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

4  Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHz          E5410   @ 2.33GHz

(看到有 8 个逻辑 CPU, 也知道了 CPU 型号)

# cat /proc/cpuinfo | grep physical | uniq -c

4 physical id : 0 

4 physical id : 1 

(说明实际上是两颗 4 核的 CPU)

# getconf LONG_BIT

64

(说明当前 CPU 运行在 32bit 模式下, 但不代表 CPU 不支持 64bit)

# cat /proc/cpuinfo | grep flags | grep lm | wc -l

4

(结果大于 0, 说明支持 64bit 计算. lm 指 long mode, 支持 lm 则是 64bit)

查看机器型号  
# dmidecode | grep Product Name

查看网卡信息  
# dmesg | grep -i eth

# df -lh
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   50G  7.2G   40G  16% /
tmpfs                         1.9G   68K  1.9G   1% /dev/shm
/dev/sda1                     485M   40M  421M   9% /boot
/dev/mapper/VolGroup-lv_home   49G  7.8G   39G  17% /home

目前市面上 CPU 基本都是 x86 和 PowerPC 吧,特定机器可能会使用 Sun 公司的 SPARC 系列。
CPU 内部含有微指令集,不同的微指令集会导致 CPU 工作效率的优势
CPU 的频率:CPU 每秒钟可以进行的工作次数
CPU 频率 3.0G 表示这个 CPU 在一秒内可以进行 3.0*10^9 次工作
所以主频高的 CPU 性能不一定就好,还要看微指令集。

机器到手,最后最重要肯定是要有网络啥,不然玩不转呀。

ifconfig

eth0      Link encap:Ethernet  HWaddr 00:50:56:B3:6B:6D

 inet addr:172.15.11.73  Bcast:172.15.11.255 Mask:255.255.255.0

 inet6 addr: fe80::250:56ff:feb3:6b6d/64 Scope:Link

 UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

 RX packets:40487206 errors:0 dropped:0 overruns:0 frame:0

 TX packets:791120 errors:0 dropped:0 overruns:0 carrier:0

 collisions:0 txqueuelen:1000

 RX bytes:5193260758 (4.8 GiB) TX bytes:296331233 (282.6 MiB)

linux 设置静态 IP

cd /etc/sysconfig/network-scripts/

#vim ifcfg-eth0

DEVICE=eth0

HWADDR=00:50:56:B0:07:B6

TYPE=Ethernet

UUID=ceb36fa0-487e-4f8a-bba1-9f94728086e5

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.6.203

NETMASK=255.255.255.0

GATEWAY=192.168.6.2

DNS1=192.168.0.5

linux 卸载多余启动项
第一种方法
1. 首先列出系统中正在使用的内核: 
# uname -a 
2. 查询系统中全部的内核: 
# rpm -qa | grep kernel 
3. 将你想删除的内核删除掉:(例如, 在我的系统中, 我要删掉 kernel-2.6.32-279.9.1.el6.x86_64 的内核, 需要把所有含有 kernel-2.6.32-279.9.1.el6.x86_64 字样的全部删掉) 
#yum remove kernel-2.6.32-279.9.1.el6.x86_64 
4. 重启后就可以看到, 内核被删掉了, 同时多余的启动项也自动被删掉了。 
第二种方法  
手动修改 /boot/grub/menu.lst   把多余的项删除。

linux 刷新内存
清除页面缓存

# sync; echo 1 /proc/sys/vm/drop_caches

清除目录项和 inode  

# sync; echo 2 /proc/sys/vm/drop_cachesc 

清除页面换粗 目录项和 inode

# sync; echo 3 /proc/sys/vm/drop_caches

swap 清理

swapoff -a swapon -a

干完上面的事,就来说我们要来完成的编译安装 percona 的任务了
首先是下载到最新的安装包

#cd /home

# wget http://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

# wget xxxx.cmake-3.3.2.tar.gz

# wget xxxx.percona-server-5.7.13-6.tar.gz

# wget xxxx/lib_mysqludf_sys-master.zip

下载这么多安装,说明一下
MySQL currently requires boost_1_59_0
CMake 2.8.2 higher
这两个是编译 percona-server-5.7.13- 6 的硬性要求,不然不给编译
CMake 和 boost_1_59_0 安装编译都是很简单
CMake 新的发行版本可能已经很高了,编译安装之前可以先查看一下,比 CMake 2.8.2 更新的版本可以不用安装也行了。

# cmake –version

cmake version 2.8.12.2

CMake:

# ./bootstrap

# make make install

boost_1_59_0:

#tar -zxvf boost_1_59_0.tar.gz

#mv  boost_1_59_0  /usr/local/boost

# cd  /usr/local/boost

# ./bootstrap.sh

# ./b2

boost_1_59_0 这个很伤,这个包好像很大,不过官方版本就集成 boost_1_59_0 的源码,percona 好像还没。
照着前面写的步骤,然后慢慢的等就得了。这个编译坑,没个进度条。
gcc.compile.c++ bin.v2/libs/wave/build/gcc-4.4.7/release/link-static/threading-multi/cpplexer/re2clex/cpp_re.o
gcc.archive bin.v2/libs/wave/build/gcc-4.4.7/release/link-static/threading-multi/libboost_wave.a
common.copy stage/lib/libboost_wave.a
…failed updating 56 targets…
…skipped 6 targets…
…updated 1069 targets…
当完成到这一步就证明已经完成了 boost_1_59_0 的安装,这些都是准备工作。
现在来正式开始安装 percona-server-5.7.13-6
①Set timezone(设置服务器的时区:亚洲上海)

#Set timezone(设置服务器的时区:亚洲上海)

rm -rf /etc/localtime

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime(这个是美帝时间额)

# ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
# date
Tue Aug  9 02:11:37 EDT 2016
# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# date
Tue Aug  9 14:11:47 CST 2016

②Remove MySQL if exists mysql (删除服务器上已存在 MySQL, 切记数据库备份)
service mysql stop
rpm -qa|grep mysql
rpm -e mysql
yum -y remove mysql-server mysql mysql-libs
因为很多机器到手的时候可能被 yum 安装了数据库,这个的提前处理一下
③常用依赖安装
centos:
yum -y install patch make cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap diffutils
debian:
yum -y install build-essential gcc g++ make cmake autoconf automake re2c wget cron bzip2 libzip-dev libc6-dev file rcconf flex vim nano bison m4 gawk less make cpp binutils diffutils unzip tar bzip2 libbz2-dev libncurses5 libncurses5-dev libtool libevent-dev libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlibc openssl libsasl2-dev libxml2 libxml2-dev libltdl3-dev libltdl-dev libmcrypt-dev zlib1g zlib1g-dev libbz2-1.0 libbz2-dev libglib2.0-0 libglib2.0-dev libpng3 libfreetype6 libfreetype6-dev libjpeg62 libjpeg62-dev libjpeg-dev libpng-dev libpng12-0 libpng12-dev curl libcurl3 libmhash3 libmhash-dev libpq-dev libpq5 gettext libncurses5-dev libcurl4-gnutls-dev libjpeg-dev libpng12-dev libxml2-dev zlib1g-dev libfreetype6 libfreetype6-dev libssl-dev libcurl3 libcurl4-openssl-dev libcurl4-gnutls-dev mcrypt libcap-dev diffutils ca-certificates debian-keyring debian-archive-keyring
ubuntu:
apt-get -yf install build-essential gcc g++ make cmake automake autoconf re2c wget cron bzip2 libzip-dev libc6-dev file rcconf flex vim nano bison m4 gawk less make cpp binutils diffutils unzip tar bzip2 libbz2-dev unrar p7zip libncurses5-dev libncurses5 libncurses5-dev libncurses5-dev libtool libevent-dev libpcre3 libpcre3-dev libpcrecpp0  libssl-dev zlibc openssl libsasl2-dev libltdl3-dev libltdl-dev libmcrypt-dev zlib1g zlib1g-dev libbz2-1.0 libbz2-dev libglib2.0-0 libglib2.0-dev libpng3 libjpeg62 libjpeg62-dev libjpeg-dev libpng-dev libpng12-0 libpng12-dev curl libcurl3 libmhash3 libmhash-dev libpq-dev libpq5 gettext libncurses5-dev libcurl4-gnutls-dev libjpeg-dev libpng12-dev libxml2-dev zlib1g-dev libfreetype6 libfreetype6-dev libssl-dev libcurl3 libcurl4-openssl-dev libcurl4-gnutls-dev mcrypt libcap-dev diffutils ca-certificates debian-keyring debian-archive-keyring

依赖很多,不一定每个都会用到,不过也没有就当更新一下软件吧
④MySQL 账户创建
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql

⑤创建 MySQL 数据库存放目录, 日志目录也要单独给磁盘这样 I / O 分开压力会小一些,这里就放一起了
mkdir -p /home/database
chown -R mysql:mysql /home/database

⑥预编译 camke
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  -DSYSCONFDIR=/etc \
  -DENABLED_PROFILING=1 \
  -DMYSQL_DATADIR=/home/database \
  -DMYSQL_TCP_PORT=3306 \
  -DWITH_MYISAM_STORAGE_ENGINE=1 \
  -DWITH_PARTITION_STORAGE_ENGINE=1 \
  -DWITH_CSV_STORAGE_ENGINE=1 \
  -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  -DWITH_EXTRA_CHARSETS=gbk,gb2312,utf8,ascii,utf8mb4 \
  -DDEFAULT_COLLATION=utf8_general_ci \
  -DDEFAULT_CHARSET=utf8 \
  -DENABLED_LOCAL_INFILE=1 \
  -DWITH_EDITLINE=bundled \
  -DWITH_ZLIB=system \
  -DWITH_BOOST=/usr/local/boost

如果之前有出错先清理错误编译日志

make clean

rm CMakeCache.txt

完成这个步骤后,再 cmake

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

 -DSYSCONFDIR=/etc \

 -DENABLED_PROFILING=1 \

 -DMYSQL_DATADIR=/home/database \

 -DMYSQL_TCP_PORT=3306 \

 -DWITH_MYISAM_STORAGE_ENGINE=1 \

 -DWITH_PARTITION_STORAGE_ENGINE=1 \

 -DWITH_CSV_STORAGE_ENGINE=1 \

 -DWITH_INNOBASE_STORAGE_ENGINE=1 \

 -DWITH_EXTRA_CHARSETS=gbk,gb2312,utf8,ascii,utf8mb4 \

 -DDEFAULT_COLLATION=utf8_general_ci \

 -DDEFAULT_CHARSET=utf8 \

 -DENABLED_LOCAL_INFILE=1 \

 -DWITH_EDITLINE=bundled \

 -DWITH_ZLIB=system \

 -DWITH_BOOST=/usr/local/boost

— INSTALL perconaserverclient.pc lib/pkgconfig

— CMAKE_BUILD_TYPE: RelWithDebInfo

— COMPILE_DEFINITIONS: _GNU_SOURCE;_FILE_OFFSET_BITS=64;HAVE_CONFIG_H

— CMAKE_C_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement

— CMAKE_CXX_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter

— CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -D_FORTIFY_SOURCE=2 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF

— CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -D_FORTIFY_SOURCE=2 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF

— Configuring done

— Generating done

— Build files have been written to: /home/percona-server-5.7.13-6

⑦编译
开始编译安装
make -j`grep processor /proc/cpuinfo | wc -l` make install
这个 100% 安装完成后
cp support-files/my-default.cnf /etc/my.cnf
ln -sf /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -sf /usr/local/mysql/include/mysql /usr/include/mysql
ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
这里搞这么多链接,也是为了不搞环境变量
cp support-files/mysql.server /etc/init.d/mysql
chmod 700 /etc/init.d/mysql
chkconfig –add mysql 
chkconfig –level 35 mysql on
进去修改一下这两个,其余的就是配置文件的事情了,这里暂时不讨论
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /home/database
# port = …..
# server_id = …..
# socket = …..
⑧初始化数据库,并启动数据库修改掉随机密码
安装完成后,初始化数据库

[root@DB-73 mysql]# rm -rf /home/database/*

[root@DB-73 mysql]# /usr/local/mysql/bin/mysqld –initialize –user=mysql –basedir=/usr/local/mysql –datadir=/home/database

2016-08-05T07:21:48.113959Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).

2016-08-05T07:21:48.114054Z 0 [Warning] NO_ZERO_DATE , NO_ZERO_IN_DATE and ERROR_FOR_DIVISION_BY_ZERO sql modes should be used with strict mode. They will be merged with strict mode in a future release.

2016-08-05T07:21:48.114066Z 0 [Warning] NO_AUTO_CREATE_USER sql mode was not set.

2016-08-05T07:21:53.194657Z 0 [Warning] InnoDB: New log files created, LSN=45790

2016-08-05T07:21:54.195562Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2016-08-05T07:21:54.469438Z 0 [Warning] No existing UUID has been found, so we assume that this is the

first time that this server has been started. Generating a new UUID: 4941b58f-5add-11e6-8c4c-005056b36b6d.

2016-08-05T07:21:54.500735Z 0 [Warning] Gtid table is not ready to be used. Table mysql.gtid_executed cannot be opened.

2016-08-05T07:21:54.502227Z 1 [Note] A temporary password is generated for root@localhost: aXx!

# /etc/init.d/mysql start
Starting MySQL (Percona Server).. [OK]
# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.13-6
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, 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 use mysql;
mysql alter user root @ localhost identified by jiayifei
Query OK, 0 rows affected (0.00 sec)
mysql flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql quit
⑨加装 UDF
# cat lib_mysqludf_sys-master/lib_mysqludf_sys.sql

[root@DB-73 lib_mysqludf_sys-master]# gcc -DMYSQL_DYNAMIC_PLUGIN -fPIC -Wall -I/usr/local/mysql/include -I. -shared lib_mysqludf_sys.c -o /usr/local/mysql/lib/mysql/plugin/lib_mysqludf_sys.so

[root@DB-73 lib_mysqludf_sys-master]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.7.13-6 Source distribution

Copyright (c) 2009-2016 Percona LLC and/or its affiliates

Copyright (c) 2000, 2016, 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 DROP FUNCTION IF EXISTS lib_mysqludf_sys_info;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql DROP FUNCTION IF EXISTS sys_get;

Query OK, 0 rows affected, 1 warning (0.00 sec)

……

⑩# 添加防火墙
防火墙的 3306 端口默认没有开启,若要远程访问,需要开启这个端口
打开 /etc/sysconfig/iptables
在“-A INPUT –m state –state NEW –m tcp –p –dport 22 –j ACCEPT”,下添加:
-A INPUT -m state –state NEW -m tcp -p -dport 3306 -j ACCEPT
然后保存,并关闭该文件,在终端内运行下面的命令,刷新防火墙配置:
service iptables restart CentOS 7 中默认使用 Firewalld 做防火墙,所以修改 iptables 后,在重启系统后,根本不管用。
Firewalld 中添加端口方法如下:
firewall-cmd –zone=public –add-port=3306/tcp –permanent
firewall-cmd –reload

感谢你能够认真阅读完这篇文章,希望丸趣 TV 小编分享的“linux 在 DB 运维时常用的命令有哪些”这篇文章对大家有帮助,同时也希望大家多多支持丸趣 TV,关注丸趣 TV 行业资讯频道,更多相关知识等着你来学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-27发表,共计12131字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)