Friday, November 13, 2009
mysql ini 5.1
# MySQL Server Instance Configuration File
# ----------------------------------------------------------------------
# Generated by the MySQL Server Instance Configuration Wizard
#
#
# Installation Instructions
# ----------------------------------------------------------------------
#
# On Linux you can copy this file to /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options
# (@localstatedir@ for this installation) or to
# ~/.my.cnf to set user-specific options.
#
# On Windows you should keep this file in the installation directory
# of your server (e.g. C:\Program Files\MySQL\MySQL Server X.Y). To
# make sure the server reads the config file use the startup option
# "--defaults-file".
#
# To run run the server from the command line, execute this in a
# command line shell, e.g.
# mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"
#
# To install the server as a Windows service manually, execute this in a
# command line shell, e.g.
# mysqld --install MySQLXY --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"
#
# And then execute this in a command line shell to start the server, e.g.
# net start MySQLXY
#
#
# Guildlines for editing this file
# ----------------------------------------------------------------------
#
# In this file, you can use all long options that the program supports.
# If you want to know the options a program supports, start the program
# with the "--help" option.
#
# More detailed information about the individual options can also be
# found in the manual.
#
#
# CLIENT SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
[client]
port=3306
[mysql]
default-character-set=utf8
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Tools/mysql5.1/"
#Path to the database root
datadir="C:/Tools/mysql5.1/data/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=utf8
# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
# The maximum amount of concurrent sessions the MySQL server will
# allow. One of these connections will be reserved for a user with
# SUPER privileges to allow the administrator to login even if the
# connection limit has been reached.
max_connections=100
# Query cache is used to cache SELECT results and later return them
# without actual executing the same query once again. Having the query
# cache enabled may result in significant speed improvements, if your
# have a lot of identical queries and rarely changing tables. See the
# "Qcache_lowmem_prunes" status variable to check if the current value
# is high enough for your load.
# Note: In case your tables change very often or if your queries are
# textually different every time, the query cache may result in a
# slowdown instead of a performance improvement.
query_cache_size=0
# The number of open tables for all threads. Increasing this value
# increases the number of file descriptors that mysqld requires.
# Therefore you have to make sure to set the amount of open files
# allowed to at least 4096 in the variable "open-files-limit" in
# section [mysqld_safe]
table_cache=256
# Maximum size for internal (in-memory) temporary tables. If a table
# grows larger than this value, it is automatically converted to disk
# based table This limitation is for a single table. There can be many
# of them.
tmp_table_size=18M
# How many threads we should keep in a cache for reuse. When a client
# disconnects, the client's threads are put in the cache if there aren't
# more than thread_cache_size threads from before. This greatly reduces
# the amount of thread creations needed if you have a lot of new
# connections. (Normally this doesn't give a notable performance
# improvement if you have a good thread implementation.)
thread_cache_size=8
#*** MyISAM Specific options
# The maximum size of the temporary file MySQL is allowed to use while
# recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.
# If the file-size would be bigger than this, the index will be created
# through the key cache (which is slower).
myisam_max_sort_file_size=100G
# If the temporary file used for fast index creation would be bigger
# than using the key cache by the amount specified here, then prefer the
# key cache method. This is mainly used to force long character keys in
# large tables to use the slower key cache method to create the index.
myisam_sort_buffer_size=35M
# Size of the Key Buffer, used to cache index blocks for MyISAM tables.
# Do not set it larger than 30% of your available memory, as some memory
# is also required by the OS to cache rows. Even if you're not using
# MyISAM tables, you should still set it to 8-64M as it will also be
# used for internal temporary disk tables.
key_buffer_size=25M
# Size of the buffer used for doing full table scans of MyISAM tables.
# Allocated per thread, if a full scan is needed.
read_buffer_size=64K
read_rnd_buffer_size=256K
# This buffer is allocated when MySQL needs to rebuild the index in
# REPAIR, OPTIMZE, ALTER table statements as well as in LOAD DATA INFILE
# into an empty table. It is allocated per thread so be careful with
# large settings.
sort_buffer_size=256K
#*** INNODB Specific options ***
innodb_data_home_dir="C:/Tools/mysql5.1/data/innodb/"
# Use this option if you have a MySQL server with InnoDB support enabled
# but you do not plan to use it. This will save memory and disk space
# and speed up some things.
#skip-innodb
# Additional memory pool that is used by InnoDB to store metadata
# information. If InnoDB requires more memory for this purpose it will
# start to allocate it from the OS. As this is fast enough on most
# recent operating systems, you normally do not need to change this
# value. SHOW INNODB STATUS will display the current amount used.
innodb_additional_mem_pool_size=2M
# If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately once per second. Value 2
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately once per second.
innodb_flush_log_at_trx_commit=1
# The size of the buffer InnoDB uses for buffering log data. As soon as
# it is full, InnoDB will have to flush it to disk. As it is flushed
# once per second anyway, it does not make sense to have it very large
# (even with long transactions).
innodb_log_buffer_size=1M
# InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and
# row data. The bigger you set this the less disk I/O is needed to
# access data in tables. On a dedicated database server you may set this
# parameter up to 80% of the machine physical memory size. Do not set it
# too large, though, because competition of the physical memory may
# cause paging in the operating system. Note that on 32bit systems you
# might be limited to 2-3.5G of user level memory per process, so do not
# set it too high.
innodb_buffer_pool_size=47M
# Size of each log file in a log group. You should set the combined size
# of log files to about 25%-100% of your buffer pool size to avoid
# unneeded buffer pool flush activity on log file overwrite. However,
# note that a larger logfile size will increase the time needed for the
# recovery process.
innodb_log_file_size=24M
# Number of threads allowed inside the InnoDB kernel. The optimal value
# depends highly on the application, hardware as well as the OS
# scheduler properties. A too high value may lead to thread thrashing.
innodb_thread_concurrency=8
Thursday, November 12, 2009
安装、设置与启动MySql绿色版的方法
1、解压 mysql-noinstall-5.1.30-win32.zip
2、在 F 盘建立目录 MySql\MySqlServer5.1\
3、把解压的内容复制到 F:\MySql\MySqlServer5.1\
4、在 F:\MySql\MySqlServer5.1\ 中找 my-large.ini 把它复制成 my.ini
5、在 my.ini 中找 [mysqld] ,添加以下语句;
basedir="F:/MySql/MySqlServer5.1/"
datadir="F:/MySql/MySqlServer5.1/data/"
default-character-set=latin1 #utf8
default-storage-engine=innodb
max_allowed_packet =12M
#skip-networking #// 这句会忽略网络登陆
#bind-address=192.168.0.72 #// 如果加上这句 localhost 就用不了 只要改 user 表的 127.0.0.1 为 % 重启服务 就可以远程登陆
6、安装 MySQL_Administrator_1.2 绿色版:把 mysql-gui-tools-noinstall-5.0-r14-win32.zip 解压到 F:\MySql\MySQL GUI Tools 5.0
6.5、可以尝试手动启动 MySql 服务器,并用 MySQL_Administrator_1.2 和 console 登陆:
1、手动启动服务:cmd --> F:\MySql\MySqlServer5.1\bin\mysqld --console
会看到 InnoDB: The first specified datafile c:\ibdata\ibdata1 did not exist:
InnoDB: a new database to be created!
InnoDB: Setting file c:\ibdata\ibdata1 size to 209715200
InnoDB: Database physically writes the file full: wait... 等 很长的
最后看到 mysqld: ready for connections
Version: '5.1.2-alpha' socket: '' port: 3306
表示 MySql 服务已经启动,可以登陆了,这时: 登陆名是 root ,密码为空,IP 地址只能写 localhost 或 127.0.0.1 ,因为现在
root 的权限只允许本地登陆,远程登陆不可以,在本机写本机 IP 地址来登陆被 MySql 视为远程登陆,所以是登陆不了的,会报错 1130
2、MySQL_Administrator_1.2 登陆:到 F:\MySql\MySQL GUI Tools 5.0\ 运行 MySQLAdministrator.exe ,
填入 localhost或127.0.0.1 3306 root 密码为空 就可以登陆
3、用 console 登陆: cmd --> f:\MySql\MySqlServer5.1\bin\mysql -u root -p
密码为空
如果要在登陆时就选定数据库可以这样写:f:\MySql\MySqlServer5.1\bin\mysql -u root -p[密码] [数据库名]
当前情况举例:f:\MySql\MySqlServer5.1\bin\mysql -u root -p mysql 就是密码是空的,登陆的数据库是 mysql 库
4、修改root的密码、让root可以远程登陆、添加新用户
修改root的密码:在登陆后的 console 中输入
use mysql
update user set Password=PASSWORD('[密码]') where user='root';
让root可以远程登陆:在登陆后的 console 中输入
use mysql
update user set Host='%' where user='root' and Host='127.0.0.1';
添加新用户,用户名是 gary,密码为空,权限等于root,用户允许远程登陆 :在登陆后的 console 中输入
GRANT ALL PRIVILEGES ON *.* TO 'gary'@'%';
如果用户不可以远程登陆:GRANT ALL PRIVILEGES ON *.* TO 'gary'@'localhost';
然后用上面的方法修改gary的密码,root 改为 gary
5、手工停止 MySql 服务:cmd --> F:\MySql\MySqlServer5.1\bin\mysqladmin -u root shutdown
如果MySQL root用户账户有密码,你需要调用命令 F:\MySql\MySqlServer5.1\bin\mysqladmin -u root -p shutdown 并根据提示输入密码。
注意:修改密码、修改是否远程登陆,添加用户后必须重启MySql服务才生效 !!!!!!!!!!!!!!!!!!!!!!!!!!!
注意: MySQL权限系统中的用户完全独立于Windows下的登录用户。
7、添加 MySql 服务到windows服务中:
1、简易添加方法:cmd --> F:\MySql\MySqlServer5.1\bin\mysqld --install 这样用默认的 MySQL 为名称添加一个windows服务
这是,该服务的属性写着:F:\MySql\MySqlServer5.1\bin\mysqld MySQL
2、指定服务名称与指定启动选项文件的添加方法:
F:\MySql\MySqlServer5.1\bin\mysqld --install LevelDBServer --defaults-file=F:\MySql\MySqlServer5.1\my.ini
用 LevelDBServer 为名称来创建windows服务,指定 F:\MySql\MySqlServer5.1\my.ini 为MySql的启动选项文件
如果在服务安装命令中,在--install选项后面指定的服务名不是默认服务名(MySQL)。则从具有相同服务名的组中读取选项,并从标准选项文件读取选项。
服务器还从标准选项文件的[mysqld]组读取选项。你可以使用[mysqld]组中的选项用于所有MySQL 服务,还可以使用具有相同服务名的组,用于该服务名所对应的服务器。
该命令中,--install选项后面给出了默认服务名(MySQL)。如果未给出--defaults-file选项,该命令可以让服务器从标准选项文件的[mysqld]组中读数。
由于提供了--defaults-file选项,服务器只从命名文件的[mysqld]组读取选项。
注意:添加服务后该服务并未启动。重启电脑服务就会启动,要手动启动与关闭 MySql 服务用以下语句:
cmd --> NET START MySQL 或 NET START LevelDBServer , NET STOP MySQL 或 NET STOP LevelDBServer
8、测试MySQL安装
可以通过以下命令测试MySQL服务器是否工作:
C:\> F:\MySql\MySqlServer5.1\bin\mysqlshow
C:\> F:\MySql\MySqlServer5.1\bin\mysqlshow -u root mysql
C:\> F:\MySql\MySqlServer5.1\bin\mysqladmin version status proc
C:\> F:\MySql\MySqlServer5.1\bin\mysql test
如果mysqld对客户端程序TCP/IP连接的响应较慢,可能是DNS问题。此时,使用--skip-name-resolve选项启动 mysqld,在MySQL授权表的Host列只使用localhost和IP号。
可以通过 --pipe 或 --protocol=PIPE 选项强制 MySQL 客户端使用命名管道连接代替TCP/IP连接,或指定.(阶段)做为主机名。使用 --socket 选项指定管道名。
my.ini配置设置
my.ini配置设置
linux下/etc/mysql/my.cnf(windows下my.ini)
[client] 下添加
default-character-set=utf8 默认字符集为utf8
[mysqld] 添加
default-character-set=utf8 默认字符集为utf8
#character-set-server=utf8
collation-server=utf8_general_ci
init_connect='set collation_connection=utf8_general_ci'
init_connect='set names utf8' (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行;
注意该参数对于连接数据库的用户是超级用户组的用户将被忽略,这样是为了避免该参数导致数据库致命错误,而无法使用任何一个用户连接上修改该项配置)
修改好后,重新启动mysql 即可,查询一下show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
Tuesday, April 7, 2009
Adding User Accounts
From : http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
mysql>CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
mysql>GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
->WITH GRANT OPTION;
mysql>CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';
mysql>GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
->WITH GRANT OPTION;
mysql>CREATE USER 'admin'@'localhost';
mysql>GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';
mysql>CREATE USER 'dummy'@'localhost';
Tuesday, January 13, 2009
MySQL Cluster(MySQL 集群) 初试
转自: MySQL Cluster(MySQL 集群) 初试
MySQL Cluster 是MySQL适合于分布式计算环境的高实用、高冗余版本。它采用了NDB Cluster 存储引擎,允许在1个 Cluster 中运行多个MySQL服务器。在MyQL 5.0及以上的二进制版本中、以及与最新的Linux版本兼容的RPM中提供了该存储引擎。(注意,要想获得MySQL Cluster 的功能,必须安装 mysql-server 和 mysql-max RPM)。
目前能够运行MySQL Cluster 的操作系统有Linux、Mac OS X和Solaris(一些用户通报成功地在FreeBSD上运行了MySQL Cluster ,但MySQL AB公司尚未正式支持该特性)。
一、MySQL Cluster概述
MySQL Cluster 是一种技术,该技术允许在无共享的系统中部署“内存中”数据库的 Cluster 。通过无共享体系结构,系统能够使用廉价的硬件,而且对软硬件无特殊要求。此外,由于每个组件有自己的内存和磁盘,不存在单点故障。
MySQL Cluster 由一组计算机构成,每台计算机上均运行着多种进程,包括MySQL服务器,NDB Cluster 的数据节点,管理服务器,以及(可能)专门的数据访问程序。关于 Cluster 中这些组件的关系,请参见下图:


