Blog

  • 制作MariaDB5.5.31的rpm包

    制作MariaDB的rpm包使用CMake CPack Package Generators,这种制作出来的包叫做CPackRPM包。

    首先安装好环境(必须全部检查):

    bzr > 2.0
    gunzip
    GNU tar
    gcc 2.95.2 or later
    g++
    GNU make 3.75 or later
    libtool 1.5.24 or later
    bison (2.0 for MariaDB 5.5)
    libncurses (在centos下名为ncurses-devel)
    zlib-dev
    GNU automake
    GNU autoconf
    cmake >= 2.8.7 (在centos下名为cmake28)

    使用非root用户,将下载好的mariadb-5.5.31.tar.gz解压,这里是 /home/jpuyy/mariadb-5.5.31。

    将要编译的参数写到cmake后面,这里是最关键的是制作rpm包的参数-DRPM=centos6,语句的最后要有一个点号,表示在当前目录生成。

    cmake28 -DRPM=centos6 \
    -DCMAKE_INSTALL_PREFIX=/usr/local/DBServer/MariaDB5.5.31 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_EXTRA_CHARSETS=complex .

    当最后几行是如下信息时,表示检查编译通过

    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/jpuyy/mariadb-5.5.31

    打包

    make package

    连spec文件都不用写,cmake会自动生成制作rpm的环境,位置在/home/jpuyy/mariadb-5.5.31/_CPack_Packages/Linux/RPM

    最后生成的rpm包如下

    MariaDB-5.5.31-centos6-x86_64-client.rpm
    MariaDB-5.5.31-centos6-x86_64-common.rpm
    MariaDB-5.5.31-centos6-x86_64-devel.rpm
    MariaDB-5.5.31-centos6-x86_64-server.rpm
    MariaDB-5.5.31-centos6-x86_64-shared.rpm
    MariaDB-5.5.31-centos6-x86_64-test.rpm

    这些包的作用

    https://kb.askmonty.org/en/about-the-mariadb-rpm-files/

    安装方法:

    需要解决冲突和依赖

    冲突:查找已经安装的mysql包,rpm -qa ‘mysql*’,移除查找到的mysql包

    rpm -e mysql-libs-5.1.66-2.el6_3.x86_64 --nodeps
    rpm -e `rpm -qa mysql-libs*` --nodeps
    

    依赖:在执行rpm -ivh MariaDB-5.5.31-centos6-x86_64*时,如果出现依赖,如

    error: Failed dependencies:
    perl(DBI) is needed by MariaDB-server-5.5.31-1.x86_64
    perl(Time::HiRes) is needed by MariaDB-test-5.5.31-1.x86_64

    可以yum安装perl-DBI,perl-Time-HiRes。也可以使用

    rpm -ivh MariaDB-5.5.31-centos6-x86_64-* --nodeps

    不去检测依赖。PS:有些依赖确实无用可不用安装。

    参考:

    https://kb.askmonty.org/en/source-building-mariadb-on-centos/

    https://kb.askmonty.org/en/installing-mariadb-with-the-rpm-tool/

  • 查看路由与添加删除静态路由

    静态路由可以细化,定制网络运行方式。很多时候网络走不通,也要加静态路由,指定包的转发。

    显示当前的路由表

    root@agent-test:~# ip route show
    default via 192.168.198.2 dev eth0 
    192.168.198.0/24 dev eth0  proto kernel  scope link  src 192.168.198.137

    内核的路由表

    root@agent-test:~# route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         localhost       0.0.0.0         UG    0      0        0 eth0
    192.168.198.0   *               255.255.255.0   U     0      0        0 eth0

    内核的路由表,全部使用数字方式显示

    root@agent-test:~# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.198.2   0.0.0.0         UG    0      0        0 eth0
    192.168.198.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0

    添加路由,格式为 ip route add {目标网络} via {ip地址} dev {设备},如192.168.3.0/24的网络从192.168.1.254走

    ip route add 192.168.3.0/24 via 192.168.1.254 dev eth0

    旧命令格式为

    route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth1

    以上命令立即生效,重启失效。永久静态路由

    如CentOS下

    #vi /etc/sysconfig/network-scripts/route-eth0

    既可以如下添加一行

    10.0.0.0/8 via 10.9.38.65

    也可以按这种格式

    ADDRESS0=192.168.0.62
    NETMASK0=255.255.255.255
    GATEWAY0=192.168.8.51
    ADDRESS1=192.168.0.71
    NETMASK1=255.255.255.255
    GATEWAY1=192.168.8.51
    ADDRESS2=192.168.1.0
    NETMASK2=255.255.255.0
    GATEWAY2=192.168.8.28

    重启网络后生效

    在Debian下,找到对应的interface,编辑/etc/network/interface

    auto eth0
    iface eth0 inet static
    address 10.9.38.76
    netmask 255.255.255.240
    network 10.9.38.64
    broadcast 10.9.38.79
    ### static routing ###
    post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65
    pre-down route del -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65

    在启动eth0时,添加一条路由,关闭时删掉对应路由即可。

    除了路由的添加删除,可以使用ip route replace改变已有路由的属性。如

    ip route replace default via 192.168.8.33 dev eth0

    关于默认路由,在CentOS中,既可以在/etc/sysconfig/networ-scripts/ifcfg-eth0中指定gateway,也可以在/etc/sysconfig/network中指定gateway,也可以在上面的ip route add default via 192.168.8.1 dev eth0。

  • dd创建大文件制作iso启动盘

    生成一个2GB的文件
    dd if=/dev/zero of=test bs=1024k count=2048
    生成一个10GB的文件
    dd if=/dev/zero of=test bs=1024k count=10240

    dd if=/dev/zero of=test bs=8M count=128

    mac下用dd制作启动u盘

    确定盘符

    ➜  quick diskutil list

    /dev/disk0

    #:                       TYPE NAME                    SIZE       IDENTIFIER

    0:      GUID_partition_scheme                        *121.3 GB   disk0

    1:                        EFI EFI                     209.7 MB   disk0s1

    2:                  Apple_HFS mac                     120.5 GB   disk0s2

    3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

    /dev/disk1

    #:                       TYPE NAME                    SIZE       IDENTIFIER

    0:      GUID_partition_scheme                        *500.1 GB   disk1

    1:                        EFI EFI                     209.7 MB   disk1s1

    2:                  Apple_HFS 500G                    499.8 GB   disk1s2

    /dev/disk2

    #:                       TYPE NAME                    SIZE       IDENTIFIER

    0:     FDisk_partition_scheme                        *7.7 GB     disk2

    1:               Windows_NTFS 21                      7.7 GB     disk2s1

    去掉挂载

    ➜  quick diskutil umountDisk /dev/disk2

    Unmount of all volumes on disk2 was successful

    用dd写入光盘镜像

    ➜  quick sudo dd if=/Users/jpuyy/Downloads/CentOS-7.0-1406-x86_64-GnomeLive.iso of=/dev/disk2 bs=1m

  • C2960S交换机汇聚

    这里的汇聚要达到的目的:交换机之间可以扩展带宽,同时增强可用性。C2960S是二层交换机。实现汇聚的两个协议

    Link Aggregation Control Protocol (LACP) 和 Port Aggregation Protocol (PAgP)

    LACP是IEEE 802.3ad中规定的协议,所以具有通用性,所以linux又网卡绑定时,需要选择Mode 4 (802.3ad),这样交换机与服务器之间就能增加带宽。

    而PAgP是思科自己的汇聚协议,它适用于思科产品之间的连接。

    一句话,服务器与交换机,只能用LACP才有汇聚效果,而交换机与交换机既可以是LACP,也可以是PAgP,同时要注意,互相连接的端口之间协议要统一。不管是LACP还是PAgP,组成的新的虚拟通道在思科里叫做etherchannel。

    下面是将1/0/1与1/0/2汇聚,前提进入enable模式,

    配置LACP

    Switch#configure terminal
    Switch(config)#interface range gigabitEthernet 1/0/1 - 2
    Switch(config-if-range)#switchport mode trunk
    Switch(config-if-range)#channel-group 1 mode active
    Switch(config-if-range)#end

    配置PAgP

    Switch#configure terminal
    Switch(config)#interface range gigabitEthernet 1/0/1 - 2
    Switch(config-if-range)#switchport mode trunk
    Switch(config-if-range)#channel-group 1 mode desirable
    Switch(config-if-range)#end

    几个有用的查看命令

    show running-config查看当前配置信息

    interface GigabitEthernet1/0/1
     switchport mode trunk
     spanning-tree portfast
     channel-group 1 mode active
     !
     interface GigabitEthernet1/0/2
     switchport mode trunk
     spanning-tree portfast
     channel-group 1 mode active

    show ip interfaces brief 显示interfaces的状态

    Port-channel1          unassigned      YES unset  up               up

    show spanning-tree 查看生成树,如果汇聚成功会只显示

    Po1                 Desg FWD 3         128.224  P2p

    更改port-channel的负载均衡的方式为src-mac

    Switch#configure terminal
    Switch(config)#port-channel load-balance src-mac

    可选的方式有:

    dst-ip Dst IP Addr
    dst-mac Dst Mac Addr
    src-dst-ip Src XOR Dst IP Addr
    src-dst-mac Src XOR Dst Mac Addr
    src-ip Src IP Addr
    src-mac Src Mac Addr

    查看etherchannel负载均衡的方式:

    Switch#show etherchannel load-balance
    EtherChannel Load-Balancing Configuration:
    src-mac

    查看interfaces的trunk状态

    Switch#show interfaces trunk

    Port Mode Encapsulation Status Native vlan
     Po1 on 802.1q trunking 1
     Po3 on 802.1q trunking 1
     Po4 on 802.1q trunking 1
    Port Vlans allowed on trunk
     Po1 1-4094
     Po3 1-4094
     Po4 1-4094
    Port Vlans allowed and active in management domain
     Po1 1
     Po3 1
     Po4 1
    Port Vlans in spanning tree forwarding state and not pruned
     Po1 1
     Po3 1
     Po4 1

    查看ether channel状态

    Switch#show etherchannel summary
    
    Flags: D - down P - bundled in port-channel
    I - stand-alone s - suspended
    H - Hot-standby (LACP only)
    R - Layer3 S - Layer2
    U - in use f - failed to allocate aggregator
    
    M - not in use, minimum links not met
    u - unsuitable for bundling
    w - waiting to be aggregated
    d - default port
    Number of channel-groups in use: 4
    Number of aggregators: 4
    
    Group Port-channel Protocol Ports
    ------+-------------+-----------+--------------------------------------------
    1 Po1(SU) PAgP Gi1/0/1(P) Gi1/0/2(P)
    2 Po2(SD) LACP Gi1/0/3(D) Gi1/0/4(D)
    3 Po3(SU) LACP Gi1/0/5(P) Gi1/0/6(P)
    4 Po4(SD) LACP Gi1/0/7(D) Gi1/0/8(D)

    可以看到正在运行的是第一个PAgP,第3LACP

    参考:

    http://www.cisco.com/en/US/docs/switches/lan/catalyst2960/software/release/12.2_55_se/configuration/guide/swethchl.html#wpxref12539

    服务器 bond0  mode0 交换机做 etherchannel 使交换机 show logging 没有 flapping.

    在 configure terminal 下,做好 port-channel

    switch(config)#interface port-channel 2
    switch(config-if)#switchport mode access
    switch(config-if)#switchport access vlan 6

    将 gi 口加入到 port-channel,这里用的 mode 是 on

    switch(config-if)#int range gi 1/0/10 - 13
    switch(config-if-range)#channel-group 2 mode on

    默认是 no shutdown ,这样以后会有几十秒的中断,然后查看 etherchannel

    2 Po2(SU) - Gi1/0/10(P) Gi1/0/11(P) Gi1/0/12(P)
     Gi1/0/13(P)
  • shell判断文件,目录是否存在或者具有权限

    #!/bin/sh

    myPath=”/var/log/httpd/”
    myFile=”/var/log/httpd/access.log”

    #这里的-x 参数判断$myPath是否存在并且是否具有可执行权限

     if [ ! -x "$myPath"]; then
     mkdir "$myPath"
     fi

    #这里的-d 参数判断$myPath是否存在

     if [ ! -d "$myPath"]; then
     mkdir "$myPath"
     fi

    #这里的-f参数判断$myFile是否存在

     if [ ! -f "$myFile" ]; then
     touch "$myFile"
     fi

    #其他参数还有-n,-n是判断一个变量是否是否有值

     if [ ! -n "$myVar" ]; then
     echo "$myVar is empty"
     exit 0
     fi

    #两个变量判断是否相等

     if [ "$var1" = "$var2" ]; then
     echo '$var1 eq $var2'
     else
     echo '$var1 not eq $var2'
     fi

    判断一个变量是不是纯数字

    if [[ $object_id =~ ^[0-9]+$ ]]; then
        echo "$object_id is a number"
    fi
  • mysql绑定多个ip地址

    my.cnf中有选项bind-address=127.0.0.1,是说mysql server监听的是本地发来的请求,如果是任意主机都可以请求,则写为0.0.0.0,但是这样又不太安全。监听某ip,指定此ip地址即可,但是要保证mysql的user中有允许此ip访问,否则不能对数据库操作。那么是否可以在配置里只规定几个ip呢?

    简单直接回答:不可能

    请参考:http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_bind-address

    The MySQL server listens on a single network socket for TCP/IP connections. This socket is bound to a single address, but it is possible for an address to map onto multiple network interfaces. The default address is 0.0.0.0. To specify an address explicitly, use the –bind-address=addr option at server startup, where addr is an IPv4 address or a host name. If addr is a host name, the server resolves the name to an IPv4 address and binds to that address. The server treats different types of addresses as follows:

    If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.
    If the address is a “regular” IPv4 address (such as 127.0.0.1), the server accepts TCP/IP connections only for that particular IPv4 address.

    但是有此需求,就会到访问控制,那么使用防火墙iptables可实现此效果

    mysql-server为192.168.1.3,只允许192.168.1.4,  192.168.1.5,  192.168.1.6来访问3306端口

    在my.cnf中

    bind-address = 0.0.0.0

    在访问3306端口的主机中,只允许192.168.1.4-6,其他ip一律DROP掉

    /sbin/iptables -A INPUT -p tcp -s 192.168.1.4 --dport 3306 -j ACCEPT
    /sbin/iptables -A INPUT -p tcp -s 192.168.1.5 --dport 3306 -j ACCEPT
    /sbin/iptables -A INPUT -p tcp -s 192.168.1.6 --dport 3306 -j ACCEPT
    /sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP

    /sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.4 -j DROP
    /sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.5 -j DROP
    /sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.6 -j DROP

    保存防火墙规则

    service iptables save

    查看INPUT链包含3306的规则

    echo -e "target prot opt source destination\n$(iptables -L INPUT -n | grep 3306)"

    这样就实现了mysql只允许指定ip访问。

    参考:

    http://www.cyberciti.biz/faq/unix-linux-mysqld-server-bind-to-more-than-one-ip-address/