Posts tagged ‘my.cnf’

admin账户下安装mysql

安装MySQL相信大家都会安装,但是如果现在你只有机器的admin账户,你不能够su到root下去,那么你怎么才能安装好mysql列。
至少我安装起来感觉缚手缚脚的,最终还是安装成功运行起来了,特别把一些需要注意的点记录下来。

1、my.cnf放在哪里?
从reference上抄录如下:
On Unix, Linux and Mac OS X, MySQL programs read startup options from the following files, in the specified order (top items are used first).
File Name     Purpose
/etc/my.cnf     Global options
/etc/mysql/my.cnf     Global options (as of MySQL 5.1.15)
SYSCONFDIR/my.cnf     Global options
$MYSQL_HOME/my.cnf     Server-specific options
defaults-extra-file     The file specified with –defaults-extra-file=path, if any
~/.my.cnf     User-specific options

~ represents the current user’s home directory (the value of $HOME).
前面的那些目录都没有权限,那么就只好在$HOME下放一个.my.cnf

2、目录配置
目录当然需要放在/home/admin下了,我这边的目录在[mysqld]组下设置如下:
basedir=/home/admin/mysql/mysql/
datadir=/home/admin/mysql/mysqldata/mydata
socket=/home/admin/mysql/mysqldata/sock/mysql.sock
pid-file=/home/admin/mysql/mysqldata/sock/mysql.pid
tmpdir=/home/admin/mysql/mysqldata/tmpdir
log-error=/home/admin/mysql/mysqldata/log/error.log
slow_query_log_file=/home/admin/mysql/mysqldata/log/slow-query.log
log-bin=/home/admin/mysql/mysqldata/binlog/mysql-bin
relay-log=/home/admin/mysql/mysqldata/relaylog/mysql-relay-bin
innodb_data_home_dir = /home/admin/mysql/mysqldata/innodb_ts
innodb_log_group_home_dir = /home/admin/mysql/mysqldata/innodb_log
本来basedir没有在这个.my.cnf里面,结果就出错了。
唉,还好 ./support-files/mysql.server 和 mysqld_safe是脚本,用bash -x 调试模式执行的时候仔细看还是能够找到对应的错误

对应的[client]需要指定:
socket = /home/admin/mysql/mysqldata/sock/mysql.sock

3、用户配置:
用户之前是用的mysql账户运行的,现在要用admin运行,所以修改[mysqld]组如下:
user=admin

4、./support-files/mysql.server修改
作为起停MySQL的脚本,它的basedir和datadir我都设置了
basedir=/home/admin/mysql/mysql/
datadir=/home/admin/mysql/mysqldata/mydata/

5、[mysqld_safe]设置
pid-file=/home/admin/mysql/mysqldata/sock/mysql.pid
本来我只设置了pid,并且在一台机器上启动MySQL成功了。但是在第二台机器上启动的时候失败了。
找了半天,才在mysqld_safe脚本里面找到出错的地方:
/home/admin/mysql/mysql//bin/mysqld_safe: line 137: /var/log/mysqld.log: Permission denied
对应的错误日志不对,于是,在.my.cnf的[mysqld_safe]组里加上
log-error=/home/admin/mysql/mysqldata/log/error.log
启动MySQL,没有异常。大功告成!

另外,也要注意把MySQL的bin目录加入到PATH里面去!

其实这里面最需要注意的就是出错的时候:
bash -x ./support-files/mysql.server start
或者
bash -x /home/admin/mysql/mysql//bin/mysqld_safe –datadir=/home/admin/mysql/mysqldata/mydata –pid-file=/home/admin/mysql/mysqldata/sock/mysql.pid &
查看到底是哪里出错了。根据具体的错误来定位和修改。

may your success!

loose- my.cnf参数prefix

发现一个有意思的参数prefix: –loose-
我们平常使用mysqlbinlog经常会出现错误:
mysqlbinlog: unknown variable ‘default-character-set=utf8’
说不认识这个参数,网上找了一下,发现只要修改my.cnf的client组里面的default-character-set = utf8为loose_default-character-set = utf8,mysqlbinlog就可以正常工作。MySQL5.0和5.1都适用。
这个prefix其实就是让读取该参数的程序不要那么死心眼,如果这个参数自己不解析,那么就当它不存在吧。

拷贝一下MySQL的reference:
4.2.3.2. Program Option Modifiers

Some options are “boolean” and control behavior that can be turned on or off. For example, the mysql client supports a –column-names option that determines whether or not to display a row of column names at the beginning of query results. By default, this option is enabled. However, you may want to disable it in some instances, such as when sending the output of mysql into another program that expects to see only data and not an initial header line.

To disable column names, you can specify the option using any of these forms:

–disable-column-names
–skip-column-names
–column-names=0

The –disable and –skip prefixes and the =0 suffix all have the same effect: They turn the option off.

The “enabled” form of the option may be specified in any of these ways:

–column-names
–enable-column-names
–column-names=1

If an option is prefixed by –loose, a program does not exit with an error if it does not recognize the option, but instead issues only a warning:

shell> mysql –loose-no-such-option
mysql: WARNING: unknown option ‘–no-such-option’

The –loose prefix can be useful when you run programs from multiple installations of MySQL on the same machine and list options in an option file, An option that may not be recognized by all versions of a program can be given using the –loose prefix (or loose in an option file). Versions of the program that recognize the option process it normally, and versions that do not recognize it issue a warning and ignore it.

mysqld enables a limit to be placed on how large client programs can set dynamic system variables. To do this, use a –maximum prefix with the variable name. For example, –maximum-query_cache_size=4M prevents any client from making the query cache size larger than 4MB.