所有的这些节点构成一个完成的MySQL集群体系。数据保存在“NDB存储服务器”的存储引擎中,表(结构)则保存在“MySQL服务器”中。应用程序通过“MySQL服务器”访问这些数据表,集群管理服务器通过管理工具(ndb_mgmd)来管理“NDB存储服务器”。
通过将MySQL Cluster 引入开放源码世界,MySQL为所有需要它的人员提供了具有高可用性、高性能和可缩放性的 Cluster 数据管理。
二、MySQL Cluster 基本概念
“NDB” 是一种“内存中”的存储引擎,它具有可用性高和数据一致性好的特点。
MySQL Cluster 能够使用多种故障切换和负载平衡选项配置NDB存储引擎,但在 Cluster 级别上的存储引擎上做这个最简单。MySQL Cluster的NDB存储引擎包含完整的数据集,仅取决于 Cluster本身内的其他数据。
MySQL-Cluster集群研究
转自: MySQL-Cluster集群研究
一、介绍
这篇文档旨在介绍如何安装配置基于2台服务器的MySQL集群。并且实现任意一台服务器出现问题或宕机时MySQL依然能够继续运行。
注意!
虽然这是基于2台服务器的MySQL集群,但也必须有额外的第三台服务器作为管理节点,但这台服务器可以在集群启动完成后关闭。同时需要注意的是并不推荐在集群启动完成后关闭作为管理节点的服务器。尽管理论上可以建立基于只有2台服务器的MySQL集群,但是这样的架构,一旦一台服务器宕机之后集群就无法继续正常工作了,这样也就失去了集群的意义了。出于这个原因,就需要有第三台服务器作为管理节点运行。
另外,可能很多朋友都没有3台服务器的实际环境,可以考虑在VMWare或其他虚拟机中进行实验。
下面假设这3台服务的情况:
Server1: mysql1.vmtest.net 192.168.0.1
Server2: mysql2.vmtest.net 192.168.0.2
Server3: mysql3.vmtest.net 192.168.0.3
Servers1和Server2作为实际配置MySQL集群的服务器。对于作为管理节点的Server3则要求较低,只需对Server3的系统进行很小的调整并且无需安装MySQL,Server3可以使用一台配置较低的计算机并且可以在Server3同时运行其他服务。
[编辑]
二、在Server1和Server2上安装MySQL
从http://www.mysql.com上下载mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz
注意:必须是max版本的MySQL,Standard版本不支持集群部署!
以下步骤需要在Server1和Server2上各做一次
# mv mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz /usr/local/
# cd /usr/local/
# groupadd mysql
# useradd -g mysql mysql
# tar -zxvf mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz
# rm -f mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz
# mv mysql-max-4.1.9-pc-linux-gnu-i686 mysql
# cd mysql
# scripts/mysql_install_db --user=mysql
# chown -R root .
# chown -R mysql data
# chgrp -R mysql .
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
此时不要启动MySQL!
[编辑]
三、安装并配置管理节点服务器(Server3)
作为管理节点服务器,Server3需要ndb_mgm和ndb_mgmd两个文件:
从http://www.mysql.com上下载mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz
# mkdir /usr/src/mysql-mgm
# cd /usr/src/mysql-mgm
# tar -zxvf mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz
# rm mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz
# cd mysql-max-4.1.9-pc-linux-gnu-i686
# mv bin/ndb_mgm .
# mv bin/ndb_mgmd .
# chmod +x ndb_mg*
# mv ndb_mg* /usr/bin/
# cd
# rm -rf /usr/src/mysql-mgm
现在开始为这台管理节点服务器建立配置文件:
# mkdir /var/lib/mysql-cluster
# cd /var/lib/mysql-cluster
# vi config.ini
在config.ini中添加如下内容:
[NDBD DEFAULT]
NoOfReplicas=2
[MYSQLD DEFAULT]
[NDB_MGMD DEFAULT]
[TCP DEFAULT]
# Managment Server
[NDB_MGMD]
HostName=192.168.0.3 #管理节点服务器Server3的IP地址
# Storage Engines
[NDBD]
HostName=192.168.0.1 #MySQL集群Server1的IP地址
DataDir= /var/lib/mysql-cluster
[NDBD]
HostName=192.168.0.2 #MySQL集群Server2的IP地址
DataDir=/var/lib/mysql-cluster
# 以下2个[MYSQLD]可以填写Server1和Server2的主机名。
# 但为了能够更快的更换集群中的服务器,推荐留空,否则更换服务器后必须对这个配置进行更改。
[MYSQLD]
[MYSQLD]
保存退出后,启动管理节点服务器Server3:
# ndb_mgmd
启动管理节点后应该注意,这只是管理节点服务,并不是管理终端。因而你看不到任何关于启动后的输出信息。
[编辑]
四、配置集群服务器并启动MySQL
在Server1和Server2中都需要进行如下改动:
# vi /etc/my.cnf
[mysqld]
ndbcluster
ndb-connectstring=192.168.0.3 #Server3的IP地址
[mysql_cluster]
ndb-connectstring=192.168.0.3 #Server3的IP地址
保存退出后,建立数据目录并启动MySQL:
# mkdir /var/lib/mysql-cluster
# cd /var/lib/mysql-cluster
# /usr/local/mysql/bin/ndbd --initial
# /etc/rc.d/init.d/mysqld start
可以把/usr/local/mysql/bin/ndbd加到/etc/rc.local中实现开机启动。
注意:只有在第一次启动ndbd时或者对Server3的config.ini进行改动后才需要使用--initial参数!
[编辑]
五、检查工作状态
回到管理节点服务器Server3上,并启动管理终端:
# /usr/bin/ndb_mgm
键入show命令查看当前工作状态:(下面是一个状态输出示例)
[root@mysql3 root]# /usr/bin/ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.0.1 (Version: 4.1.9, Nodegroup: 0, Master)
id=3 @192.168.0.2 (Version: 4.1.9, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.3 (Version: 4.1.9)
[mysqld(API)] 2 node(s)
id=4 (Version: 4.1.9)
id=5 (Version: 4.1.9)
ndb_mgm>
如果上面没有问题,现在开始测试MySQL:
注意,这篇文档对于MySQL并没有设置root密码,推荐你自己设置Server1和Server2的MySQL root密码。
在Server1中:
# /usr/local/mysql/bin/mysql -u root -p
> use test;
> CREATE TABLE ctest (i INT) ENGINE=NDBCLUSTER;
> INSERT INTO ctest () VALUES (1);
> SELECT * FROM ctest;
应该可以看到1 row returned信息(返回数值1)。
如果上述正常,则换到Server2上重复上面的测试,观察效果。如果成功,则在Server2中执行INSERT再换回到Server1观察是否工作正常。
如果都没有问题,那么恭喜成功!
[编辑]
六、破坏性测试
将Server1或Server2的网线拔掉,观察另外一台集群服务器工作是否正常(可以使用SELECT查询测试)。测试完毕后,重新插入网线即可。
如果你接触不到物理服务器,也就是说不能拔掉网线,那也可以这样测试:在Server1或Server2上:
# ps aux | grep ndbd
将会看到所有ndbd进程信息:
root 5578 0.0 0.3 6220 1964 ? S 03:14 0:00 ndbd
root 5579 0.0 20.4 492072 102828 ? R 03:14 0:04 ndbd
root 23532 0.0 0.1 3680 684 pts/1 S 07:59 0:00 grep ndbd
然后杀掉一个ndbd进程以达到破坏MySQL集群服务器的目的:
# kill -9 5578 5579
之后在另一台集群服务器上使用SELECT查询测试。并且在管理节点服务器的管理终端中执行show命令会看到被破坏的那台服务器的状态。
测试完成后,只需要重新启动被破坏服务器的ndbd进程即可:
# ndbd
注意!前面说过了,此时是不用加--inital参数的!
至此,MySQL集群就配置完成了!
MySQL数据库存储引擎详解
官方文档
转自: MySQL数据库存储引擎详解
转自: MySQL数据库存储引擎详解_2
存储引擎是什么?
MySQL中的数据用各种不同的技术存储在文件(或者内存)中。这些技术中的每一种技术都使用不同的存储机制、索引技巧、锁定水平并且最终提供广泛的不同的功能和能力。通过选择不同的技术,你能够获得额外的速度或者功能,从而改善你的应用的整体功能。
例如,如果你在研究大量的临时数据,你也许需要使用内存存储引擎。内存存储引擎能够在内存中存储所有的表格数据。又或者,你也许需要一个支持事务处理的数据库(以确保事务处理不成功时数据的回退能力)。
这些不同的技术以及配套的相关功能在MySQL中被称作存储引擎(也称作表类型)。MySQL默认配置了许多不同的存储引擎,可以预先设置或者在 MySQL服务器中启用。你可以选择适用于服务器、数据库和表格的存储引擎,以便在选择如何存储你的信息、如何检索这些信息以及你需要你的数据结合什么性能和功能的时候为你提供最大的灵活性。
选择如何存储和检索你的数据的这种灵活性是MySQL为什么如此受欢迎的主要原因。其它数据库系统(包括大多数商业选择)仅支持一种类型的数据存储。遗憾的是,其它类型的数据库解决方案采取的“一个尺码满足一切需求”的方式意味着你要么就牺牲一些性能,要么你就用几个小时甚至几天的时间详细调整你的数据库。使用MySQL,我们仅需要修改我们使用的存储引擎就可以了。
在这篇文章中,我们不准备集中讨论不同的存储引擎的技术方面的问题(尽管我们不可避免地要研究这些因素的某些方面),相反,我们将集中介绍这些不同的引擎分别最适应哪种需求和如何启用不同的存储引擎。为了实现这个目的,在介绍每一个存储引擎的具体情况之前,我们必须要了解一些基本的问题。
如何确定有哪些存储引擎可用
你可以在MySQL(假设是MySQL服务器4.1.2以上版本)中使用显示引擎的命令得到一个可用引擎的列表。
mysql> show engines;
+------------+---------+------------------------------------------------------------+
| Engine | Support | Comment |
+------------+---------+------------------------------------------------------------+
| MyISAM | DEFAULT | Default engine as of MySQL 3.23 with great performance |
| HEAP | YES | Alias for MEMORY |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables |
| MERGE | YES | Collection of identical MyISAM tables |
| MRG_MYISAM | YES | Alias for MERGE |
| ISAM | NO | Obsolete storage engine, now replaced by MyISAM |
| MRG_ISAM | NO | Obsolete storage engine, now replaced by MERGE |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys |
| INNOBASE | YES | Alias for INNODB |
| BDB | NO | Supports transactions and page-level locking |
| BERKELEYDB | NO | Alias for BDB |
| NDBCLUSTER | NO | Clustered, fault-tolerant, memory-based tables |
| NDB | NO | Alias for NDBCLUSTER |
| EXAMPLE | NO | Example storage engine |
| ARCHIVE | NO | Archive storage engine |
| CSV | NO | CSV storage engine |
+------------+---------+------------------------------------------------------------+
16 rows in set (0.01 sec)
这个表格显示了可用的数据库引擎的全部名单以及在当前的数据库服务器中是否支持这些引擎。
对于MySQL 4.1.2以前版本,可以使用mysql> show variables like "have_%"(显示类似“have_%”的变量):
mysql> show variables like "have_%";
+------------------+----------+
| Variable_name | Value |
+------------------+----------+
| have_bdb | YES |
| have_crypt | YES |
| have_innodb | DISABLED |
| have_isam | YES |
| have_raid | YES |
| have_symlink | YES |
| have_openssl | YES |
| have_query_cache | YES |
+------------------+----------+
8 rows in set (0.01 sec)
你可以通过修改设置脚本中的选项来设置在MySQL安装软件中可用的引擎。如果你在使用一个预先包装好的MySQL二进制发布版软件,那么,这个软件就包含了常用的引擎。然而,需要指出的是,如果你要使用某些不常用的引擎,特别是CSV、RCHIVE(存档)和BLACKHOLE(黑洞)引擎,你就需要手工重新编译MySQL源码 。
使用一个指定的存储引擎
你可以使用很多方法指定一个要使用的存储引擎。最简单的方法是,如果你喜欢一种能满足你的大多数数据库需求的存储引擎,你可以在MySQL设置文件中设置一个默认的引擎类型(使用storage_engine 选项)或者在启动数据库服务器时在命令行后面加上--default-storage -engine或--default-table-type选项 。
更灵活的方式是在随MySQL服务器发布同时提供的MySQL客户端时指定使用的存储引擎。最直接的方式是在创建表时指定存储引擎的类型,向下面这样:
CREATE TABLE mytable (id int, title char(20)) ENGINE = INNODB
你还可以改变现有的表使用的存储引擎,用以下语句:
ALTER TABLE mytable ENGINE = MyISAM
然而,你在以这种方式修改表格类型的时候需要非常仔细,因为对不支持同样的索引、字段类型或者表大小的一个类型进行修改可能使你丢失数据。如果你指定一个在你的当前的数据库中不存在的一个存储引擎,那么就会创建一个MyISAM(默认的)类型的表。
各存储引擎之间的区别
为了做出选择哪一个存储引擎的决定,我们首先需要考虑每一个存储引擎提供了哪些不同的核心功能。这种功能使我们能够把不同的存储引擎区别开来。我们一般把这些核心功能分为四类:支持的字段和数据类型、锁定类型、索引和处理。一些引擎具有能过促使你做出决定的独特的功能,我们一会儿再仔细研究这些具体问题。
字段和数据类型
虽然所有这些引擎都支持通用的数据类型,例如整型、实型和字符型等,但是,并不是所有的引擎都支持其它的字段类型,特别是BLOG(二进制大对象)或者TEXT文本类型。其它引擎也许仅支持有限的字符宽度和数据大小。
这些局限性可能直接影响到你可以存储的数据,同时也可能会对你实施的搜索的类型或者你对那些信息创建的索引产生间接的影响。这些区别能够影响你的应用程序的性能和功能,因为你必须要根据你要存储的数据类型选择对需要的存储引擎的功能做出决策。
锁定
数据库引擎中的锁定功能决定了如何管理信息的访问和更新。当数据库中的一个对象为信息更新锁定了,在更新完成之前,其它处理不能修改这个数据(在某些情况下还不允许读这种数据)。
锁定不仅影响许多不同的应用程序如何更新数据库中的信息,而且还影响对那个数据的查询。这是因为查询可能要访问正在被修改或者更新的数据。总的来说,这种延迟是很小的。大多数锁定机制主要是为了防止多个处理更新同一个数据。由于向数据中插入信息和更新信息这两种情况都需要锁定,你可以想象,多个应用程序使用同一个数据库可能会有很大的影响。
不同的存储引擎在不同的对象级别支持锁定,而且这些级别将影响可以同时访问的信息。得到支持的级别有三种:表锁定、块锁定和行锁定。支持最多的是表锁定,这种锁定是在MyISAM中提供的。在数据更新时,它锁定了整个表。这就防止了许多应用程序同时更新一个具体的表。这对应用很多的多用户数据库有很大的影响,因为它延迟了更新的过程。
页级锁定使用Berkeley DB引擎,并且根据上载的信息页(8KB)锁定数据。当在数据库的很多地方进行更新的时候,这种锁定不会出现什么问题。但是,由于增加几行信息就要锁定数据结构的最后8KB,当需要增加大量的行,也别是大量的小型数据,就会带来问题。
行级锁定提供了最佳的并行访问功能,一个表中只有一行数据被锁定。这就意味着很多应用程序能够更新同一个表中的不同行的数据,而不会引起锁定的问题。只有InnoDB存储引擎支持行级锁定。
建立索引
建立索引在搜索和恢复数据库中的数据的时候能够显著提高性能。不同的存储引擎提供不同的制作索引的技术。有些技术也许会更适合你存储的数据类型。
有些存储引擎根本就不支持索引,其原因可能是它们使用基本表索引(如MERGE引擎)或者是因为数据存储的方式不允许索引(例如FEDERATED或者BLACKHOLE引擎)。
事务处理
事务处理功能通过提供在向表中更新和插入信息期间的可靠性。这种可靠性是通过如下方法实现的,它允许你更新表中的数据,但仅当应用的应用程序的所有相关操作完全完成后才接受你对表的更改。例如,在会计处理中每一笔会计分录处理将包括对借方科目和贷方科目数据的更改,你需要要使用事务处理功能保证对借方科目和贷方科目的数据更改都顺利完成,才接受所做的修改。如果任一项操作失败了,你都可以取消这个事务处理,这些修改就不存在了。如果这个事务处理过程完成了,我们可以通过允许这个修改来确认这个操作。
至此为止,我们介绍了MySQL存储引擎的相关基本概念。
MySQL Storage Engines
One of the greatest things about MySQL, other than being free, widely supported and fast, is the flexibility of choosing different storage engines for different tables.
Out of the box, MySQL comes with 7 storage engines, including an "example" stub storage engine that allows you to implement your own storage library.
What's the big deal about having all these options?
Every storage engine is completely different, designed to address a unique application need.
Not being locked down to a single storage engine (like Oracle), means you can optimize and choose the best tool for the job.
Tip: A well designed MySQL-powered application will use different storage engines for different tables. If you're still stuck with all MyISAM tables, now may be a good time to revisit.
MySQL Storage Engines Overview
MyISAM: The default engine. No transactions support, average data reliability. Offers great performance for read heavy applications. Most web services and data warehousing applications use MyISAM heavily.
HEAP: All in-memory. Very fast for data retrieval, however due to being stored only in memory - all data is lost on shutdown. Great for temporary tables.
Archive: Used for storing large amounts of data without indexes in a small footprint.
Merge: Collection of MyISAM tables logically merged together to provide a single view.
InnoDB: Transaction-safe storage engine, best suited for write heavy environments thanks to row-level locking. Offers good built-in recovery and solid data reliability. InnoDB engine was acquired by Oracle on 2005.
NDB: A clustered engine - data is automatically split and replicated across several machines, a.k.a data nodes. Best suited for applications that require high performance lookups with the highest possible degree of uptime and availability. Originally designed by Ericsson for the Telco market, NDB offers the highest levels of data reliability (99.999%). NDB works well in read heavy environments. For write heavy environments with multiple concurrent writes, consider InnoDB.
The biggest disadvantadge of NDB, is that by design your entire database must fit in memory. If your database size times 2 is too big to fit in memory, NDBCluster is not for you.
-
To make it easier to follow the unique characteristics of each storage engine, I created this magic quadrant diagram:

Examples:
Below are some examples of using the best storage engine for different tasks:
Search Engine - NDBCluster
Web stats logging - Flat file for the logging with an offline processing demon processing and writing all stats into InnoDB tables.
Financial Transactions - InnoDB
Session data - MyISAM or NDBCluster
Localized calculations - HEAP
Dictionary - MyISAM
Important notes about MyISAM tables:
1. Your tables will get corrupted eventually! Plan accordingly.
Tar the entire database directory daily and setup MySQL replication to a slave for an up-to-the-minute live backup.
2. Turn on auto-repair by adding this flag to your my.cnf file:
myisam-recover=backup,force
Or consider running a check-all-tables-and-email-me cronjob daily: See our MySQL Table Maintenance automation.
3. Super fast for read (select) operations.
4. Concurrent writes lock the entire table. Switch everything to offline processing where you can, to serialize writes without taking the database down. (Offline processing is golden and applies to all table types)
Important notes about HEAP/Memory tables:
While this type of table offers super fast retrieval, it only works well for small temporary tables.
If you try to load too much data into a Memory table, MySQL will start swapping information to disk and then you lose the benefits of an all-memory storage.
Important notes about InnoDB tables:
1. ACID transactions support. Row-level locking (compared to table level locking with MyISAM) means faster concurrent writes.
2. Doing a "SELECT Count(*) FROM table" without specifying any indexes is very slow on InnoDB and requires a full table scan. (With MyIsam this operation doesn't cost anything because MyIsam stores an internal record counter with each table).
If you need to "SELECT COUNT(*)" often on InnoDB tables, create MySQL insert/delete triggers that will increment/decrement a counter whenever records are added or deleted from the table.
3. Backup:
Doing a tar/rsync backup where you simply copy all files is not possible with InnoDB.
MySQLDump backup is too slow with InnoDB. (If you insist on using it, turn on these flags: --opt --compress)
The only viable fast backup option, which can also be used to populate new slave machines, is InnoDB Hot Backup.
4. Recovery:
InnoDB has built-in recovery that works 99% of the times automatically. Never try to move .frm or .ibd files around as a way of "helping" the database to recover. If the built-in recovery doesn't work, switch to your slave server and restore the primary from backup.
5. LOAD DATA INFILE is too slow with InnoDB. Consider using MyIsam tables for LOAD DATA operations.
6. InnoDB is less forgiving than MyIsam when it comes to queries on non indexes. InnoDB is going to "School" you into ensuring every single query and update statement runs on an index. Issue no index queries and you'll pay dearly in execution time.
7. Never ever change my.cnf INnoDB log file size while the database is running. You'll corrupt the log sequence number beyond repair.
8. To maximize InnoDB MySQL database performance, start with these my.cnf settings:
innodb_open_files = 500
innodb_file_per_table
innodb_buffer_pool_size = 250M
innodb_flush_log_at_trx_commit = 2
innodb_thread_concurrency =8
innodb_lock_wait_timeout = 500
interactive_timeout = 20
back_log = 75
table_cache = 300
thread_cache = 32
thread_concurrency = 8
wait_timeout = 30
connect_timeout = 10
9. If InnoDB crashes and the built-in recovery mechanism is unable to roll-back transactions, your database will not start. This is very important to understand. With MyISAM, even if a table gets corrupted, you can still start the database and everything will work normally. InnoDB will simply refuse to start until you restore the entire database from backup. Make sure you understand this principle and backup religiously.
Scalability:
Every successful web application eventually outgrows the capacity and throughput of a single database machine.
At that point you typically have two options - Replication or NDBCluster.
As always the choice depends on the needs of your application.
For read-heavy environments, use an NDBCluster or setup replication for n MyISAM slave read-only machines.
For write-heavy environments, InnoDB on an active/passive replication setup is typically the best choice. You may also want to experiment with an NDBCluster. An NDBCluster is generally going to be slower than InnoDB in write-heavy environments, but it offers a higher level of availability.
Helpful Scripts:
* Convert all tables to InnoDB
* MySQLDump one database to another (designed for a case where you cannot use InnoDB Hot backup and you have a lot of tables/databases to import. Script automatically retries, reopens connection and imports one table at a time)
* Add triggers to all tables for fast InnoDB count(*)
You can only have one delete/insert trigger per each table so if you're already using triggers, modify the code accordingly.
Thursday, September 18, 2008
mysql常用命令 2
Monday, September 8, 2008
常用MySQL的命令集锦
一、连接MySQL
格式: mysql -h主机地址 -u用户名 -p用户密码
1、例1:连接到本机上的MYSQL。
首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是:mysql>。
2、例2:连接到远程主机上的MYSQL。假设远程主机的IP为:110.110.110.110,用户名为root,密码为abcd123。则键入以下命令:
mysql -h110.110.110.110 -uroot -pabcd123
(注:u与root可以不用加空格,其它也一样)
3、退出MYSQL命令: exit (回车)。
二、修改密码
格式:mysqladmin -u用户名 -p旧密码 password 新密码
1、例1:给root加个密码ab12。首先在DOS下进入目录mysqlbin,然后键入以下命令:
mysqladmin -uroot -password ab12
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。
2、例2:再将root的密码改为djg345。
mysqladmin -uroot -pab12 password djg345
三、增加新用户。(注意:和上面不同,下面的因为是MySQL环境中的命令,所以后面都带一个分号作为命令结束符)
格式:grant select on 数据库.* to 用户名@登录主机 identified by \"密码\"
例1、增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令:
grant select,insert,update,
delete on *.* to test1@\"%\" Identified by \"abc\";
但例1增加的用户是十分危险的,你想如某个人知道test1的密码,那么他就可以在internet上的任何一台电脑上登录你的MySQL数据库并对你的数据可以为所欲为了,解决办法见例2。
例2、增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作 (localhost指本地主机,即MySQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据 库,只能通过MySQL主机上的web页来访问。
grant select,insert,update,
delete on mydb.* to test2@localhost identified by \"abc\";
如果你不想test2有密码,可以再打一个命令将密码消掉。
grant select,insert,update,delete on mydb
.* to test2@localhost identified by \"\";
在上面讲了登录、增加用户、密码更改等问题。下面我们来看看MySQL中有关数据库方面的操作。注意:你必须首先登录到MySQL中,以下操作都是在MySQL的提示符下进行的,而且每个命令以分号结束。
一、操作技巧
1、如果你打命令时,回车后发现忘记加分号,你无须重打一遍命令,只要打个分号回车就可以了。也就是说你可以把一个完整的命令分成几行来打,完后用分号作结束标志就完成。
2、你可以使用光标上下键调出以前的命令。但以前我用过的一个MySQL旧版本不支持。我现在用的是mysql-3.23.27-beta-win。
二、显示命令
1、显示数据库列表:
show databases;
刚开始时才两个数据库:mysql和test。MySQL库很重要它里面有MYSQL的系统信息,我们改密码和新增用户,实际上就是用这个库进行操作。
2、显示库中的数据表:
use mysql; //打开库,学过FOXBASE的一定不会陌生吧
show tables;
3、显示数据表的结构:
describe 表名;
4、建库:
create database 库名;
5、建表:
use 库名;
create table 表名 (字段设定列表);
6、删库和删表:
drop database 库名;
drop table 表名;
7、将表中记录清空:
delete from 表名;
8、显示表中的记录:
select * from 表名;
三、一个建库和建表以及插入数据的实例
drop database if exists school; //如果存在SCHOOL则删除
create database school; //建立库SCHOOL
use school; //打开库SCHOOL
create table teacher //建立表TEACHER
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default ’深圳’,
year date
); //建表结束
//以下为插入字段
insert into teacher values(’’,’glchengang’,’深圳一中’,’1976-10-10’);
insert into teacher values(’’,’jack’,’深圳一中’,’1975-12-23’);
注:在建表中(1)将ID设为长度为3的数字字段:int(3)并让它每个记录自动加一:auto_increment并不能为空:not null而且让他成为主字段primary key(2)将NAME设为长度为10的字符字段(3)将ADDRESS设为长度50的字符字段,而且缺省值为深圳。varchar和char有什么区别 呢,只有等以后的文章再说了。(4)将YEAR设为日期字段。
如果你在MySQL提示符键入上面的命令也可以,但不方便调试。你可以将以上命令原样写入一个文本文件中假设为school.sql,然后复制到c:\\下,并在DOS状态进入目录\\mysql\\bin,然后键入以下命令:
mysql -uroot -p密码 <>
如果成功,空出一行无任何显示;如有错误,会有提示。(以上命令已经调试,你只要将//的注释去掉即可使用)。
四、将文本数据转到数据库中
1、文本数据应符合的格式:字段数据之间用tab键隔开,null值用\\n来代替。
例:
3 rose 深圳二中 1976-10-10
4 mike 深圳一中 1975-12-23
2、数据传入命令 load data local infile \"文件名\" into table 表名。
注意:你最好将文件复制到\\mysql\\bin目录下,并且要先用use命令打表所在的库。
五、备份数据库:
1、mysqldump --opt school>school.bbb
mysqldump --opt school>school.bbb
(命令在DOS的\\mysql\\bin目录下执行)
注释:将数据库school备份到school.bbb文件,school.bbb是一个文本文件,文件名任取,打开看看你会有新发现。
后记:其实MySQL的对数据库的操作与其它的SQL类数据库大同小异,您最好找本将SQL的书看看。我在这里只介绍一些基本的,其实我也就只懂这些了, 呵呵。最好的MYSQL教程还是“晏子“译的“MYSQL中文参考手册“不仅免费每个相关网站都有下载,而且它是最权威的。可惜不是象\"PHP4中文手 册\"那样是chm的格式,在查找函数命令的时候不太方便。
2、修改登录密码
1) mysqladmin -u用户名 -p旧密码 password 新密码:
例:mysqladmin -u root password 21century
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。
2)直接修改user表的root用户口令:
mysql> user mysql;
mysql> update user set pasword=password('21century') where user='root';
mysql> flush privileges;
注:flush privileges的意思是强制刷新内存授权表,否则用的还是缓冲中的口令。
3、测试密码是否修改成功:
1)不用密码登录:
[root@test1 local]# mysql
ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)
显示错误,说明密码已经修改。
2)用修改后的密码登录:
[root@test1 local]# mysql -u root -p
Enter password: (输入修改后的密码21century)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 177 to server version: 3.23.48
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
成功!
这是通过mysqladmin命令修改口令,也可通过修改库来更改口令。
4、启动和停止:
启动:Mysql从3.23.15版本开始作了改动,默认安装后服务要用MySQL用户来启动,不允许root用户启动。
如果非要用root用户来启动,必须加上--user=root参数(./safe_mysqld --user=root &)停止:mysqladmin -u root -p shutdown。
5、导出meeting数据库:
mysqldump -uroot -p21century meeting > db_meeting.sql
导人dbname数据库:
mysqldump -uroot -p21century dbname <>
导入数据库还可用类似于oracle中@my_script.sql的方式一次执行大量sql语句,这在使用mysqldump不起作用时非常有用。
例:#./mysql -uroot -p < /home/xuys/db_meeting_2003.sql
(注:create database、use databasename、create table和insert into语句都可写在上面的脚步文件中)
6、给表改名:
RENAME TABLE ztemp TO ztemp4;
7、修改字段属性:
ALTER TABLE bbabase CHANGE news_id id VARCHAR(5) NOT NULL;
8、在表中的content后增加一字段:
ALTER TABLE bbabase ADD leave_time DATETIME NOT NULL AFTER