mysql多实例部署

一、数据库环境介绍
Mysql是oracle公司提供的开源数据库,可以通过重新编译来实现个性化需求。根据客要求,操作系统是Cent OS,要求mysql版本为5.0 。根据这一情况,我们决定通过rpm文件来进行安装,安装版本为:
MySQL-server-5.5.15-1.rhel4.i386.rpm
MySQL-client-5.5.15-1.rhel5.i386.rpm
二、安装过程
1.安装数据库软件及自带库
某些linux系统自带有mysql数据库的安装包,可以直接用,但为了需要我们需要安装一些新的,所以必须先卸载掉再安装,如下:
[root@ggate1 u02]# rpm -qa | grep -i mysql
mysql-5.0.77-3.el5
[root@ggate1 u02]# rpm -ev --nodeps mysql-5.0.77-3.el5
[root@ggate1 u02]# rpm -ivh MySQL-server-5.5.15-1.rhel4.i386.rpm
Preparing...            ########################################### [100%]
    1:MySQL-server     ########################################### [100%]

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h ggate1 password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems with the /usr/bin/mysqlbug script!
2.安装客户端软件
安装客户端软件,主要是为方面在服务器上进行一些参数配置操作及性能调整等
[root@ggate1 u02]# rpm -ivh MySQL-client-5.5.15-1.rhel5.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-client           ########################################### [100%]
3.配置密码文件
[root@ggate1 u02]# /etc/init.d/mysql start
Starting MySQL....[  OK  ]
[root@ggate1 u02]# /usr/bin/mysqladmin -u root password 'password'

--以上工作其实是白做了,后来客户要求用操作系统自带的mysql版本,mysql5.1,害得我们重新安装操作系统,非常郁闷!

以下部分就是从5.1上配置开始的

三、添加实例配置
Mysql多实例的配置。Mysql每个实例里面包含多个数据库,但每个数据库相当于oracle里的一个schema,所以从表相上看是多个数据库,实际上是一些schema级别的。每个实例有自己独立的配置,可以把具体的配置写到同一个my.cnf文件中,通过mysqld_multi来启动每个实例,不需要的时候就进行关闭。下面进行多实例配置:
1. 建mysql用户
[root@localhost ~]# useradd -G mysql mytest
[root@localhost ~]# passwd mytest
Changing password for user mytest.
New password: 
BAD PASSWORD: it is based on a dictionary word
Retype new password: 
passwd: all authentication tokens updated successfully.
2. 建数据库实例目录
[root@localhost ~]# mkdir  /home/mytest/mysql/datafile
[root@localhost ~]# mkdir -p /home/mytest/mysql/log
[root@localhost ~]# mkdir -p /home/mytest/mysql/logbin
3. 配置参数文件
[root@localhost ~]# vi /home/mytest/mysql/my.cnf
[mysqld_multi]
mysqld          = /usr/bin/mysqld_safe
mysqladmin      = /usr/bin/mysqladmin

user            = mytest
#password        = password

[client]
#port= 3306
#socket= /var/lib/mysql/mysql.sock


[mysqld20]
port= 3320
socket= /home/mytest/mysql/mysql.sock
pid-file        = /home/mytest/mysql/mysql.pid
log-error       = /home/mytest/mysql/mysql.err
language        = /usr/share/mysql/english/
datadir         = /home/mytest/mysql
basedir         = /home/mytest/mysql
log-bin         = /home/mytest/mysql/logbin/mysql-bin
log-bin-index   = /home/mytest/mysql/logbin/mysql-bin.index
user            = mytest
innodb_log_group_home_dir = /home/mytest/mysql/log
#skip-locking


back_log = 50
join_buffer_size = 4M
max_connections = 150
table_cache = 256
thread_cache = 32
thread_concurrency = 16
tmp_table_size = 128M
default-character-set = gbk
query_cache_size = 16M
query_cache_type = 1


skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 512K
read_rnd_buffer_size = 2M
myisam_sort_buffer_size = 8M

