分类目录归档:shell脚本

mysql-5.7在centos7中按systemd启动,手册中说明

The following discussion covers these topics:

Overview of systemd

Configuring MySQL Using systemd

Configuring Multiple MySQL Instances Using systemd

Migrating from mysqld_safe to systemd

Overview of systemd

systemd provides automatic server startup and shutdown. It also enables manual server management using the systemctl command. For example:

systemctl {start|stop|restart|status} mysqld
Alternatively, use the service command (with the arguments reversed), which is compatible with System V systems:

service mysqld {start|stop|restart|status}
For the systemctl or service commands, if the MySQL service name is not mysqld, use the appropriate name (for example, mysql on SLES systems).

Support for systemd includes these files:

mysqld.service: systemd service unit configuration, with details about the mysqld service.

mysqld@.service: Like mysqld.service, but used for managing multiple MySQL instances.

mysqld.tmpfiles.d: File containing information to support the tmpfiles feature. This file is installed under the name mysql.conf.

mysqld_pre_systemd: Support script for the unit file. This script assists in creating the error log file only if its location matches the pattern /var/log/mysql*.log. In other cases, the error log directory must be writable or the error log must be present and writable for the user running the mysqld process.

On platforms for which systemd support is installed, scripts such as mysqld_safe and the System V initialization script are not installed because they are unnecessary. For example, mysqld_safe can handle server restarts, but systemd provides the same capability, and does so in a manner consistent with management of other services rather than using an application-specific program.

As of MySQL 5.7.13, on platforms for which systemd support is installed, systemd has the capability of managing multiple MySQL instances. For details, see Configuring Multiple MySQL Instances Using systemd. Consequently, mysqld_multi and mysqld_multi.server are not installed because they are unnecessary.

Configuring MySQL Using systemd

To add or change systemd options for MySQL, these methods are available:

Use a localized systemd configuration file.

Arrange for systemd to set environment variables for the MySQL server process.

Set the MYSQLD_OPTS systemd variable.

To use a localized systemd configuration file, create the /etc/systemd/system/mysqld.service.d directory if it does not exist. In that directory, create a file that contains a [Service] section listing the desired settings. For example:

[Service]
LimitNOFILE=max_open_files
PIDFile=/path/to/pid/file
Nice=nice_level
LimitCore=core_file_limit
Environment=”LD_PRELOAD=/path/to/malloc/library”
Environment=”TZ=time_zone_setting”
The discussion here uses override.conf as the name of this file. Newer versions of systemd support the following command, which opens an editor and permits you to edit the file:

systemctl edit mysqld
Whenever you create or change override.conf, reload the systemd configuration, then tell systemd to restart the MySQL service:

systemctl daemon-reload
systemctl restart mysqld
Support for configuration using override.conf was added in MySQL 5.7.7.

With systemd, the override.conf configuration method must be used for certain parameters, rather than settings in a [mysqld_safe] or [mysqld] group in a MySQL option file:

For some parameters, override.conf must be used because systemd itself must know their values and it cannot read MySQL option files to get them.

Parameters that specify values otherwise settable only using options known to mysqld_safe must be specified using systemd because there is no corresponding mysqld parameter.

For additional information about using systemd rather than mysqld_safe, see Migrating from mysqld_safe to systemd.

You can set the following parameters in override.conf:

To specify the process ID file:

As of MySQL 5.7.10: Use override.conf and change both PIDFile and ExecStart to name the PID file path name. Any setting of the process ID file in MySQL option files will be ignored.

Before MySQL 5.7.10: Use PIDFile in override.conf rather than the –pid-file option for mysqld_safe or mysqld. systemd must know the PID file location so that it can restart or stop the server. If the PID file value is specified in a MySQL option file, the value must match the PIDFile value or MySQL startup may fail.

To set the number of file descriptors available to the MySQL server, use LimitNOFILE in override.conf rather than the –open-files-limit option for mysqld_safe or mysqld.

To set the maximum core file size, use LimitCore in override.conf rather than the –core-file-size option for mysqld_safe.

To set the scheduling priority for the MySQL server, use Nice in override.conf rather than the –nice option for mysqld_safe.

Some MySQL parameters are configured using environment variables:

LD_PRELOAD: Set this variable if the MySQL server should use a specific memory-allocation library.

TZ: Set this variable to specify the default time zone for the server.

There are multiple ways to specify the value of environment values that should be in effect for the MySQL server process managed by systemd:

Use Environment lines in the override.conf file. For the syntax, see the example in the preceding discussion that describes how to use this file.

Specify the values in the /etc/sysconfig/mysql file (create the file if it does not exist). Assign values using the following syntax:

LD_PRELOAD=/path/to/malloc/library
TZ=time_zone_setting
After modifying /etc/sysconfig/mysql, restart the server to make the changes effective:

systemctl restart mysqld
To specify options for mysqld without modifying systemd configuration files directly, set or unset the MYSQLD_OPTS systemd variable. For example:

