Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Monday, May 24, 2010

MySQL5 slave同步报错(server_errno=1236)

100519 19:35:33 [Note] Slave I/O thread: connected to master 'repl@master:3306', replication started in log 'mysql-bin.000176' at position 615525147

100519 19:35:33 [ERROR] Error reading packet from server: Could not find first log file name in binary log index file ( server_errno=1236)

从日志信息上可知,slave已经连接到master,并且准备从指定的binlog文件指定位置开始同步,但后面错误提示日志文件找不到,而master服务器上日志文件是存在的。
这种情况有二种处理方法:
一、重启主数据库(master)之后,然后slave上stop slave;start slave,再检查同步的状态。
二、不重启主服务器,则用mysqlbinlog根据start-position或者start-datetime,将日志分析出来后,将分析结果在slave上用mysql命令导入,导入完成后,再用CHANGE MASTER TO语句,从下一个日志文件MASTER_LOG_POSITION=98开始同步。

另外在使用mysqlbinlog工具进行日志导入时,需要注意以下问题,下面内容转自MySQL官方手册:
如果MySQL服务器上有多个要执行的二进制日志,安全的方法是在一个连接中处理它们。下面是一个说明什么是不安全的例子:

shell> mysqlbinlog hostname-bin.000001 | mysql # DANGER!!
shell> mysqlbinlog hostname-bin.000002 | mysql # DANGER!!

使用与服务器的不同连接来处理二进制日志时,如果第1个日志文件包含一个CREATE TEMPORARY TABLE语句,第2个日志包含一个使用该临时表的语句,则会造成问题。当第1个mysql进程结束时,服务器撤销临时表。当第2个mysql进程想使用该表时,服务器报告 “不知道该表”。

要想避免此类问题,使用一个连接来执行想要处理的所有二进制日志中的内容。下面提供了一种方法:

shell> mysqlbinlog hostname-bin.000001 hostname-bin.000002 | mysql -u root -ppassword

Reference: http://yuweijun.blogspot.com/2009/12/mysql50.html

MySQL: Many tables or many databases?

Question:
For a project we having a bunch of data that always have the same structure and is not linked together. There are two approaches to save the data:
* Creating a new database for every pool (about 15-25 tables)
* Creating all the tables in one database and differ the pools by table names.
Which one is easier and faster to handle for MySQL?

Answer:
There should be no significant performance difference between multiple tables in a single database versus multiple tables in separate databases.

In MySQL, databases (standard SQL uses the term "schema" for this) serve chiefly as a namespace for tables. A database has only a few attributes, e.g. the default character set and collation. And that usage of GRANT makes it convenient to control access privileges per database, but that has nothing to do with performance.

You can access tables in any database from a single connection (provided they are managed by the same instance of MySQL Server). You just have to qualify the table name:

SELECT * FROM database17.accounts_table;

This is purely a syntactical difference. It should have no effect on performance.

Regarding storage, you can't organize tables into a file-per-database as @Chris speculates. With the MyISAM storage engine, you always have a file per table. With the InnoDB storage engine, you either have a single set of storage files that amalgamate all tables, or else you have a file per table (this is configured for the whole MySQL server, not per database). In either case, there's no performance advantage or disadvantage to creating the tables in a single database versus many databases.

There aren't many MySQL configuration parameters that work per database. Most parameters that affect server performance are server-wide in scope.

Regarding backups, you can specify a subset of tables as arguments to the mysqldump command. It may be more convenient to back up logical sets of tables per database, without having to name all the tables on the command-line. But it should make no difference to performance, only convenience for you as you enter the backup command.

-- Bill Karwin (the author of SQL Antipatterns from Pragmatic Bookshelf)

Thursday, January 28, 2010

mysql 客户端中比较有用的几个命令

mysql> help

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Network Support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

谈一下个人觉得上面比较有用的几个命令:
1、 edit
mysql>\e
输入\e后调用系统的默认文本编辑器,如vi出来,用于编辑SQL语句,避免直接在命令行中,太长的SQL输入错误,修改起来麻烦,输入完成保存退出即运行。

2、 pager
利用此命令最方便就是可以结合less查看输出结果,有时比用\G输出要方便整齐。在mysql命令行中输入:
mysql>pager less -S
之后的查询就会以不折行的形式输出到控制台。另一个用法是将SQL输出重定向到另外一个文本日志文件中:
mysql>pager cat > /tmp/sql_log.txt
以后的查询生成结果会被写入此文件中。

3、 tee
这个命令的功能与pager重定向日志文件类似,可以将生成的日志写入外部文件中。
mysql>tee /tmp/sql_log.txt

4、 system
在mysql console中运行外部操作系统的命令:
mysql>\! ls -l

5、 source
执行外部的SQL文件,有时在命令行中直接导入外部的一个SQL文件:
mysql>source /home/yu/test.sql;
这个与在操作系统命令行下执行:
$>mysql -u test -p < /home/yu/test.sql
效果一样。

Reference:
http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html

Thursday, December 10, 2009

MySQL5.0 同步备份恢复的三种方法

第一种最简单的方法适用于数据库文件比较小,能在停止主数据库服务后几分钟内打完tar包的情况,这种情况与第一次做slave同步的方法一样:
mysql> FLUSH TABLES WITH READ LOCK;
mysql > SHOW MASTER STATUS;
记录状态后进入命令行将数据库原生数据文件打个tar包,再
mysql> UNLOCK TABLES;
将tar包重布署到MySQL slave上即可,注意将tar包中的日志文件,master.info,relay-log.info要删除之后再CHANGE MASTER TO。

第二种方法是利用mysql的二进制日志文件。
首先重启一次主数据库。
然后在slave上用以下命令查看二进制日志文件执行的最后SQL语句,其中start-datetime时间为此二进制日志文件最后的修改时间,或者提前30秒方便查看最后执行完成的SQL语句:
$> mysqlbinlog -S /tmp/mysql.sock hostname-relay-bin.000002 --start-datetime='2009-12-11 11:12:00'