#log-bin=mysql-bin

binlog_format=mixed

server-id= 1

innodb_data_home_dir = /home/mytest/mysql/datafile
innodb_data_file_path = ibdata1:10M:autoextend

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

模板放在/etc/sqlmodule/module.cnf
4.更改文件夹及文件属性
[root@localhost ~]# chown -R mytest:mysql /home/mytest/mysql
[root@localhost ~]# cd /home
[root@localhost home]# ls -ll
total 24
drwx------. 5 jrb07  jrb07   4096 Mar  9 12:50 jrb07
drwx------. 2 root   root   16384 Mar  9 10:45 lost+found
drwx------. 5 mytest mytest  4096 Mar  9 12:55 mytest
[root@localhost home]# cd mysql
-bash: cd: mysql: No such file or directory
[root@localhost home]# cd mytest
[root@localhost mytest]# ls -ll
total 4
drwxr-xr-x. 5 mytest mysql 4096 Mar  9 12:59 mysql
[root@localhost mytest]# cd mysql
[root@localhost mysql]# ls -ll
total 16
drwxr-xr-x. 2 mytest mysql 4096 Mar  9 12:55 datafile
drwxr-xr-x. 2 mytest mysql 4096 Mar  9 12:55 log
drwxr-xr-x. 2 mytest mysql 4096 Mar  9 12:55 logbin
-rw-r--r--. 1 mytest mysql 1474 Mar  9 12:59 my.cnf
5. 初始化实例,建立基本数据字典
[root@localhost mysql]# su -mytest
[mytest@localhost ~]$ mysql_install_db --datadir=/home/mytest/mysql --user=mytest
Installing MySQL system tables...
120309 13:04:36 [Warning] Ignoring user change to 'mytest' because the user was set to 'mysql' earlier on the command line

OK
Filling help tables...
120309 13:04:36 [Warning] Ignoring user change to 'mytest' because the user was set to 'mysql' earlier on the command line

OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!
6. 启动实例
[mytest@localhost ~]$ mysqld_multi --defaults-file=/home/mytest/mysql/my.cnf start 20
WARNING: Log file disabled. Maybe directory or file isn't writable?
mysqld_multi log file version 2.16; run: Fri Mar  9 13:05:46 2012

Starting MySQL servers
[mytest@localhost ~]$ 120309 13:05:46 mysqld_safe Logging to '/home/mytest/mysql/mysql.err'.
120309 13:05:46 mysqld_safe Starting mysqld daemon with databases from /home/mytest/mysql

--注意:这里启动后,会停在这里,其实实例已经启动,可以打开另外窗口检查

[mytest@localhost ~]$ ps -ef | grep mysql

