[跟我学中小企业架构部署]之三:数据库多实例部署

DB1 DB2 部署
所需软件:
mysql-5.0.56.tar.gz

安装mysql

1
cd /root/tools

创建mysql帐号:

1
2
groupadd mysql
useradd -g mysql -M -s /sbin/nologin mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
tar zxvf mysql-5.0.56.tar.gz
cd mysql-5.0.56
./configure \
--prefix=/usr/local/mysql \
--enable-assembler \
--with-extra-charsets=complex \
--enable-thread-safe-client \
--with-big-tables \
--with-readline \
--with-ssl \
--with-embedded-server \
--enable-local-infile \
--with-plugins=partition,innobase \
--with-plugin-PLUGIN \
--with-mysqld-ldflags=-all-static \
--with-client-ldflags=-all-static
make && make install

cd ..

创建数据目录

1
2
3
4
mkdir -p /data/3306/data
mkdir -p /data/3307/data
chown -R mysql:mysql /data/3306
chown -R mysql:mysql /data/3307

建立3306,3307 my.cnf配置文件

1
2
vi /data/3306/my.cnf
vi /data/3307/my.cnf

my.cnf内容:(3307 my.cnf就是3306 批量替换3307就好)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#/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 = /usr/local/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-bin=mysql3306-bin
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
interactive_timeout = 60
wait_timeout = 60
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_err.log
pid-file=/data/3306/mysqld.pid

批量替换方法:

1
sed -i "s/3306/3307/g"  /data/3306/my.cnf  >/data/3307/my.cnf

改变my.cnf配置文件的所有权

1
2
chown -R mysql:mysql /data/3306/my.cnf
chown -R mysql:mysql /data/3307/my.cnf

建立mysql启动脚本

1
2
vi /data/3306/mysql
vi /data/3307/mysql

mysql 内容: (3307的mysql需把port改为3307)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#!/bin/sh

#init
port=3306
mysql_user="root"
mysql_pwd=""
CmdPath="/usr/local/mysql/bin"

#startup function
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
}

#stop function
function_stop_mysql()
{
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 6
function_start_mysql
}
if [ "$1" = "start" ]; then
function_start_mysql
elif [ "$1" = "stop" ]; then
function_stop_mysql
elif [ "$1" = "restart" ]; then
function_restart_mysql
else
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
fi

批量替换方法:

1
sed -i "s/3306/3307/g"  /data/3306/mysql  >/data/3307/mysql

添加700权限并初始化数据库(仅root可管理)。

1
2
3
chmod 700 /data/3306/mysql
chmod 700 /data/3307/mysql
/bin/cp /usr/local/mysql/bin/mysql* /sbin/

初始化数据库

1
2
3
4
cd /root/tools/mysql-5.0.56
./scripts/mysql_install_db --datadir=/data/3306/data
./scripts/mysql_install_db --datadir=/data/3307/data
chown -R mysql:mysql /data

启动数据库

1
2
/data/3306/mysql start
/data/3307/mysql start

添加为系统自启动

1
2
echo "/data/3306/mysql start" >>/etc/rc.local
echo "/data/3307/mysql start" >>/etc/rc.local

用 netstat –ant 命令查看mysql端口,若看到下图标记的端口,即MYSQL安装成功!

访问方法(安装完初始无密码):

1
2
3

mysql -uroot -p -S /data/3306/mysql.sock
mysql -uroot -p -S /data/3307/mysql.sock

更改root密码

1
2
/usr/local/mysql/bin/mysqladmin -u root password 'elain' -S /data/3306/mysql.sock
/usr/local/mysql/bin/mysqladmin -u root password 'elain' -S /data/3307/mysql.sock

登录MYSQL

1
2
mysql -uroot -p -S /data/3306/mysql.sock
mysql -uroot -p -S /data/3307/mysql.sock

清理掉系统默认(标记的)的多余的mysql用户 3306,3307

1
select user,host from mysql.user;

删除无用帐号

1
2
3
4
5


drop user ''@'db1';
drop user ''@'localhost';
drop user 'root'@'db1';

3307同理
处理后结果:

到此,MYSQL 安装完成,DB1 DB2上安装完全相同!!

文章目录
,