然后在主数据库上用命令筛选出符合时间范围的日志:
$> mysqlbinlog --start-datetime='2009-12-11 11:12:00' -S /tmp/mysql.sock mysql-bin.000007 > /home/username/bin-log.sql
再用grep命令,查找到bin-log.sql中与之前在slave上查到的SQL行号,并用sed命令删除1到此行号的全部行(举例查到行号为1234):
$> grep -n "last executed sql statement" /home/username/bin-log.sql
$> sed '1,1234d' /home/username/bin-log.sql > /home/username/bin-log-sed.sql
将生成的bin-log-sed.sql文件传到slave数据库上用mysql命令行工具导入数据。
$> mysql -uroot -p < bin-log-sed.sql
如果碰到报错,用sed再删除sql文件的前几行导入。
导入完成后,进行mysql控制台,下面的master_log_file是同步中断的主数据库日志文件mysql-bin.000007的后继日志文件名mysql-bin.000008:
$> stop slave;
$> change master to master_log_file='mysql-bin.000008', master_log_pos=98;
$> start slave;
$> show slave status\G

第三种方法也是利用二进制日志文件恢复。
与第二种方法类似,只是在grep查到行号之后,用awk命令找到日志中断的位置,重新调整slave上的master_log_pos即可。
举例查到行号为1234:
$> cat /home/username/bin-log.sql |awk 'NR >= 1234 {print $0}' |more
可以找到最后执行完的那个SQL之后,服务器的二进制日志文件位置:
last executed sql statuement;
之后会看到类似:
# at 1023411738
这样的内容,用这个值更新slave上的master信息:
$> stop slave;
$> change master to master_log_file='mysql-bin.000007', master_log_pos=1023411738;
$> start slave;
$> show slave status\G
在slave的二进制日志或者是hostname-relay-log.info,hostname.err中也会有master上的end_log_pos,可以先尝试用一下,一般因为master/slave异常才导致同步失败,在slave上的这些信息已经不正确,所以需要用awk找到服务器上的二进制日志位置。


Wednesday, December 02, 2009

在64位的CentOS5中编译安装mysql5.0.88

$> wget http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.88.zip/from/http://mysql.byungsoo.net/
$> yum -y install gcc
$> yum -y install gcc-c++
$> yum -y install ncurses-devel

$> CC=gcc \
CFLAGS="-O3 -fno-omit-frame-pointer" \
CXX=gcc \
CXXFLAGS="-O3 -fno-omit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti" \
./configure --prefix=/usr/local/mysql --without-debug --with-big-tables --with-unix-socket-path=/tmp/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client

$> make && make install

Reference: http://dev.mysql.com/doc/refman/5.0/en/linux-ia-64.html

Monday, November 09, 2009

Mysql Server System Variables

mysql 数据库中几个系统变量的分析说明

connect_timeout

Command Line Format --connect_timeout=#
Config File Format connect_timeout
Option Sets Variable Yes, connect_timeout
Variable Name connect_timeout
Variable Scope Global
Dynamic Variable Yes
   Permitted Values (<= 5.1.22)
Type numeric
Default 5
   Permitted Values (>= 5.1.23)
Type numeric
Default 10

The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake. The default value is 10 seconds as of MySQL 5.1.23 and 5 seconds before that.
Increasing the connect_timeout value might help if clients frequently encounter errors of the form Lost connection to MySQL server at 'XXX', system error: errno.
增加此值可以解决此问题:Lost connection to MySQL server at 'XXX', system error: errno.



delay_key_write

Command Line Format --delay-key-write[=name]
Config File Format delay-key-write
Option Sets Variable Yes, delay_key_write
Variable Name delay-key-write
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type enumeration
Default ON
Valid Values ON, OFF, ALL

This option applies only to MyISAM tables.
此参数只针对MyISAM表。
It can have one of the following values to affect handling of the DELAY_KEY_WRITE table option that can be used in CREATE TABLE statements.
Option Description
OFF DELAY_KEY_WRITE is ignored.
ON MySQL honors any DELAY_KEY_WRITE option specified in CREATE TABLE statements. This is the default value.
ALL All new opened tables are treated as if they were created with the DELAY_KEY_WRITE option enabled.

If DELAY_KEY_WRITE is enabled for a table, the key buffer is not flushed for the table on every index update, but only when the table is closed. This speeds up writes on keys a lot, but if you use this feature, you should add automatic checking of all MyISAM tables by starting the server with the --myisam-recover option (for example, --myisam-recover=BACKUP,FORCE). See Section 5.1.2, “Server Command Options”, and Section 13.5.1, “MyISAM Startup Options”.
Warning
如果启用了DELAY_KEY_WRITE,说明使用该项的表的键缓冲区在每次更新索引时不被清空,只有关闭表时才清空。遮掩盖可以大大加快键的写操作,但如果你使用该特性,你应用--myisam-recover选项启动服务器,为所有MyISAM表添加自动检查(例如,--myisam-recover=BACKUP,FORCE)。
If you enable external locking with --external-locking, there is no protection against index corruption for tables that use delayed key writes.



expire_logs_days

Command Line Format --expire_logs_days=#
Config File Format expire_logs_days
Option Sets Variable Yes, expire_logs_days
Variable Name expire_logs_days
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 0
Range 0-99

The number of days for automatic binary log removal. The default is 0, which means “no automatic removal.” Possible removals happen at startup and when the binary log is flushed.
二进制日志自动删除的天数,可设置值的范围为0至99。默认值为0,表示“没有自动删除”。启动时和二进制日志循环时可能删除。

init_connect
Command Line Format --init-connect=name
Config File Format init_connect
Option Sets Variable Yes, init_connect
Variable Name init_connect
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type string

A string to be executed by the server for each client that connects. The string consists of one or more SQL statements. To specify multiple statements, separate them by semicolon characters. For example, each client begins by default with autocommit mode enabled. There is no global system variable to specify that autocommit should be disabled by default, but init_connect can be used to achieve the same effect:

SET GLOBAL init_connect='SET autocommit=0';

This variable can also be set on the command line or in an option file. To set the variable as just shown using an option file, include these lines:

[mysqld]
init_connect='SET autocommit=0'

Note that the content of init_connect is not executed for users that have the SUPER privilege. This is done so that an erroneous value for init_connect does not prevent all clients from connecting. For example, the value might contain a statement that has a syntax error, thus causing client connections to fail. Not executing init_connect for users that have the SUPER privilege enables them to open a connection and fix the init_connect value.
在这里比较常用的初始化语句是设置一个字符集,当然也可以直接修改my.cnf中的配置参数(character_set_connection/character_set_client等):
[mysqld]
init_connect='SET names utf8'




interactive_timeout

Command Line Format --interactive_timeout=#
Config File Format interactive_timeout
Option Sets Variable Yes, interactive_timeout
Variable Name interactive_timeout
Variable Scope Both
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 28800
Min Value 1

The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect(). See also wait_timeout.
一个连接如果长时间空闲会被mysql服务端强行关闭掉,其默认的时间是8小时,在用JDBC连接到mysql中,如果池里的连接超过8小时没有使用,就会产生连接异常。

wait_timeout
Command Line Format --wait_timeout=#
Config File Format wait_timeout
Option Sets Variable Yes, wait_timeout
Variable Name wait_timeout
Variable Scope Both
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 28800
Range 1-31536000
   Permitted Values
Type (windows) numeric
Default 28800
Range 1-2147483

The number of seconds the server waits for activity on a noninteractive connection before closing it. This timeout applies only to TCP/IP and Unix socket file connections, not to connections made via named pipes, or shared memory.

On thread startup, the session wait_timeout value is initialized from the global wait_timeout value or from the global interactive_timeout value, depending on the type of client (as defined by the CLIENT_INTERACTIVE connect option to mysql_real_connect()).
这个值一般不需要去设置,只要设置interactive_timeout。



join_buffer_size
Command Line Format --join_buffer_size=#
Config File Format join_buffer_size
Option Sets Variable Yes, join_buffer_size
Variable Name join_buffer_size
Variable Scope Both
Dynamic Variable Yes
   Permitted Values
Platform Bit Size 64
Type numeric
Default 131072
Range 8200-18446744073709547520

The size of the buffer that is used for plain index scans, range index scans, and joins that do not use indexes and thus perform full table scans. Normally, the best way to get fast joins is to add indexes. Increase the value of join_buffer_size to get a faster full join when adding indexes is not possible. One join buffer is allocated for each full join between two tables. For a complex join between several tables for which indexes are not used, multiple join buffers might be necessary.

The maximum allowable setting for join_buffer_size is 4GB. As of MySQL 5.1.23, values larger than 4GB are allowed for 64-bit platforms (except 64-bit Windows, for which large values are truncated to 4GB with a warning).




key_buffer_size

Command Line Format --key_buffer_size=#
Config File Format key_buffer_size
Option Sets Variable Yes, key_buffer_size
Variable Name key_buffer_size
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 8388608
Range 8-4294967295

此参数仅用于MyISAM表中,一般此值可设置为物理内存的25-50%,尽可能的使用 Key_reads/Key_read_requests 的比率小于0.01,最重要的mysql性能调优参数。

Index blocks for MyISAM tables are buffered and are shared by all threads. key_buffer_size is the size of the buffer used for index blocks. The key buffer is also known as the key cache.
MyISAM表的索引块分配了缓冲区,由所有线程共享。key_buffer_size是索引块缓冲区的大小。键值缓冲区即为键值缓存。
The maximum allowable setting for key_buffer_size is 4GB on 32-bit platforms. As of MySQL 5.1.23, values larger than 4GB are allowed for 64-bit platforms, except 64-bit Windows prior to MySQL 5.1.31, for which large values are truncated to 4GB with a warning. As of MySQL 5.1.31, values larger than 4GB are also allowed for 64-bit Windows. The effective maximum size might be less, depending on your available physical RAM and per-process RAM limits imposed by your operating system or hardware platform. The value of this variable indicates the amount of memory requested. Internally, the server allocates as much memory as possible up to this amount, but the actual allocation might be less.
key_buffer_size的最大允许设定值为4GB。有效最大值可以更小,取决于可用物理RAM和操作系统或硬件平台强加的每个进程的RAM限制。
You can increase the value to get better index handling for all reads and multiple writes; on a system whose primary function is to run MySQL using the MyISAM storage engine, 25% of the machine's total memory is an acceptable value for this variable. However, you should be aware that, if you make the value too large (for example, more than 50% of the machine's total memory), your system might start to page and become extremely slow. This is because MySQL relies on the operating system to perform file system caching for data reads, so you must leave some room for the file system cache. You should also consider the memory requirements of any other storage engines that you may be using in addition to MyISAM.
增加该值,达到你可以提供的更好的索引处理(所有读和多个写操作)。通常为主要运行MySQL的机器内存的25%。但是,如果你将该值设得过大(例如,大于总内存的50%),系统将转换为页并变得极慢。MySQL依赖操作系统来执行数据读取时的文件系统缓存,因此你必须为文件系统缓存留一些空间。
For even more speed when writing many rows at the same time, use LOCK TABLES. See Section 7.2.21, “Speed of INSERT Statements”.
同时写多行时要想速度更快,应使用LOCK TABLES。
You can check the performance of the key buffer by issuing a SHOW STATUS statement and examining the Key_read_requests, Key_reads, Key_write_requests, and Key_writes status variables. (See Section 12.5.5, “SHOW Syntax”.) The Key_reads/Key_read_requests ratio should normally be less than 0.01. The Key_writes/Key_write_requests ratio is usually near 1 if you are using mostly updates and deletes, but might be much smaller if you tend to do updates that affect many rows at the same time or if you are using the DELAY_KEY_WRITE table option.

