MySQL多实例安装
1.安装MySQL需要的依赖的包和编译软件
(1)安装MySQL需要的依赖包
安装MySQL之前,最好先安装MySQL需要的依赖包,不然后面会出现报错,还得回来安装MySQL的依赖包。
[root@Mysql-server tools]# yum install -y ncurses-devel libaio-devel
[root@Mysql-server tools]# rpm -qa ncurses-devel libaio-devel
ncurses-devel-5.7-4.20090207.el6.x86_64
libaio-devel-0.3.107-10.el6.x86_64
(2)安装编译MySQL需要的软件
要安装cmake编译软件:
[root@Mysql-server tools]# tar xf cmake-2.8.8.tar.gz
[root@Mysql-server tools]# cd cmake-2.8.8
[root@Mysql-server cmake-2.8.8]#./configure
[root@mysql cmake-2.8.8]# gmake
[root@mysql cmake-2.8.8]# gmake install
2.开始安装MySQL
2.1 创建用户mysql
[root@mysql tools]# useradd mysql -s /sbin/nologin -M
[root@mysql tools]# id mysql uid=501(mysql) gid=501(mysql) 组=501(mysql) [root@mysql tools]#
2.2 解压编译MySQL
tar zxf mysql-5.5.32.tar.gz cd mysql-5.5.32
cmake .-DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \
-DMYSQL_DATADIR=/application/mysql-5.5.32/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
#-- Build files have been written to: /home/oldboy/tools/mysql-5.5.32
提示,编译时可配置的选项很多,具体可参考结尾附录或官方文档:
make
#[100%] Built target my_safe_process
make install
ln -s /application/mysql-5.5.32//application/mysql
如果上述操作未出现错误,则MySQL5.5.32软件cmake方式的安装就算成功了。
编译过程中出现如下报错,解决方法为:
[root@mysql mysql-5.5.32]# rm CMakeCache.txt ###删除这个文件,重新cmake
[root@mysql mysql-5.5.32]# mkdir -p /data/{3306,3307}/data
[root@mysql mysql-5.5.32]# tree /data/
/data/
├──3306
│??└── data ##实例3306的数据文件
├──3307
│??└── data ##实例3307的数据文件
└── test
2.3 接着创建配置文件,将data.zip文件上传到/(根)下,并且使用unzip命令解压
[root@mysql /]# unzip data.zip ##上传到根下,加压会替换上面创建的/data目录
Archive: data.zip
inflating: data/3306/my.cnf
inflating: data/3306/mysql
inflating: data/3307/my.cnf
inflating: data/3307/mysql
[root@mysql /]# tree /data
data/ data.zip
[root@mysql /]# tree data
data/ data.zip
[root@mysql /]# tree data/
data/
├──3306
│??├── data
│??├── my.cnf ##实例的配置文件
│??└── mysql ##实例的启动文件
├──3307
│??├── data
│??├── my.cnf ##实例的配置文件
│??└── mysql ##实例的启动文件
└── test
/data/3306/my.cnf配置文件信息
[root@mysql /]# cat /data/3306/my.cnf
[client]
port =3306
socket =/data/3306/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port =3306
socket =/data/3306/mysql.sock
basedir =/application/mysql
datadir =/data/3306/data
open_files_limit =1024
back_log =600
max_connections =800
max_connect_errors =3000
table_cache =614
external-locking = FALSE
max_allowed_packet =8M
sort_buffer_size =1M
join_buffer_size =1M
thread_cache_size =100
thread_concurrency =2
query_cache_size =2M
query_cache_limit =1M
query_cache_min_res_unit =2k
#default_table_type = InnoDB
thread_stack =192K
#transaction_isolation = READ-COMMITTED
tmp_table_size =2M
max_heap_table_size =2M
long_query_time =1
#log_long_format
#log-error = /data/3306/error.log
#log-slow-queries = /data/3306/slow.log
pid-file =/data/3306/mysql.pid
log-bin =/data/3306/mysql-bin
relay-log =/data/3306/relay-bin
relay-log-info-file =/data/3306/relay-log.info
binlog_cache_size =1M
max_binlog_cache_size =1M
max_binlog_size =2M
expire_logs_days =7
key_buffer_size =16M
read_buffer_size =1M
read_rnd_buffer_size =1M
bulk_insert_buffer_size =1M
#myisam_sort_buffer_size = 1M
#myisam_max_sort_file_size = 10G
#myisam_max_extra_sort_file_size = 10G
#myisam_repair_threads = 1
#myisam_recover
lower_case_table_names =1
skip-name-resolve
slave-skip-errors =1032,1062
replicate-ignore-db=mysql
server-id =1
innodb_additional_mem_pool_size =4M
innodb_buffer_pool_size =32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads =4
innodb_thread_concurrency =8
innodb_flush_log_at_trx_commit =2
innodb_log_buffer_size =2M
innodb_log_file_size =4M
innodb_log_files_in_group =3
innodb_max_dirty_pages_pct =90
innodb_lock_wait_timeout =120
innodb_file_per_table =0
[mysqldump]
quick
max_allowed_packet =2M
[mysqld_safe]
log-error=/data/3306/mysql_oldboy3306.err
pid-file=/data/3306/mysqld.pid
/data/3307/my.cnf配置文件参数
[root@mysql /]# cat /data/3307/my.cnf
[client]
port =3307
socket =/data/3307/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port =3307
socket =/data/3307/mysql.sock
basedir =/application/mysql
datadir =/data/3307/data
open_files_limit =1024
back_log =600
max_connections =800
max_connect_errors =3000
table_cache =614
external-locking = FALSE
max_allowed_packet =8M
sort_buffer_size =1M
join_buffer_size =1M
thread_cache_size =100
thread_concurrency =2
query_cache_size =2M
query_cache_limit =1M
query_cache_min_res_unit =2k
#default_table_type = InnoDB
thread_stack =192K
#transaction_isolation = READ-COMMITTED
tmp_table_size =2M
max_heap_table_size =2M
#long_query_time = 1
#log_long_format
#log-error = /data/3307/error.log
#log-slow-queries = /data/3307/slow.log
pid-file =/data/3307/mysql.pid
#log-bin = /data/3307/mysql-bin
relay-log =/data/3307/relay-bin
relay-log-info-file =/data/3307/relay-log.info
binlog_cache_size =1M
max_binlog_cache_size =1M
max_binlog_size =2M
expire_logs_days =7
key_buffer_size =16M
read_buffer_size =1M
read_rnd_buffer_size =1M
bulk_insert_buffer_size =1M
#myisam_sort_buffer_size = 1M
#myisam_max_sort_file_size = 10G
#myisam_max_extra_sort_file_size = 10G
#myisam_repair_threads = 1
#myisam_recover
lower_case_table_names =1
skip-name-resolve
slave-skip-errors =1032,1062
replicate-ignore-db=mysql
server-id =3
innodb_additional_mem_pool_size =4M
innodb_buffer_pool_size =32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads =4
innodb_thread_concurrency =8
innodb_flush_log_at_trx_commit =2
innodb_log_buffer_size =2M
innodb_log_file_size =4M
innodb_log_files_in_group =3
innodb_max_dirty_pages_pct =90
innodb_lock_wait_timeout =120
innodb_file_per_table =0
[mysqldump]
quick
max_allowed_packet =2M
[mysqld_safe]
log-error=/data/3307/mysql_oldboy3307.err
pid-file=/data/3307/mysqld.pid
特别说明:
本文多实例启动文件的启动mysql服务实质命令:
mysqld_safe --defaults-file=/data /3306/my.cnf 2>&1 /dev/null &
mysqld_safe --defaults-file=/data /3307/my.cnf 2>&1 /dev/null &
本文多实例启动文件的停止mysql服务实质命令:
mysqladmin -u root -p123456 -S /data/3306/mysql.sock shutdown
mysqladmin -u root -p123456 -S /data/3307/mysql.sock shutdown
2.4 修改数据文件/data 权限
[root@mysql /]# chown -R mysql.mysql /data
[root@mysql /]# find /data -type f -name "my.cnf"|xargs ls -l ##将my.cnf文件权限设置成644
-rw-r--r--.1 mysql mysql 189910月292013/data/3306/my.cnf
-rw-r--r--.1 mysql mysql 190110月292013/data/3307/my.cnf
[root@mysql /]# find /data -type f -name "mysql"|xargs ls -l ##由于mysql脚本中保存了登录数据库的密码,需要将文件权限设置成700,只允许root,mysql访问
-rw-r--r--.1 mysql mysql 13077月152013/data/3306/mysql
-rw-r--r--.1 mysql mysql 13077月212013/data/3307/mysql
[root@mysql /]# find /data -type f -name "mysql"|xargs chmod 700
[root@mysql /]# find /data -type f -name "mysql"|xargs ls -l
-rwx------.1 mysql mysql 13077月152013/data/3306/mysql
-rwx------.1 mysql mysql 13077月212013/data/3307/mysql
2.5 配置MySQL命令全局使用路径
如果不为MySQL的命令配置全局路径,就无法直接在命令行输入mysql,这样只能用全路径/application/msyq/bin/mysql 这样带着路径输出会比较麻烦
2.5.1 配置MySQL全局路径方法
1.确认mysql命令所在路径
[root@mysql /]# ll /application/mysql/bin/mysql
-rwxr-xr-x.1 root root 53453564月816:05/application/mysql/bin/mysql
2. 修改PATH路径
[root@mysql /]# tail -1/etc/profile
export PATH=/application/mysql/bin:$PATH ##添加如下行
[root@mysql /]# source /etc/profile ##让其生效
3. 最后检查PATH
[root@mysql /]# echo $PATH
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
2.6 初始化数据库
cd /application/mysql/scripts/##注意和MySQL 5.1的路径不同,MySQL 5.1 不在mysql/bin下
./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data/--user=mysql
./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data/--user=mysql
注意:
当初始化的过程中出现两个OK时,表示初始化成功
3. 最后启动mysql
[root@mysql scripts]#/data/3306/mysql start
StartingMySQL...
[root@mysql scripts]#/data/3307/mysql start
StartingMySQL...
查看是否启动成功
[root@mysql scripts]# netstat -lntup|grep mysql
tcp 000.0.0.0:33060.0.0.0:* LISTEN 4614/mysqld
tcp 000.0.0.0:33070.0.0.0:* LISTEN 5412/mysqld
4. 登录mysql
mysql -uroot -p123456 -S /data/3306/mysql.sock
5. 多实例修改mysql 密码
[root@mysql scripts]# mysqladmin -uroot password 123456 -S /data/3306/mysql.sock
[root@mysql scripts]# mysqladmin -uroot password 123456 -S /data/3307/mysql.sock
6. 修改配置文件中的mysql连接密码,如果不修改否则在停库的时候会报错
[root@mysql scripts]# sed -i '13 s#oldboy#123456#g'/data/3306/mysql
[root@mysql scripts]# sed -n '13p'/data/3306/mysql
mysql_pwd="123456"
[root@mysql scripts]# sed -i '13 s#oldboy#123456#g' /data/3307/mysql [root@mysql scripts]# sed -n '13p' /data/3307/mysql mysql_pwd="123456" [root@mysql scripts]#
7.停止mysql数据库
[root@mysql scripts]#/data/3306/mysql stop
StopingMySQL...
[root@mysql scripts]#/data/3307/mysql stop
StopingMySQL...
[root@mysql scripts]#
新增一个多实例库3308,操作命令如下
附件列表
########## 今天的苦逼是为了不这样一直苦逼下去!##########