exceeds the log group capacity

做数据订正,需要更新了一批表数据。
数据库是采用的分库结构,也就是说一个MySQL instance里面有多个分库(一共有8个MySQL instance),我的数据库instance里面是16个分库,现在需要更新每个分库里面的一张表(即16张表)的所有数据。
我是用脚本更新的,串行操作,每个分库执行:
update vas_eq_enquiries set ENABLE_VIEW=1;
结果就收到报警,应用部门也过来说他们很多语句执行不了,报错说lock timeout:
100506 19:51:16  InnoDB: ERROR: the age of the last checkpoint is 966364350,
InnoDB: which exceeds the log group capacity 966363956.
InnoDB: If you are using big BLOB or TEXT rows, you must set the
InnoDB: combined size of log files at least 10 times bigger than the
InnoDB: largest such row.
100506 19:51:32  InnoDB: ERROR: the age of the last checkpoint is 1028687928,
InnoDB: which exceeds the log group capacity 966363956.
InnoDB: If you are using big BLOB or TEXT rows, you must set the
InnoDB: combined size of log files at least 10 times bigger than the
InnoDB: largest such row.

认真看了一下错误描述,是说log group的容量不够了。MySQL的innodb引擎日志比较重要,所有数据变更都共享着这个日志undo log。看样子一下子更新的数据太多了,于是我修改脚本,每次间隔5s,10s,30s去操作每个分库,最后发现间隔30s就不会有问题了。
当然,这个30s只对我的这个情况有意义。我这里也查看了一下MySQL instance的logfile大小:
innodb_log_file_size = 512M
innodb_log_files_in_group = 2

也就是说,它只允许最大1G的变更。
查看了我变更的表vas_eq_enquiries,在各个分库上大小不一,25M~77M的都有,平均算50M,16个分库,那就有16*50=800M了,在加上数据库自己的变化量1G空间确实会捉襟见肘。

这个问题的最终解决方案有两个:
1、减少短时间内变更的数据量,避免更新造成log日志的容量不足。对于分库更新量的情况特别需要注意,
2、增加log日志的容量。log日志的容量,目前MySQL最大不允许超过4G。这里需要注意的是,修改log日志的容量必须要在MySQL将所有日志文件都应用完毕的情况下,先停机,然后将日志文件移到另外的目录备份,重启MySQL数据库。MySQL数据库启动以后,会自动生成新的log日志。否则可能会有如下的错误:
InnoDB: Error: log file /data/mysqldata/innodb_log/ib_logfile0 is of different size 0 1073741824 bytes
InnoDB: than specified in the .cnf file 0 1283457024 bytes!
100506 21:57:41 [ERROR] Plugin ‘InnoDB’ init function returned error.
100506 21:57:41 [ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed.
100506 21:57:41 [ERROR] Unknown/unsupported table type: INNODB
100506 21:57:41 [ERROR] Aborting

导致MySQL数据库启动不了。

顺便把MySQL这两个参数解释抄在下面:
innodb_log_file_size
The size in bytes of each log file in a log group. The combined size of log files must be less than 4GB. The default value is 5MB. Sensible values range from 1MB to 1/N-th of the size of the buffer pool, where N is the number of log files in the group. The larger the value, the less checkpoint flush activity is needed in the buffer pool, saving disk I/O. But larger log files also mean that recovery is slower in case of a crash.

innodb_log_files_in_group
The number of log files in the log group. InnoDB writes to the files in a circular fashion. The default (and recommended) value is 2.

这里log日志的容量可能大家会觉得越大越好。innodb_log_file_size里面也说了这个值越大,把checkpoint从buffer pool中刷到磁盘的频率也越少,减少了很多磁盘I/O。同时,crash情况下需要恢复的时间也越多。

Baron Schwartz 在MySQL Performance Blog里面提到一种怎样设置innodb_log_file_size的方法。http://www.mysqlperformanceblog.com/2008/11/21/how-to-calculate-a-good-innodb-log-file-size/
他根据每分钟变化的数据量*60分钟,来粗略的得到一个innodb_log_file_size的值。
mysql> pager grep sequence
PAGER SET TO ‘grep sequence’
mysql> SHOW engine innodb STATUS\G SELECT sleep(60); SHOW engine innodb STATUS\G
Log sequence number 84 3836410803
1 row IN SET (0.06 sec)
1 row IN SET (1 min 0.00 sec)
Log sequence number 84 3838334638
1 row IN SET (0.05 sec)
然后:
mysql> SELECT (3838334638 – 3836410803) / 1024 / 1024 AS MB_per_min;
+————+
| MB_per_min
+————+
| 1.83471203
+————+

于是:1.8*60 约等于110M,那么就取128M把,如果innodb_log_files_in_group=2,那么innodb_log_file_size=128/2=64M。
这个当然取决于你这一分钟应用程序修改的量的多少,如果取的时间不对,那么这个值当然不精确。

3楼的3. Sheeri K. Cabral提出了他自己的方法:登录系统,直接看ib_logfile切换的速度。
/var/lib/mysql> ls -lrth ib_logfile*
-rw-r–r– 1 mysql mysql 400M Nov 22 09:00 ib_logfile1
-rw-r–r– 1 mysql mysql 400M Nov 23 10:51 ib_logfile0

(这里26个小时切换了一次)然后再根据你的要求:你需要logfile中保存多久的日志,来调整logfile的大小。当然,这个也不精确。取决于你什么时候去看这个文件,白天数据量变化多,晚上变化少,这个间隔时间就完全不同。

总的来说,需要注意这个innodb_log_file_size的大小设置,每个应用都有自己的特色,大小设置需要根据应用的不同来设置。

may you success.