The fraction of the key buffer in use can be determined using key_buffer_size in conjunction with the Key_blocks_unused status variable and the buffer block size, which is available from the key_cache_block_size system variable:

   ((Key_blocks_unused × key_cache_block_size) / key_buffer_size)
  
用key_buffer_size结合Key_blocks_unused状态变量和缓冲区块大小,可以确定使用的键值缓冲区的比例。从key_cache_block_size服务器变量可以获得缓冲区块大小。使用的缓冲区的比例为:
   ((Key_blocks_unused * key_cache_block_size) / key_buffer_size)
该值为约数,因为键值缓冲区的部分空间被分配用作内部管理结构。

可以创建多个MyISAM键值缓存。4GB限制可以适合每个缓存,而不是一个组。
This value is an approximation because some space in the key buffer may be allocated internally for administrative structures.

It is possible to create multiple MyISAM key caches. The size limit of 4GB applies to each cache individually, not as a group. See Section 7.4.5, “The MyISAM Key Cache”.



log_slave_updates

Whether updates received by a slave server from a master server should be logged to the slave's own binary log. Binary logging must be enabled on the slave for this variable to have any effect.
假如mysql slave还用作为master server时,需要设置此参数,记录所有SQL操作,提供其他slave复制。



log_slow_queries
Command Line Format --log-slow-queries[=name]
Config File Format log-slow-queries
Option Sets Variable Yes, log_slow_queries
Variable Name log_slow_queries
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type boolean

Whether slow queries should be logged. “Slow” is determined by the value of the long_query_time variable.
记录查询非常慢的SQL到一个独立的日志文件中,可用于SQL分析和优化。