mytest   27232     1  0 13:05 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --port=3320 --socket=/home/mytest/mysql/mysql.sock --pid-file=/home/mytest/mysql/mysql.pid --log-error=/home/mytest/mysql/mysql.err --language=/usr/share/mysql/english/ --datadir=/home/mytest/mysql --basedir=/home/mytest/mysql --log-bin=/home/mytest/mysql/logbin/mysql-bin --log-bin-index=/home/mytest/mysql/logbin/mysql-bin.index --user=mytest --innodb_log_group_home_dir=/home/mytest/mysql/log --back_log=50 --join_buffer_size=4M --max_connections=150 --table_cache=256 --thread_cache=32 --thread_concurrency=16 --tmp_table_size=128M --default-character-set=gbk --query_cache_size=16M --query_cache_type=1 --skip-external-locking --key_buffer_size=16M --max_allowed_packet=1M --table_open_cache=64 --sort_buffer_size=512K --net_buffer_length=8K --read_buffer_size=512K --read_rnd_buffer_size=2M --myisam_sort_buffer_size=8M --binlog_format=mixed --server-id=1 --innodb_data_home_dir=/home/mytest/mysql/datafile --innodb_data_file_path=ibdata1:10M:autoextend
mytest   27484 27232  0 13:05 pts/1    00:00:00 /usr/libexec/mysqld --basedir=/home/mytest/mysql --datadir=/home/mytest/mysql --language=/usr/share/mysql/english/ --log-bin=/home/mytest/mysql/logbin/mysql-bin --log-bin-index=/home/mytest/mysql/logbin/mysql-bin.index --innodb_log_group_home_dir=/home/mytest/mysql/log --back_log=50 --join_buffer_size=4M --max_connections=150 --table_cache=256 --thread_cache=32 --thread_concurrency=16 --tmp_table_size=128M --default-character-set=gbk --query_cache_size=16M --query_cache_type=1 --skip-external-locking --key_buffer_size=16M --max_allowed_packet=1M --table_open_cache=64 --sort_buffer_size=512K --net_buffer_length=8K --read_buffer_size=512K --read_rnd_buffer_size=2M --myisam_sort_buffer_size=8M --binlog_format=mixed --server-id=1 --innodb_data_home_dir=/home/mytest/mysql/datafile --innodb_data_file_path=ibdata1:10M:autoextend --log-error=/home/mytest/mysql/mysql.err --pid-file=/home/mytest/mysql/mysql.pid --socket=/home/mytest/mysql/mysql.sock --port=3320
mytest   27502 27143  0 13:06 pts/1    00:00:00 grep mysql
7. 授权及更改数据库用户管理密码
[mytest@localhost ~]$ mysql -uroot -p -S /home/mytest/mysql/mysql.sock
Enter password: (这里没有密码,直接回车)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.52-log Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> delete from user;
Query OK, 5 rows affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges  on *.* to root@'localhost'   identified   by 'qazxs w';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges  on *.* to mytest@'localhost'   identified   by 'qazxsw w';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from user;
+--------+-----------+-------------------------------------------+
| user   | host      | password                                  |
+--------+-----------+-------------------------------------------+
| root   | localhost | *F83365F186D5B45C390EBD9B01A200B1D113D0BC |
| mytest | localhost | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+--------+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> update user set host='%' where user = 'mytest';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
  select user,host,password from user;
+--------+-----------+-------------------------------------------+
| user   | host      | password                                  |
+--------+-----------+-------------------------------------------+
| root   | localhost | *F83365F186D5B45C390EBD9B01A200B1D113D0BC |
| mytest | %         | *437F1809645E0A92DAB553503D2FE21DB91270FD |
+--------+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

至此,一个实例 已经建立起来,并且可以完全使用!
四、实例测试
新建数据库(在实例4上进行的)
mysql> create database mytest;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mytest             |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
新建表
mysql> create table mytable (id int,name char(20),birthdate date);
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| mytable          |
+------------------+
1 row in set (0.00 sec)
五、主要参数说明
MySQL 支持超过 100  个的可调节设置;但是幸运的是,掌握少数几个就可以满足大部分需要。查找第二世界整理发布这些设置的正确值可以通过 SHOW  STATUS 命令查看状态变量,从中可以确定 mysqld的运作情况是否符合我们的预期。给缓冲区和缓存分配的内存不能超过系统中的现有内存,因此调优通常都需要进行一些妥协。