systemctl set-environment MYSQLD_OPTS=”–general_log=1″
systemctl unset-environment MYSQLD_OPTS
After modifying the systemd environment, restart the server to make the changes effective:

systemctl restart mysqld
MYSQLD_OPTS can also be set in the /etc/sysconfig/mysql file.

Configuring Multiple MySQL Instances Using systemd

As of MySQL 5.7.13, on platforms for which systemd support is installed, systemd has the capability of managing multiple MySQL instances. Consequently, mysqld_multi and mysqld_multi.server are not installed because they are unnecessary.

To use multiple-instance capability, modify my.cnf to include configuration of key options for each instance. For example, to manage two instances named replica01 and replica02, add something like this to the file:

[mysqld@replica01]
datadir=/var/lib/mysql-replica01
socket=/var/lib/mysql-replica01/mysql.sock
port=3307
log-error=/var/log/mysqld-replica01.log

[mysqld@replica02]
datadir=/var/lib/mysql-replica02
socket=/var/lib/mysql-replica02/mysql.sock
port=3308
log-error=/var/log/mysqld-replica02.log
The replica names shown here use @ as the delimiter because that is the only delimiter supported by systemd.

Instances then are managed by normal systemd commands, such as:

systemctl start mysqld@replica01
systemctl start mysqld@replica02
To enable instances to run at boot time, do this:

systemctl enable mysqld@replica01
systemctl enable mysqld@replica02
Use of wildcards is also supported. For example, this command displays the status of all replica instances:

systemctl status ‘mysqld@replica*’
For management of multiple MySQL instances on the same machine, systemd automatically uses a different unit file (mysqld@.service rather than mysqld.service). In that unit file, %I and %i reference the parameter passed in after the @ marker and are used to manage the specific instance. For a command such as this:

systemctl start mysqld@mysql1
systemd starts the server using a command such as this:

mysqld –defaults-group-suffix=@%I …
The result is that the [server], [mysqld], and [mysqld@mysql1] option groups are read and used for that instance of the service.

Migrating from mysqld_safe to systemd

Because mysqld_safe is not installed when systemd is used, options previously specified for that program (for example, in an [mysqld_safe] option group) must be specified another way:

Some mysqld_safe options are also understood by mysqld and can be moved from the [mysqld_safe] option group to the [mysqld] group. This does not include –pid-file or –open-files-limit. To specify those options, use the override.conf systemd file, described previously.

For some mysqld_safe options, there are similar mysqld options. For example, the mysqld_safe option for enabling syslog logging is –syslog. For mysqld, enable the log_syslog system variable instead. For details, see Section 6.4.2, “The Error Log”.

mysqld_safe options not understood by mysqld can be specified in override.conf or environment variables. For example, with mysqld_safe, if the server should use a specific memory allocation library, this is specified using the –malloc-lib option. For installations that manage the server with systemd, arrange to set the LD_PRELOAD environment variable instead, as described previously.

shell的登陆交互

#!/bin/bash
# test.sh user pw
if [ ! $# -eq 2 ] ; then
echo “请输入用户名和密码以空格分开!”
exit

else
name=”$1″
passwd=”$2″
fi

cat hosts | while read hosts
do

echo “正在$hosts上用户$name”
expect <” {
send “enable\r” ;exp_continue
}
}
EOF
echo “成功建立”
done

sed单行脚本处理文本

————————————————————————-
SED单行脚本快速参考(Unix 流编辑器) 2005年12月29日

在以下地址可找到本文档的最新(英文)版本:
http://sed.sourceforge.net/sed1line.txt
http://www.pement.org/sed/sed1line.txt

其他语言版本:
中文 – http://sed.sourceforge.net/sed1line_zh-CN.html
捷克语 – http://sed.sourceforge.net/sed1line_cz.html
荷语 – http://sed.sourceforge.net/sed1line_nl.html
法语 – http://sed.sourceforge.net/sed1line_fr.html
德语 – http://sed.sourceforge.net/sed1line_de.html

葡语 – http://sed.sourceforge.net/sed1line_pt-BR.html

继续阅读

shell中的大括号,截取字符串