lower_case_file_system
Command Line Format --lower_case_file_system[=#]
Config File Format lower_case_file_system
Option Sets Variable Yes, lower_case_file_system
Variable Name lower_case_file_system
Variable Scope Global
Dynamic Variable No
   Permitted Values
Type boolean

This variable describes the case sensitivity of file names on the file system where the data directory is located. OFF means file names are case sensitive, ON means they are not case sensitive.

lower_case_table_names
Command Line Format --lower_case_table_names[=#]
Config File Format lower_case_table_names
Option Sets Variable Yes, lower_case_table_names
Variable Name lower_case_table_names
Variable Scope Global
Dynamic Variable No
   Permitted Values
Type numeric
Default 0
Range 0-2

If set to 1, table names are stored in lowercase on disk and table name comparisons are not case sensitive. If set to 2 table names are stored as given but compared in lowercase. This option also applies to database names and table aliases. See Section 8.2.2, “Identifier Case Sensitivity”.

这个参数在linux服务器上需要注意,一般是将此值设置为1,使表名全部以小写方式写入硬盘,避免表名的大小写问题。

If you are using InnoDB tables, you should set this variable to 1 on all platforms to force names to be converted to lowercase.

You should not set this variable to 0 if you are running MySQL on a system that does not have case-sensitive file names (such as Windows or Mac OS X). If this variable is not set at startup and the file system on which the data directory is located does not have case-sensitive file names, MySQL automatically sets lower_case_table_names to 2.


max_allowed_packet
Command Line Format --max_allowed_packet=#
Config File Format max_allowed_packet
Option Sets Variable Yes, max_allowed_packet
Variable Name max_allowed_packet
Variable Scope Both
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 1048576
Range 1024-1073741824

The maximum size of one packet or any generated/intermediate string.

The packet message buffer is initialized to net_buffer_length bytes, but can grow up to max_allowed_packet bytes when needed. This value by default is small, to catch large (possibly incorrect) packets.

此值需要是1024的一个倍数值,如果操作的数据有使用很长的字符串和大的BLOB字段,如图片,需要增加此值。
You must increase this value if you are using large BLOB columns or long strings. It should be as big as the largest BLOB you want to use. The protocol limit for max_allowed_packet is 1GB. The value should be a multiple of 1024; nonmultiples are rounded down to the nearest multiple.

When you change the message buffer size by changing the value of the max_allowed_packet variable, you should also change the buffer size on the client side if your client program allows it. On the client side, max_allowed_packet has a default of 1GB. Some programs such as mysql and mysqldump enable you to change the client-side value by setting max_allowed_packet on the command line or in an option file.




max_connect_errors
Command Line Format --max_connect_errors=#
Config File Format max_connect_errors
Option Sets Variable Yes, max_connect_errors
Variable Name max_connect_errors
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Platform Bit Size 32
Type numeric
Default 10
Range 1-4294967295
   Permitted Values
Platform Bit Size 64
Type numeric
Default 10
Range 1-18446744073709547520

If there are more than this number of interrupted connections from a host, that host is blocked from further connections. You can unblock blocked hosts with the FLUSH HOSTS statement.
如果从某台服务器的连接错误过多,会被mysql服务器阻挡连接。




max_connections
Command Line Format --max_connections=#
Config File Format max_connections
Option Sets Variable Yes, max_connections
Variable Name max_connections
Variable Scope Global
Dynamic Variable Yes
   Permitted Values (<= 5.1.14)
Type numeric
Default 100
   Permitted Values (>= 5.1.15)
Type numeric
Default 151
Range 1-16384
   Permitted Values (>= 5.1.17)
Type numeric
Default 151
Range 1-100000

The number of simultaneous client connections allowed. By default, this is 151, beginning with MySQL 5.1.15. (Previously, the default was 100.)
这个值一般都会调整得大一些,如200或者是500,用于处理并发连接数。如果看到“Too many connections”这样的错误提示就是表示mysql服务器的连接数已经被用完。



max_relay_log_size
Command Line Format --max_relay_log_size=#
Config File Format max_relay_log_size
Option Sets Variable Yes, max_relay_log_size
Variable Name max_relay_log_size
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 0
Range 0-1073741824

If a write by a replication slave to its relay log causes the current log file size to exceed the value of this variable, the slave rotates the relay logs (closes the current file and opens the next one). If max_relay_log_size is 0, the server uses max_binlog_size for both the binary log and the relay log. If max_relay_log_size is greater than 0, it constrains the size of the relay log, which enables you to have different sizes for the two logs. You must set max_relay_log_size to between 4096 bytes and 1GB (inclusive), or to 0. The default value is 0.



net_read_timeout
Command Line Format --net_read_timeout=#
Config File Format net_read_timeout
Option Sets Variable Yes, net_read_timeout
Variable Name net_read_timeout
Variable Scope Both
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 30
Min Value 1

The number of seconds to wait for more data from a connection before aborting the read. This timeout applies only to TCP/IP connections, not to connections made via Unix socket files, named pipes, or shared memory. When the server is reading from the client, net_read_timeout is the timeout value controlling when to abort. When the server is writing to the client, net_write_timeout is the timeout value controlling when to abort.
此参数只针对TCP/IP的mysql连接,当超过此值的秒数后,服务器端会放弃从客户端读取数据。



old_passwords
Command Line Format --old_passwords
Config File Format old-passwords
Option Sets Variable Yes, old_passwords
Variable Name old_passwords
Variable Scope Both
Dynamic Variable Yes
   Permitted Values
Type boolean
Default FALSE

Whether the server should use pre-4.1-style passwords for MySQL user accounts.
如果客户端收到的错误消息为:“Client does not support authentication protocol”,说明服务器使用的是旧的密码格式,需要为用户按旧的格式重设密码。
mysql>SET PASSWORD 'some_user'@'some_host' = OLD_PASSWORD('newpwd');




read_only
Command Line Format --read_only
Config File Format read_only
Option Sets Variable Yes, read_only
Variable Name read_only
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 0

This variable is off by default. When it is enabled, the server allows no updates except from users that have the SUPER privilege or (on a slave server) from updates performed by slave threads. On a slave server, this can be useful to ensure that the slave accepts updates only from its master server and not from clients. This variable does not apply to TEMPORARY tables, nor does it prevent the server from inserting rows into the log tables (see Section 5.2.1, “Selecting General Query and Slow Query Log Output Destinations”).

read_only exists only as a GLOBAL variable, so changes to its value require the SUPER privilege. Changes to read_only on a master server are not replicated to slave servers. The value can be set on a slave server independent of the setting on the master.

As of MySQL 5.1.15, the following conditions apply:

    * If you attempt to enable read_only while you have any explicit locks (acquired with LOCK TABLES) or have a pending transaction, an error occurs.
    * If you attempt to enable read_only while other clients hold explicit table locks or have pending transactions, the attempt blocks until the locks are released and the transactions end. While the attempt to enable read_only is pending, requests by other clients for table locks or to begin transactions also block until read_only has been set.
    * read_only can be enabled while you hold a global read lock (acquired with FLUSH TABLES WITH READ LOCK) because that does not involve table locks.
这个参数用于slave服务器上,可以控制避免同步复制发生问题。在master上设置此值与slave是无关的,二都互相独立。




server_id
Command Line Format --server-id=#
Config File Format server-id
Option Sets Variable Yes, server_id
Variable Name server_id
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 0
Range 0-4294967295

The server ID, used in replication to give each master and slave a unique identity. This variable is set by the --server-id option. For each server participating in replication, you should pick a positive integer in the range from 1 to 232 – 1 to act as that server's ID.
这个参数用在同步复制时,分配给每个mysql server一个独立唯一的ID标识。




skip_networking

This is ON if the server allows only local (non-TCP/IP) connections. On Unix, local connections use a Unix socket file. On Windows, local connections use a named pipe or shared memory. On NetWare, only TCP/IP connections are supported, so do not set this variable to ON. This variable can be set to ON with the --skip-networking option.
这个参数在许多linux发行版中是被打开的,这样如果是通过TCP/IP进行连接的话,是无法连接成功的,需要注释掉这一行设置才可以,或者使用socket进行连接。





slow_query_log

Whether the slow query log is enabled. The value can be 0 (or OFF) to disable the log or 1 (or ON) to enable the log. The default value depends on whether the --slow_query_log option is given (--log-slow-queries before MySQL 5.1.29). The destination for log output is controlled by the log_output system variable; if that value is NONE, no log entries are written even if the log is enabled. The slow_query_log variable was added in MySQL 5.1.12.
用于分析查询效率低下的SQL


slow_query_log_file
Version Introduced 5.1.12
Command Line Format --slow-query-log-file=file_name
Config File Format slow_query_log_file
Option Sets Variable Yes, slow_query_log_file
Variable Name slow_query_log_file
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type filename

The name of the slow query log file. The default value is host_name-slow.log, but the initial value can be changed with the --slow_query_log_file option (--log-slow-queries before MySQL 5.1.29). This variable was added in MySQL 5.1.12.


  
sort_buffer_size
Command Line Format --sort_buffer_size=#
Config File Format sort_buffer_size
Option Sets Variable Yes, sort_buffer_size
Variable Name sort_buffer_size
Variable Scope Both
Dynamic Variable Yes
   Permitted Values
Platform Bit Size 32
Type numeric
Default 2097144
Max Value 4294967295
   Permitted Values
Platform Bit Size 64
Type numeric
Default 2097144
Max Value 18446744073709547520

Each thread that needs to do a sort allocates a buffer of this size. Increase this value for faster ORDER BY or GROUP BY operations. See Section B.5.4.4, “Where MySQL Stores Temporary Files”.

The maximum allowable setting for sort_buffer_size is 4GB. As of MySQL 5.1.23, values larger than 4GB are allowed for 64-bit platforms (except 64-bit Windows, for which large values are truncated to 4GB with a warning).
对于SQL中用到order by和group by子句的,提高此值可以增加查询的速度。



table_cache
Version Removed 5.1.3
Version Deprecated 5.1.3
Command Line Format --table_cache=#
Config File Format table_cache
Option Sets Variable Yes, table_cache
Variable Name table_cache
Variable Scope Global
Dynamic Variable Yes
Deprecated 5.1.3, by table_open_cache
   Permitted Values
Type numeric
Default 64
Range 1-524288

This is the old name of table_open_cache before MySQL 5.1.3. From 5.1.3 on, use table_open_cache instead.



table_open_cache
Version Introduced 5.1.3
Command Line Format --table-open-cache=#
Config File Format table_open_cache
Variable Name table_open_cache
Variable Scope Global
Dynamic Variable Yes
   Permitted Values
Type numeric
Default 64
Range 64-524288

The number of open tables for all threads. Increasing this value increases the number of file descriptors that mysqld requires. You can check whether you need to increase the table cache by checking the Opened_tables status variable. See Section 5.1.7, “Server Status Variables”. If the value of Opened_tables is large and you don't do FLUSH TABLES often (which just forces all tables to be closed and reopened), then you should increase the value of the table_open_cache variable. For more information about the table cache, see Section 7.4.7, “How MySQL Opens and Closes Tables”. Before MySQL 5.1.3, this variable is called table_cache.
  


thread_concurrency
Command Line Format --thread_concurrency=#
Config File Format thread_concurrency
Option Sets Variable Yes, thread_concurrency
Variable Name thread_concurrency
Variable Scope Global
Dynamic Variable No
   Permitted Values
Type numeric
Default 10
Range 1-512

This variable is specific to Solaris systems, for which mysqld invokes the thr_setconcurrency() with the variable value. This function enables applications to give the threads system a hint about the desired number of threads that should be run at the same time.

Tuesday, December 23, 2008

install ruby-mysql gem in mac osx 10.5

首先用dmg包安装的mysql(/usr/local/mysql),来安装mysql-ruby gem包:
$> sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions. This could take a while...
Successfully installed mysql-2.7
1 gem installed

$> sudo gem install dbd-mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
Successfully installed dbd-mysql-0.4.2
1 gem installed
Installing ri documentation for dbd-mysql-0.4.2...
Installing RDoc documentation for dbd-mysql-0.4.2...
看上去是装成功了,但实际使用时却抛出了以下错误:
dyld: lazy symbol binding failed: Symbol not found: _mysql_init
Referenced from: /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Expected in: dynamic lookup

这个主要是因为平台原因造成的,分别查看dmg包和二进制包的mysql_config可看到-arch的差异:
$> /usr/local/mysql/bin/mysql_config
Usage: /usr/local/mysql/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/usr/local/mysql/include -Os -arch ppc -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL]
--include [-I/usr/local/mysql/include]
--libs [-L/usr/local/mysql/lib -lmysqlclient -lz -lm]
--libs_r [-L/usr/local/mysql/lib -lmysqlclient_r -lz -lm]
--socket [/tmp/mysql.sock]
--port [0]
--version [5.1.23-rc]
--libmysqld-libs [-L/usr/local/mysql/lib -lmysqld -lz -lm]
$> /usr/local/mysql5/bin/mysql_config
Usage: /usr/local/mysql5/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/usr/local/mysql5/include -g -Os -arch i386 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT]
--include [-I/usr/local/mysql5/include]
--libs [-L/usr/local/mysql5/lib -lmysqlclient -lz -lm -lmygcc]
--libs_r [-L/usr/local/mysql5/lib -lmysqlclient_r -lz -lm -lmygcc]
--socket [/tmp/mysql.sock]
--port [0]
--version [5.0.67]
--libmysqld-libs [-L/usr/local/mysql5/lib -lmysqld -lz -lm -lmygcc]

