关系型数据库MySQL多实例

简介


MySQL数据库是一个中小型关系型数据库管理系统,软件开发者为瑞典MySQL AB公司。在2008年1月16号被Sun公司收购后Sun公司又被oracle公司收购。目前MySQL被广泛地应用在Internet上的大中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多大中小型网站为了降低网站成本而选择了MySQL作为网站数据库,甚至国内知名的淘宝网也选择弃用ORACLE而更换为更开放的MySQL。其实就是一个存数据的地方~


 

环境准备


 系统环境:CentOS7

基础优化:略

关闭selinux关闭防火墙:略

服务器IP:192.168.110.20


 

 安装


 yum install mariadb mariadb-server #mariadb是mysql的一个分支

useradd mysql -M -s /sbin/nologin  #如有mysql用户可以忽略


 

配置


 mkdir -p /data/{3306,3307,3308}/data #多实例存放目录

vim /data/3306/my.cnf    #优化好的配置文件
[client]
port = 3306
socket = /data/3306/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user = mysql
port = 3306
server-id =1
socket = /data/3306/mysql.sock
basedir = /usr
datadir = /data/3306/data
pid-file = /data/3306/mysql.pid
relay-log = /data/3306/relay-bin
relay-log-info-file = /data/3306/relay-log.info
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
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
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_wsy3306.err
pid-file=/data/3306/mysqld.pid

 


 

 

写启动脚本


 

vim /data/3306/mysql
#!/bin/bash
mysql_port=3306
mysql_username="root"
mysql_password=""
function_start_mysql()
{
printf "Starting MySQL...\n"
mysqld_safe --defaults-file=/data/$mysql_port/my.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
mysqladmin -u ${mysql_username} -p${mysql_password} -S /data/3306/mysql.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/$mysql_port/mysql {start|stop|restart|kill}";;
esac

 


 

 

启动


 

 

chown -R mysql.mysql /data   #权限

mysql_install_db --basedir=/usr/ --datadir=/data/3306/data --user=mysql #安装数据库 两个ok为成功

/data/3306/mysql start #启动
echo "/data/3306/mysql start" >>/etc/rc.local #加入开机自启动
mysql -S /data/3306/mysql.sock #连接数据库


 

其他两个实例配置

只需要稍稍变动一下配置文件


 

cp /data/3306/my.cnf /data/3307/my.cnf /data/3308/my.cnf
cp /data/3306/mysql /data/3307/mysql /data/3308/mysql
sed -i s#3306#3307#g /data/3307/my.cnf
sed -i s#3306#3308#g /data/3308/my.cnf
sed -i s#3306#3307#g /data/3307/mysql
sed -i s#3306#3308#g /data/3308/mysql
sed -i s#server-id=1#server-id=2#g /data/3307/my.cnf
sed -i s#server-id=1#server-id=2#g /data/3308/my.cnf

mysql_install_db --basedir=/usr/ --datadir=/data/3307/data --user=mysql
mysql_install_db --basedir=/usr/ --datadir=/data/3308/data --user=mysql

/data/3307/mysql start
/data/3308/mysql start

设置密码:

use mysql
update user set password=password('123456') where user='root'; #自定义密码123456
flush privileges;


密码丢失

方法1:
mysqld_safe -skip-grant-tables &  #跳过授权表
mysql -u root mysql
UPDATE user SET password=PASSWORD(“new password”) WHERE user=’root’;
FLUSH PRIVILEGES;

方法2:
1、修改mysql的配置文件(默认为/etc/my.cnf),在[mysqld]下添加一行skip-grant-tables

2、保存配置文件后,重启mysql服务 service mysqld restart

3、mysql -u root -p登录mysql,然后不输入密码直接回车,然后按照上面的流程修改密码

4、密码修改完毕,按照流程1的删除配置文件中的那行,然后重启mysql服务

查看三个端口是不是启动了,多实例就配置好了


 

posted @ 2018-02-11 17:32  活蹦乱跳的鱼  阅读(483)  评论(0编辑  收藏  举报