CentOS7下yum安装MariaDB配置多实例

8 篇文章 0 订阅
7 篇文章 0 订阅

测试mysql 主从同步+Mycat,但是没有更多的服务器,听说一台服务器可以搭建多个mysql

yum安装MariaDB:

yum install mariadb mariadb-server

安装完成后要进行手动开启MySQL服务并初始化:

service mariadb start
[root@localhost ~]# mysql_secure_installation

 安装成功,测试结果:

[root@server187 multiMysql]#  mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.8-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

exit;退出mysql,开始安装多例:

[root@localhost home]# mkdir /home/multiMysql
[root@localhost home]# mkdir /home/multiMysql/{etc,socket,bin,datadir,tmp}

在/home目录下创建multiMysql文件夹,并在里面创建etc,socket,bin,datadir,tmp 这五个文件夹备用。
现在我们在datadir中创建3个文件夹以放置三个实例的数据文件:3307,3308,3309

[root@localhost multiMysql]# mkdir /home/multiMysql/datadir/{3307,3308,3309}

然后用mysql_install_db来生成即将使用的多个实例的数据文件,首先需要对/home/multiMysql进行递归授权防止之后的操作出现权限不够的情况:(但是不建议直接修改整个文件夹的权限为777,权限太大,风险太高),可以在配置完成之后把文件的的权限修改小一点

chmod -R 777 /home/multiMysql
mysql_install_db --basedir=/usr --datadir=/home/multiMysql/datadir/3307 --user=mysql
mysql_install_db --basedir=/usr --datadir=/home/multiMysql/datadir/3308 --user=mysql

--user是指mysql实例将使用的在linux系统中的用户,最好命名为mysql,yum安装后一般都有这个用户,如果没有可以自主创建:

groupadd mysql
adduser -g mysql mysql
[root@localhost multiMysql]# ls /home/multiMysql/datadir/3308/
aria_log.00000001  aria_log_control  mysql  performance_schema  test

如果里面有文件代表生成成功。

接下来我们来做多实例的配置:

先创建一个公用配置文件:

mkdir /home/multiMysql/etc/my.cnf.d/
vim /home/multiMysql/etc/my.cnf.d/my.cnf
[mysqld]
skip-name-resolve
lower_case_table_names=1
innodb_file_per_table=1
back_log = 50
max_connections = 300
max_connect_errors = 1000
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 2M
max_heap_table_size = 64M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 64
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 1
server-id = 1
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 200M
innodb_data_file_path = ibdata1:10M:autoextend
innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 60
innodb_lock_wait_timeout = 120
[mysqldump]
quick
max_allowed_packet = 256M
[mysql]
no-auto-rehash
prompt=\\u@\\d \\R:\\m>
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192

然后分别创建三个实例的配置文件:

cd /home/multilMysql/ect
[root@localhost etc]# vim 3307.cnf 

 

[client]
port = 3307
socket = /home/multiMysql/socket/mysql3307.sock
[mysqld]
datadir=/home/multiMysql/datadir/3307
port = 3307
socket = /home/multiMysql/socket/mysql3307.sock

tmpdir=/home/multiMysql/tmp/3307

myisam-recover-options = BACKUP 

!includedir /home/multiMysql/etc/my.cnf.d

注意一定要写:(否则会报错)

tmpdir=/home/multiMysql/tmp/3307

myisam-recover-options = BACKUP 

然后把3307.cnf复制份:

[root@localhost etc]# cp 3307.cnf 3308.cnf

然后分别编辑复制的两份配置文件,把端口和socket进行修改:

[root@localhost etc]# vim 3308.cnf 
[client]
port = 3308
socket = /home/multiMysql/socket/mysql3308.sock
[mysqld]
datadir=/home/multiMysql/datadir/3308
port = 3308
socket = /home/multiMysql/socket/mysql3308.sock

tmpdir=/home/multiMysql/tmp/3308

myisam-recover-options = BACKUP 

!includedir /home/multiMysql/etc/my.cnf.d

在配置文件中,port是实例的端口,socket是实例运行时的sock文件,datadir是之前我们生成的数据库文件位置。
 

然后我们来编辑三个启动脚本:

[root@localhost bin]# vim /home/multiMysql/bin/mysql3307
#!/bin/bash
mysql_port=3307
mysql_username="root"
mysql_password=""
function_start_mysql()
{
printf "Starting MySQL...\n"
mysqld_safe --defaults-file=/home/multiMysql/etc/${mysql_port}.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
mysqladmin -u ${mysql_username} -p${mysql_password} -S /home/multiMysql/socket/mysql${mysql_port}.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";;
esac
[root@localhost bin]# vim /home/multiMysql/bin/mysql3308
#!/bin/bash
mysql_port=3308
mysql_username="root"
mysql_password=""
function_start_mysql()
{
printf "Starting MySQL...\n"
mysqld_safe --defaults-file=/home/multiMysql/etc/${mysql_port}.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
mysqladmin -u ${mysql_username} -p${mysql_password} -S /home/multiMysql/socket/mysql${mysql_port}.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo "Usage: /data/dbdata_${mysql_port}/mysqld {start|stop|restart|kill}";;
esac

因为是yum安装,所以mysqld_safe和mysqladmin可以不用加路径直接运行,另外mysql_port是指这个bash简要打开的实例的端口,mysql_username和mysql_userpassword为我们即将在实例中配置的可关闭mysql进程的mysql用户名和密码。
现在给两个bash文件权限来执行,并尝试打开两个实例:

[root@localhost bin]# chmod 777 /home/multiMysql/bin/mysql3307
[root@localhost bin]# chmod 777 /home/multiMysql/bin/mysql3308

先关闭yum安装的默认mysql实例进程:

service mysqld stop
[root@localhost bin]# /home/multiMysql/bin/mysql3307 start
[root@localhost bin]# /home/multiMysql/bin/mysql3308 start

查看进程:

[root@server187 multiMysql]#  ps -ef | grep mysqld
root      8033     1  0 14:46 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3307.cnf
mysql     8113  8033  0 14:46 pts/1    00:00:02 /usr/sbin/mysqld --defaults-file=/home/multiMysql/etc/3307.cnf --basedir=/usr --datadir=/home/multiMysql/datadir/3307 --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/home/multiMysql/datadir/3307/server187.err --pid-file=server187.pid --socket=/home/multiMysql/socket/mysql3307.sock --port=3307
root      9774     1  0 14:59 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3308.cnf
mysql     9833  9774  0 14:59 pts/1    00:00:01 /usr/sbin/mysqld --defaults-file=/home/multiMysql/etc/3308.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/server187.err --pid-file=server187.pid
root     10252     1  0 15:02 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/home/multiMysql/etc/3308.cnf
mysql    10332 10252  0 15:02 pts/1    00:00:01 /usr/sbin/mysqld --defaults-file=/home/multiMysql/etc/3308.cnf --basedir=/usr --datadir=/home/multiMysql/datadir/3308 --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/home/multiMysql/datadir/3308/server187.err --pid-file=server187.pid --socket=/home/multiMysql/socket/mysql3308.sock --port=3308
root     11638 13061  0 15:24 pts/1    00:00:00 grep --color=auto mysqld
[root@server187 multiMysql]# 

可以看到多个实例已经启动,我们来尝试连接三个实例的sock:

启动实例之后,进行连接:

[root@server187 multiMysql]# mysql -u root  -S /home/multiMysql/socket/mysql3308.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.8-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by '2018';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值