而mac osx 10.5的macbook则是i386的:
$> uname -a
Darwin Macintosh.local 9.5.0 Darwin Kernel Version 9.5.0: Wed Sep 3 11:29:43 PDT 2008; root:xnu-1228.7.58~1/RELEASE_I386 i386

所以用i386平台编译的mysql5来编译安装mysql-ruby包:


$> sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql5/bin/mysql_config
Building native extensions. This could take a while...
Successfully installed mysql-2.7
1 gem installed

$> sudo env ARCHFLAGS="-arch i386" gem install dbd-mysql -- --with-mysql-config=/usr/local/mysql5/bin/mysql_config
Successfully installed dbd-mysql-0.4.2
1 gem installed
Installing ri documentation for dbd-mysql-0.4.2...
Installing RDoc documentation for dbd-mysql-0.4.2...

这样才能正确安装上ruby-mysql和dbd-mysql。

Monday, December 15, 2008

mongrel and rails 2.2.2 环境下报 mysql lib 的错误及解决方法

Processing Rails::InfoController#properties (for 127.0.0.1 at 2008-12-15 17:10:53) [GET]

LoadError (dlopen(/Library/Ruby/Site/1.8/universal-darwin9.0/mysql.bundle, 9): Library not loaded: /usr/local/mysql/lib/libmysqlclient.15.dylib
Referenced from: /Library/Ruby/Site/1.8/universal-darwin9.0/mysql.bundle
Reason: image not found - /Library/Ruby/Site/1.8/universal-darwin9.0/mysql.bundle):
/Library/Ruby/Site/1.8/universal-darwin9.0/mysql.bundle
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
/Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:153:in `require'

在MAC OSX 10.5 和 mysql 5.1.23-rc 上,rails 2.2.2 用 mongrel server 启动时访问首页mysql连接属性时抛出以上错误,在对应的/usr/local/mysql/lib下根本没有/usr/local/mysql/lib/libmysqlclient.15.dylib这个文件,当然会加载失败,之前在 rails 2.0.2 上倒没有碰到过这个问题。从mysql官网重新下载了一个 mysql 5.0.67,从其下面拷了一个libmysqlclient.15.dylib 到 /usr/local/mysql/lib/libmysqlclient.15.dylib,就可以解决此问题。
$> sudo cp /usr/local/mysql5/lib/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib

Monday, December 08, 2008

MySQL5.0 同步错误 errno 2013 及解决方法

081208 11:28:29 [ERROR] Slave I/O thread: error connecting to master 'repl@server:3306': Error: 'Lost connection to MySQL server at 'reading initial communication packet', system error: 113' errno: 2013 retry-time: 60 retries: 86400

主要是因为slave连接不到master,可以从以下几点着手解决:
1. iptables是否将对应的master db port给禁了?
2. master db server 是否能够上网? slave db server 是否能够上网?

Saturday, September 27, 2008

MySQL Replication server_errno=2020

080927 15:28:42 [Note] Slave: connected to master 'test@localhost:3306',replication resumed in log 'mysql-bin.000003' at position 34699088
080927 15:28:42 [ERROR] Error reading packet from server: Got packet bigger than 'max_allowed_packet' bytes ( server_errno=2020)
080927 15:28:42 [Note] Slave I/O thread: Failed reading log event, reconnecting to retry, log 'mysql-bin.000003' position 34699088
080927 15:28:42 [Note] Slave: connected to master 'test@localhost:3306',replication resumed in log 'mysql-bin.000003' at position 34699088


$> vi /etc/my.cnf
# max_allowed_packet = 1M # modify it to
max_allowed_packet = 10M

Saturday, August 30, 2008

MySQL 数据库字符集问题

在rails项目开发中碰到一个MySQL数据库字符集问题 ,一个DEFAULT CHARSET utf8的数据库中,有一个ENGINE=MyISAM DEFAULT CHARSET=utf8的数据表,手工往其中插入中文,日文,韩文都没有问题,但通过rails添加数据记录时,中文,日文没有问题,但是插入韩文就一直乱码,rails和手工执行的sql相同,如下所示:


set names utf8;
insert TableName values('', '시험');

网上查不到什么相关资料,在rails中做了一些测试性的调整,都没有解决问题。最后决定调整mysql server的默认字符集,虽然觉得这个应该不会造成rails无法插入韩文,因为在rails中加过before_save前置过滤器,在其中先运行"set names utf8;",之后再执行insert操作,也一样是乱码。
在my.cnf中加入如下配置并重启mysql服务之后,rails就可以正常插入韩文了!!

# The MySQL server
[mysqld]
character-set-server = utf8


另摘录一段mysql server启动的参数选项说明:
·         --character-set-server=charset
使用charset作为 默认服务器字符
·         --collation-server=collation
使用collation作为 默认服务器校对规则
·         (DEPRECATED) --default-character-set=charset
使用char设置作为 默认字符集。由于--character-set-server,反对使用该选项。
·         --default-collation=collation
使用collation 作为默认校对规则。由于--collation-server,反对使用该选项。

Monday, August 25, 2008

MySQL int型字段说明

在mysql中int型字段,不管是int(1), int(4), int(11) 其最数值都是按int值来计算,无符号数为-2147483648止2147483647,即-2^31 ~ 2^31 - 1,不过需要注意以下关于显示宽度的说明。
int后面的参数说明:该可选显示宽度规定用于显示宽度小于指定的列宽度的值时从左侧填满宽度。
当结合可选扩展属性ZEROFILL使用时, 默认补充的空格用零代替。例如,对于声明为INT(5) ZEROFILL的列,值4检索为00004。请注意如果在整数列保存超过显示宽度的一个值,当MySQL为复杂联接生成临时表时会遇到问题,因为在这些情况下MySQL相信数据适合原列宽度。
Reference: mysql5.1 chinese manual

Friday, August 15, 2008

MySQL server_errno=1236 problem and resolution


mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: test
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000045
Read_Master_Log_Pos: 78736775
Relay_Log_File: localhost-relay-bin.000032
Relay_Log_Pos: 78736912
Relay_Master_Log_File: mysql-bin.000045
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB: search_production,dw_am
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 78736775
Relay_Log_Space: 78736912
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
1 row in set (0.00 sec)

Check mysql error log file, get below error info:
080815 12:13:44 [ERROR] Error reading packet from server: Client requested master to start replication from impossible position ( server_errno=1236)
080815 12:13:44 [ERROR] Got fatal error 1236: 'Client requested master to start replication from impossible position' from master when reading data from binary log
080815 12:13:44 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000045', position 78736775

Because master bin log file size is 78736164, but slave want to read 78736775, cause mysql 1236 error number, This problem caused by log flush exception.

-rw-rw---- 1 mysql mysql 78736164 Aug 15 10:18 mysql-bin.000045

This problem can be resolved using below method:

mysql> STOP SLAVE;
mysql> CHANGE MASTER TO MASTER_LOG_FILE=[NEXT BIN LOG FILE], MASTER_LOG_POS=98;
mysql> START SLAVE;

[NEXT BIN LOG FILE] is 'mysql-bin.000046' in this problem, MASTER_LOG_POS value is always 98 or 4.

Thursday, July 31, 2008

MyISAM 和 INNODB 引擎选择

看到一篇讨论MyISAM和INNODB的文章,分析这二种引擎的适用环境。
一般非事务性的数据存储用MyISAM引擎。
如果用INNODB也要注意修改my.cnf,按表存储。

Reference: http://mysqldba.blogspot.com/2008/07/what-should-i-use-myisam-or-innodb.html

Thursday, July 24, 2008

用MySQL拼出JSON串

假如有一个表users中有二个字段username和email,可以用以下语句获取一个json字符串。


SELECT
CONCAT("[",
GROUP_CONCAT(
CONCAT("{username:'",username,"'"),
CONCAT(",email:'",email),"'}")
)
,"]")
AS json FROM users;

Reference: http://www.thomasfrank.se/mysql_to_json.html

Friday, July 04, 2008

9.2.2. 识别符大小写敏感性[转自mysql5 中文手册]

在MySQL中,数据库对应数据目录中的目录。数据库中的每个表至少对应数据库目录中的一个文件(也可能是多个,取决于存储引擎)。因此,所使用操作系统的大小写敏感性决定了数据库名和表名的大小写敏感性。这说明在大多数Unix中数据库名和表名对大小写敏感,而在Windows中对大小写不敏感。一个显著的例外情况是Mac OS X,它基于Unix但使用默认文件系统类型(HFS+),对大小写不敏感。然而,Mac OS X也支持UFS卷,该卷对大小写敏感,就像Unix一样。

注释:尽管在某些平台中数据库名和表名对大小写不敏感,不应在同一查询中使用不同的大小写来引用给定的数据库或表。下面的查询不会工作,因为它同时引用了表my_tables和as MY_tables:

mysql> SELECT * FROM my_table WHERE MY_TABLE.col=1;
列、索引、存储子程序和触发器名在任何平台上对大小写不敏感,列的别名也不敏感。

默认情况,表别名在Unix中对大小写敏感,但在Windows或Mac OS X中对大小写不敏感。下面的查询在Unix中不会工作,因为它同时引用了别名a和A:

mysql> SELECT col_name FROM tbl_name AS a
-> WHERE a.col_name = 1 OR A.col_name = 2;
然而,该查询在Windows中是可以的。要想避免出现差别,最好采用一致的转换,例如总是用小写创建并引用数据库名和表名。在大多数移植和使用中建议使用该转换。

在MySQL中如何在硬盘上保存和使用表名和数据库名由lower-case-table-names系统变量确定,可以在启动mysqld时设置。lower-case-table-names可以采用下面的任一值:

0:
使用CREATE TABLE或CREATE DATABASE语句指定的大写和小写在硬盘上保存表名和数据库名。名称比较对大小写敏感。在Unix系统中的默认设置即如此。请注意如果在大小写不敏感的文件系统上用--lower-case-table-names=0强制设为0,并且使用不同的大小写访问MyISAM表名,会导致索引破坏。

1:
表名在硬盘上以小写保存,名称比较对大小写敏感。MySQL将所有表名转换为小写以便存储和查找。该行为也适合数据库名和表的别名。该值为Windows和Mac OS X系统中的默认值。

2:
表名和数据库名在硬盘上使用CREATE TABLE或CREATE DATABASE语句指定的大小写进行保存,但MySQL将它们转换为小写以便查找。名称比较对大小写敏感。注释:只在对大小写不敏感的文件系统上适用! InnoDB表名以小写保存,例如lower-case-table-names=1。

在Windows和Mac OS X中,lower-case-table-names的 默认值是1。

提示,手册中lower-case-table-names拼写有误,以此文为准。

Friday, February 01, 2008

MySQL 同步1064错误及补救方法

Last_Error: Error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '` = 37938' at line 1' on query. Default database: 'search_production'. Query: 'UPDATE tests SET `created_at` = 1195728959, `published_at` = 11d` = 37938'