1. skip-external-locking
避免MySQL的外部锁定,减少出错几率增强稳定性。
2. back_log
指定MySQL可能的连接数量。当MySQL主线程在很短的时间内接收到非常多的连接请求,该参数生效,主线程花费很短的时间检查连接并且启动一个新线程。back_log参数的值指出在MySQL暂时停止响应新请求之前的短时间内多少个请求可以被存在堆栈中。如果系统在一个短时间内有很多连接,则需要增大该参数的值,该参数值指定到来的TCP/IP连接的侦听队列的大小。不同的操作系统在这个队列大小上有它自己的限制。试图设定back_log高于你的操作系统的限制将是无效的。默认值为50。对于Linux系统推荐设置为小于512的整数。
3.key_buffer_size
指定用于索引的缓冲区大小,增加它可得到更好的索引处理性能。
4. sort_buffer_size
查询排序时所能使用的缓冲区大小。注意:该参数对应的分配内存是每连接独占!如果有100个连接,那么实际分配的总共排序缓冲区大小为100 *6= 600MB
5. read_buffer_size
读查询操作所能使用的缓冲区大小,每连接独享。
6. join_buffer_size 
联合查询操作所能使用的缓冲区大小,每连接独享
7. max_connections 
指定MySQL允许的最大连接进程数。
8. table_cache
打开表缓存总数,可以避免频繁的打开数据表产生的开销
9.thread_cache
缓存可重用的线程数
10.thread_concurrency
最大并发线程数,cpu数量*2
11. log-slow-queries\long_query_time
记录慢查询,然后对慢查询
12. tmp_table_size
临时缓存区,用于排序等
13. Default-character-set
字符集参数
14. query_cache_size
查询缓存区


六、用户权限管理
对每个各设置一个管理员用户,用于数据库的日常管理。设置的用户,只能管理自己的实例,而无权管理或者访问其它实例。
根据经验,通常对各实例设置的用户名为mysql或者root。设置好的用户及密码会写入相应实例的数据字典,在进行登录或者管理时,各自使用自己数据字典里的用户和密码进行验证。
例如:在测试环境上的机器实例说明:
mysql -uroot -p -S /home/mydata/mysql1/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.1.52-log Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
在上面输入的密码是:password1,通过数据字典验证通过!
如果不用密码,那么数据库会报错:
[root@localhost mysql]# mysql -uroot  -S /home/mydata/mysql1/mysql.sock
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
七、管理数据库
1.客户通过mysql连接到数据库对数据库进行管理,如toad、mysql客户端、mysql workbrench等工具,对实例进行管理,包括对该实例下的数据库、表、索引、存储过程、临时参数等等进行管理。
2.建立sftp用户,通过sftp用户来修改参数,ftp的设置由其他人配置。
3.数据文件目录:
/home/username1/mysql/log        ----日志文件夹
/home/username1/mysql/test       ----mysql自带空数据库夹
/home/username1/mysql/mysql     ----mysql数据库,mysql其它数据库运行基础
/home/username1/mysql/help      ----帮助文件夹
/home/username1/mysql/my.cnf    ----参数配置文件
/home/username1/mysql/mysql.err  ----错误文件
/home/username1/mysql/datafile   ----数据文件夹
/home/mysql1/mydata/logbin/     ----二进制日志文件
/home/mysql1/mydata/logbin/mysql-bin.index
以上文件夹是数据启动必须的,请不要删除。对于配置文件的修改,请首先做好备份后才予修改,否则可能导致数据库实例不能启动影响应用!
4.通过操作系统用户对目录进行配置读写权限后,在用户新增数据文件过程中,只有在参数文件中配置自己相应的相应目录,在可能实现启动实例成功!
八、数据备份
客户备份:
客户可以根据自己的情况对自己所负责的实例进行备份。备份的文件放到客户自己本地,包括数据文件、参数文件等。在备份的过程中,可以采用mysqldump命令或者其它第三方工具,备份策略可以根据自己的情况制定。
信息中心备份:
采用nbu备份方式进行,主要配置需要其他同事进行说明!
在本地机上备份脚本:
Su – mytest
Vi /home/mytest/mysql/backup.sh
mysqldump -uroot -pqazxsw11 -S /home/mytest/mysql/mysql.sock --all-databases --default-character-set=latin1 >/home/backup/mytest/full_dump$F_date.sql
rm -rf `find /home/backup/mytest/* -atime +10`

自动执行脚本:
Crontab –e 
1 1 * * * /home/mytest/mysql/backup.sh

在这里,都是在mytest用户下操作的,并且希望将backup.sh放到/home/mytest/mysql/
请使用浏览器的分享功能分享到微信等