# load LVS IPVS modules
if [ -d /lib/modules/$unamer/kernel/net/ipv4/ipvs ]; then
for module in /lib/modules/$unamer/kernel/net/ipv4/ipvs/* ; do
module=${module##*/}
module=${module%.ko}
modprobe $module >/dev/null 2>&1
done
fi
就把/lib/modules/2.6.21-1.3194.fc7/kernel/net/ipv4/ipvs/下的所有模块都自动加载了。其中:
if语句检查ipvs模块的目录是否存在
for循环遍历该目录下面的所有文件
module=${module##*/} :其中##表示从前面删除字符,*/表示删除到最后一个/,如果一个#就表示只删除到第一个/。如果变量后面接##,表示在##后面的字符串取最长的(一直到最后面),如果接#,表示取最小的一段。
module=${module%.ko}:表示从后面删除.ko。如果变量后面接%%,表示在%%后面的字符串取最长的(一直到最前面),如果接%,表示取最小的一段。
这样多module的两次修改就得到了模块名,就是文件名不带路径和.ko后缀。

备注:
1、${}是用来进行变量替换的
2、shell十三问中的${file##*/}的说明
為了完整起見,我這裡再用一些例子加以說明 ${ } 的一些特異功能:
假設我們定義了一個變量為:
file=/dir1/dir2/dir3/my.file.txt
我們可以用 ${ } 分別替換獲得不同的值:
${file#*/}:拿掉第一條 / 及其左邊的字串:dir1/dir2/dir3/my.file.txt
${file##*/}:拿掉最後一條 / 及其左邊的字串:my.file.txt
${file#*.}:拿掉第一個 . 及其左邊的字串:file.txt
${file##*.}:拿掉最後一個 . 及其左邊的字串:txt
${file%/*}:拿掉最後條 / 及其右邊的字串:/dir1/dir2/dir3
${file%%/*}:拿掉第一條 / 及其右邊的字串:(空值)
${file%.*}:拿掉最後一個 . 及其右邊的字串:/dir1/dir2/dir3/my.file
${file%%.*}:拿掉第一個 . 及其右邊的字串:/dir1/dir2/dir3/my
記憶的方法為:

# 是去掉左邊(在鑑盤上 # 在 $ 之左邊)
% 是去掉右邊(在鑑盤上 % 在 $ 之右邊)
單一符號是最小匹配﹔兩個符號是最大匹配。

则module=ip_vs.ko,一个#表示从左边删除字符,*/表示删除到最后一个,##*/

sort+uniq计算重复行

1.对于重复行的不同处理:

sort和uniq都可以去除重复行,
sort是去除所有重复行
uniq是去除连续的重复行,中间不能隔和其他信息

2.uniq中的-c选项:打印每一重复行出现的次数,导致我们统计的时候会直接使用这一命令,而不用编写或者使用复杂的命令。
但是它对重复行的定义是连续的,所有我们就用到的sort来排序,使相同的行连续起来。再利用uniq统计数量。

这就是著名的”sort | uniq -c”统计数量了。你也可以”sort | uniq -c | sort -nr”来排序。

当然sort和uniq还有许多不同的选项,选择在不同的时候,使用合适的命令才是我们最后的选择。

bash中脚本控制输出中的颜色设置

bash中脚本控制输出中的颜色设置

ANSI控制码的说明
例如:
echo -ne “\33[32m” 可以将字符的显示颜色改为绿色,这个我常用来脚本执行结束之后来打印OK字样。
echo -ne “\33[3;1H” 可以将光标移到第3行第1列处
具体的摘抄一些如下:
\33[0m 关闭所有属性
\33[1m 设置高亮度
\33[4m 下划线
\33[5m 闪烁
\33[7m 反显
\33[8m 消隐
\33[30m — \33[37m 设置前景色
\33[40m — \33[47m 设置背景色
\33[nA 光标上移n行
\33[nB 光标下移n行
\33[nC 光标右移n行
\33[nD 光标左移n行
\33[y;xH设置光标位置
\33[2J 清屏
\33[K 清除从光标到行尾的内容
\33[s 保存光标位置
\33[u 恢复光标位置
\33[?25l 隐藏光标
\33[?25h 显示光标
所有的转移字符表:
\a ASCII 响铃字符(也可以键入 \007)
\d “Wed Sep 06” 格式的日期
\e ASCII 转义字符(也可以键入 \033)
\h 主机名的第一部分(如 “mybox”)
\H 主机的全称(如 “mybox.mydomain.com”)
\j 在此 shell 中通过按 ^Z 挂起的进程数
\l 此 shell 的终端设备名(如 “ttyp4″)
\n 换行符
\r 回车符
\s shell 的名称(如 “bash”)
\t 24 小时制时间(如 “23:01:01″)
\T 12 小时制时间(如 “11:01:01″)
\@ 带有 am/pm 的 12 小时制时间
\u 用户名
\v bash 的版本(如 2.04)
\V Bash 版本(包括补丁级别) ?/td>
\w 当前工作目录(如 “/home/drobbins”)
\W 当前工作目录的“基名 (basename)”(如 “drobbins”)
\! 当前命令在历史缓冲区中的位置
\# 命令编号(只要您键入内容,它就会在每次提示时累加)
\$ 如果您不是超级用户 (root),则插入一个 “$”;如果您是超级用户,则显示一个 “#”
\xxx 插入一个用三位数 xxx(用零代替未使用的数字,如 “\007″)表示的 ASCII 字符
\\ 反斜杠
\[ 这个序列应该出现在不移动光标的字符序列(如颜色转义序列)之前。它使 bash 能够正确计算自动换行。
\] 这个序列应该出现在非打印字符序列之后。

这样,您已经知道了 bash 中用反斜杠转义的全部专用序列。请稍微演练一下这些序列,以对它们的工作方式获得一些感性认识。在您做了一些测试之后,下面开始添加颜色。