slaver服务器上收到这个奇怪的1064错误,从master server传过来的要执行的语句应该是:
UPDATE tests SET `created_at` = 1195728959, `published_at` = 1195728081 where `id` = 37938
却变成了上面的样子,在确认此条语句重要性不大后在从服务器上执行以下语句跳过此错误继续同步数据。
mysql> SET GLOBAL SQL_slave_SKIP_COUNTER = n;
mysql> START SLAVE;
按mysql5.1手册上说,在从服务器上没有过直接的数据操作不应该出现这类错误。

Thursday, January 31, 2008

Mysql master and slaver server日志文件清理方法

一、要在MASTER要清理日志,需按照以下步骤:
1. 在每个从属服务器上,使用SHOW SLAVE STATUS来检查它正在读取哪个日志。
2. 使用SHOW MASTER LOGS获得主服务器上的一系列日志。
3. 在所有的从属服务器中判定最早的日志。这个是目标日志。如果所有的从属服务器是更新的,这是清单上的最后一个日志。
4. 制作您将要删除的所有日志的备份。(这个步骤是自选的,但是建议采用。)
5. 清理所有的日志,但是不包括目标日志。
二、要在SLAVER上清理日志,需要按以下步骤:
1. 在本机上使用SHOW SLAVE STATUS来检查它正在使用哪个Relay_Log_File日志。
2. 保留此目标日志,可以删除之前的中继日志文件。
(中继日志也可以将本机MYSQL SERVER SHUTDOWN后,将hostname-relay-bin.index, hostname-relay-bin.*, relay-log.info删除之后重启MYSQL SERVER,其中master.info文件不能删除)
3. 使用SHOW MASTER LOGS获得本机上的一系列日志。
4. 保留最新一个,可以删除之前的bin-log文件。
5. 建议删除之前都先备份,删除后重启SERVER看是否正常同步数据,把当前正式使用的中继日志和bin-log日志删除可能会导致同步不可用。
三、如果主机更新了replication slave user的密码,在SLAVE上执行:
mysql> STOP SLAVE; -- if replication was running
mysql> CHANGE MASTER TO MASTER_PASSWORD='new3cret';
mysql> START SLAVE; -- if you want to restart replication
四、CHANGE MASTER 使用注意
CHANGE MASTER会删除所有的中继日志文件并启动一个新的日志,除非指定了RELAY_LOG_FILE或RELAY_LOG_POS,在此情况下,中继日志被保持;relay_log_purge全局变量被静默地设置为0。
CHANGE MASTER TO可以更新master.info和relay-log.info文件的内容。

Monday, December 24, 2007

[Mysql] Row size too large

Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs

一个表有130个varchar(255)字段,GBK编码,建表报以上错误,原因如下:
130 * 255 * 2 = 66560 > 66535 (GBK 2字节)
表字段长度总和Mysql有限制.如果表是utf-8的话,按3字节计算.

Tuesday, November 27, 2007

PHP/Mysql新版本安装问题

php安装了不知道多少次,经常会碰到一些安装问题,这次装5.2.5碰到2个错误如下:
configure: error: xml2-config not found. Please check your libxml2 installation
在CentOS5.0中执行以下命令:
$> yum install libxml2-devel

接下来又有新问题了(用rpm包装的mysql server 5.1.22):
Note that the MySQL client library is not bundled anymore!

configure: error: Cannot find MySQL header files under /usr/shared/mysql.
Note that the MySQL client library is not bundled anymore.
ERROR: Could not configure PHP

到mysql官网下载对应的5.1.22 mysql-devel开发包,安装完再装PHP即可。