1.多实例定义:
同一套mysql程序,不同的“my.cnf,启动程序,端口”。属于逻辑上面的多实例。
2.下载mysql-5.5.32.tar.gz
3.安装相关依赖:
yum install ncurses-devel libaio-devel cmake gcc-c++
4.创建用户:
useradd mysql -s /sbin/nologin -M
5.解压缩开始编译:
[plain] view plain copy
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5.32/ -DMYSQL_DATADIR=/data/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/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
make&&make install
6.创建实例目录:
mkdir -p /data/{3306,3307}/data
7.创建配置文件:
PS:server-id 每个实例要不一样。
vim /data/3306/my.cnf
[plain] view plain copy
[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-5.5.32
datadir = /data/3306/data
open_files_limit = 1024
character-set-server = utf8
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 = 16G #设置成内存的一半最好
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_3306.err
pid-file=/data/3306/mysqld.pid
vim /data/3307/my.cnf
[plain] view plain copy
[client]
port = 3307
socket = /data/3307/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port = 3307
socket = /data/3307/mysql.sock
basedir = /usr/local/mysql-5.5.32
datadir = /data/3307/data
open_files_limit = 1024
character-set-server = utf8
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 = 16G #设置成内存的一半
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_3307.err
pid-file=/data/3307/mysqld.pid
8.创建启动脚本:
vim /data/3306/mysql
#3307只要改动port参数就可以
[plain] view plain copy
#!/bin/sh
#init
port=3306
mysql_user=“root”
mysql_pwd=“Root123”
cmdpath=“/usr/local/mysql-5.5.32/bin”
mysql_sock=“/data/KaTeX parse error: Expected 'EOF', got '#' at position 22: …/mysql.sock" #̲startup functio…mysql_sock” ];then
echo “starting mysql…”
/bin/sh
c
m
d
p
a
t
h
/
m
y
s
q
l
d
s
a
f
e
−
−
d
e
f
a
u
l
t
s
−
f
i
l
e
=
/
d
a
t
a
/
{cmdpath}/mysqld_safe --defaults-file=/data/
cmdpath/mysqldsafe −−defaults−file=/data/{port}/my.cnf 2>&1 > /devull &
else
echo “mysql is running…”
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e “
m
y
s
q
l
s
o
c
k
"
]
;
t
h
e
n
e
c
h
o
"
m
y
s
q
l
i
s
s
t
o
p
p
e
d
…
"
e
x
i
t
e
l
s
e
e
c
h
o
"
s
t
o
p
i
n
g
m
y
s
q
l
…
"
mysql_sock" ];then echo "mysql is stopped…" exit else echo "stoping mysql…"
mysqlsock" ];then echo "mysql is stopped…" exit else echo "stoping mysql…" {cmdpath}/mysqladmin -u
m
y
s
q
l
u
s
e
r
−
p
{mysql_user} -p
mysqluser −p{mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
echo “restarting mysql…”
function_stop_mysql
sleep 2
function_start_mysql
}
case
1
i
n
s
t
a
r
t
)
f
u
n
c
t
i
o
n
s
t
a
r
t
m
y
s
q
l
;
;
s
t
o
p
)
f
u
n
c
t
i
o
n
s
t
o
p
m
y
s
q
l
;
;
r
e
s
t
a
r
t
)
f
u
n
c
t
i
o
n
r
e
s
t
a
r
t
m
y
s
q
l
;
;
∗
)
e
c
h
o
"
u
s
a
g
e
:
/
d
a
t
a
/
1 in start) function_start_mysql ;; stop) function_stop_mysql ;; restart) function_restart_mysql ;; *) echo "usage: /data/
1 in start) functionstartmysql ;; stop) functionstopmysql ;; restart) functionrestartmysql ;; ∗) echo "usage: /data/{port}/mysql {start|stop|restart}”
esac
9.修改目录权限:
chown -R mysql:mysql /data/
find /data -type f -name mysql -exec chmod 700 {} ;
10.添加环境变量:
echo ‘export PATH=/usr/local/mysql-5.5.32/bin/:$PATH’ >> /etc/profile
11.初始化数据库:
cd /usr/local/mysql-5.5.32/scripts/
./mysql_install_db --basedir=/usr/local/mysql-5.5.32/ --datadir=/data/3306/data/ --user=mysql
./mysql_install_db --basedir=/usr/local/mysql-5.5.32/ --datadir=/data/3307/data/ --user=mysql
12.启动数据库:
/data/3306/mysql start
/data/3307/mysql start
13.设置数据库root密码:
mysqladmin -uroot password Root123 -S /data/3306/mysql.sock
mysqladmin -uroot password Root456 -S /data/3307/mysql.sock
14.登陆数据库:
mysql -uroot -pRoot123 -S /data/3306/mysql.sock
mysql -uroot -pRoot456 -S /data/3307/mysql.sock
15.远程登陆数据库:
mysql -uroot -pRoot123 -h 172.16.1.214 -P 3306