Linux平台下源码安装mysql多实例数据库
Posted zhouwanchun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux平台下源码安装mysql多实例数据库相关的知识,希望对你有一定的参考价值。
Linux平台下源码安装mysql多实例数据库
[[email protected] ~]# netstat -tlunp | grep 330
tcp6 0 0 :::3306 :::* LISTEN 6191/mysqld
[[email protected] ~]# ss -tlunp | grep 330
tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=6191,fd=10))
[[email protected] ~]# systemctl status mysqld.service
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since Fri 2019-03-29 12:01:11 CST; 2min 40s ago
Docs: man:systemd-sysv-generator(8)
Process: 6056 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─6065 /bin/sh /application/mysql-5.6.41/bin/mysqld_safe --datadir=/application/mysql-5.6.41/data --pid-file=/application...
└─6191 /application/mysql-5.6.41/bin/mysqld --basedir=/application/mysql-5.6.41 --datadir=/application/mysql-5.6.41/data ...
Mar 29 12:01:07 linux-node1.eric.com systemd[1]: Starting LSB: start and stop MySQL...
Mar 29 12:01:11 linux-node1.eric.com mysqld[6056]: Starting MySQL.... SUCCESS!
Mar 29 12:01:11 linux-node1.eric.com systemd[1]: Started LSB: start and stop MySQL.
1、先停掉之前的3306单实例数据库
[[email protected] ~]# systemctl stop mysqld.service
[[email protected] ~]# netstat -tlunp | grep 330
[[email protected] ~]# netstat -tlunp | grep mysql
[[email protected] ~]# chkconfig mysqld off
[[email protected] ~]# chkconfig --list mysqld
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
2、mysql多实例常见的配置方案
(1).单一配置文件、单一启动程序多实例部署方案(不推荐)
该方案缺点:耦合度太高,只有一个配置文件,不好管理。
工作中开发和运维的统一原则是:降低耦合度。
my.cnf配置文件示例
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = mysql
[mysqld1]
socket = /var/lib/mysql/mysql.sock
port = 3306
pid-file = /var/lib/mysql/mysql.pid
datadir = /var/lib/mysql/
user = mysql
[mysqld2]
socket = /mnt/data/db1/mysql.sock
port = 3302
pid-file = /mnt/data/db1/mysql.pid
datadir = /mnt/data/db1/
user = mysql
skip-name-resolv
server-id=10
default-storage-engine=innodb
innodb_buffer_pool_size=512M
innodb_additional_mem_pool=10M
default_character_set=utf8
character_set_server=utf8
#read-only
relay-log-space-limit=3G
expire_logs_day=20
启动程序的命令如下:
mysqld_multi --config-file=/data/mysql/my_multi.cnf start 1,2
(2).多配置文件、多启动程序部署方案
mysql双实例的目录信息:
[[email protected] ~]# tree /data
/data
├── 3306
│ ├── data #3306实例的数据目录
│ ├── my.cnf #3306实例的配置文件
│ └── mysql #3306实例的启动文件
└── 3307
├── data #3307实例的数据目录
├── my.cnf #3307实例的配置文件
└── mysql #3307实例的启动文件
4 directories, 4 files
3、安装并配置多实例mysql数据库
基于上节课的单实例mysql数据库环境来做实验
生产环境一般以2~4个实例为佳
4、创建mysql多实例的数据文件目录
[[email protected] ~]# mkdir -p /data/{3306,3307}/data
[[email protected] ~]# tree /data/
/data/
├── 3306 #3306实例目录
│ └── data #3306实例的数据文件目录
├── 3307 #3307实例目录
└── data #3307实例的数据文件目录
4 directories, 0 files
5、创建mysql多实例的配置文件和启动文件
在这里我直接rz上传了
[[email protected] ~]# cd /disk/
[[email protected] disk]# rz
rz waiting to receive.
zmodem trl+C ?
100% 3 KB 3 KB/s 00:00:01 0 Errors
[[email protected] disk]# ls -lh
total 37M
drwxr-xr-x 13 root root 4.0K Mar 28 21:25 cmake-2.8.12.2
-rw-r--r-- 1 root root 5.8M Mar 26 08:58 cmake-2.8.12.2.tar.gz
-rw-r--r-- 1 root root 3.6K Mar 28 17:48 data.zip
drwxr-xr-x 35 7161 31415 4.0K Mar 28 21:54 mysql-5.6.41
-rw-r--r-- 1 root root 31M Jun 15 2018 mysql-5.6.41.tar.gz
解压
[[email protected] disk]# unzip data.zip -d /
Archive: data.zip
inflating: /data/3306/my.cnf
inflating: /data/3306/mysql
inflating: /data/3307/my.cnf
inflating: /data/3307/mysql
[[email protected] disk]# tree /data/
/data/
├── 3306
│ ├── data
│ ├── my.cnf
│ └── mysql
└── 3307
├── data
├── my.cnf
└── mysql
4 directories, 4 files
6、启动实例方法:
例如:启动3306实例的命令如下:
mysqld_safe --defaults-file=/data/3306/my.cnf > /dev/null 2>&1 &
例如:启动3307实例的命令如下:
mysqld_safe --defaults-file=/data/3307/my.cnf > /dev/null 2>&1 &
7、停止实例方法:
停止3306实例的命令如下:
mysqladmin -u root -poldboy123 -S /data/3306/mysql.sock shutdown
停止3307实例的命令如下:
mysqladmin -u root -poldboy123 -S /data/3307/mysql.sock shutdown
8、要先创建mysql多实例数据库的错误日志文件,不然后面启动mysql实例会报错
[[email protected] ~]# touch /data/3306/mysql_zhouwanchun3306.err
[[email protected] ~]# touch /data/3307/mysql_zhouwanchun3307.err
例如:
[[email protected] ~]# /data/3306/mysql start
Starting MySQL...
190329 16:27:49 mysqld_safe error: log-error set to ‘/data/3306/mysql_zhouwanchun3306.err‘, however file don‘t exists. Create writable for user ‘mysql‘.
9、配置mysql多实例的文件权限
[[email protected] ~]# chown -R mysql:mysql /data
[[email protected] ~]# find /data -name mysql|xargs ls -l
-rw-r--r-- 1 mysql mysql 1307 Jul 15 2013 /data/3306/mysql
-rw-r--r-- 1 mysql mysql 1307 Jul 21 2013 /data/3307/mysql
[[email protected] ~]# find /data -name mysql|xargs chmod 700
[[email protected] ~]# find /data -name mysql|xargs ls -l
-rwx------ 1 mysql mysql 1307 Jul 15 2013 /data/3306/mysql
-rwx------ 1 mysql mysql 1307 Jul 21 2013 /data/3307/mysql
[[email protected] ~]# find /data -name mysql -exec ls -l {} ;
-rwx------ 1 mysql mysql 1307 Jul 15 2013 /data/3306/mysql
-rwx------ 1 mysql mysql 1307 Jul 21 2013 /data/3307/mysql
10、初始化mysql多实例的数据库文件
[[email protected] ~]# cd /application/mysql/scripts/
./mysql_install_db
--defaults-file=/data/3306/my.cnf
--basedir=/application/mysql
--datadir=/data/3306/data
--user=mysql
./mysql_install_db
--defaults-file=/data/3307/my.cnf
--basedir=/application/mysql
--datadir=/data/3307/data
--user=mysql
两个初始化,每个初始化有两个OK字样,一共4个Ok字样
[[email protected] scripts]# tree /data
11、启动数据库实例
启动多实例3306的命令
[[email protected] ~]# /data/3306/mysql start
启动多实例3307的命令
[[email protected] ~]# /data/3307/mysql start
[[email protected] ~]# netstat -tlunp | grep 330
tcp6 0 0 :::3306 :::* LISTEN 7134/mysqld
tcp6 0 0 :::3307 :::* LISTEN 7376/mysqld
如果mysql多实例数据库有服务端口没有被启动,稍微等几秒再检查。
netstat -lntup | grep 330
因为mysql服务的启动比Web服务慢一些
如果还是不行,请查看mysql服务对应实例的错误日志
[[email protected] ~]# grep log-error /data/3306/my.cnf | tail -1
log-error=/data/3306/mysql_zhouwanchun3306.err
[[email protected] ~]# tail /data/3306/mysql_zhouwanchun3306.err
12、配置mysql多实例数据库开机自启动
[[email protected] ~]# echo "# mysql multi instances" >> /etc/rc.local
[[email protected] ~]# echo "/data/3306/mysql start" >> /etc/rc.local
[[email protected] ~]# echo "/data/3307/mysql start" >> /etc/rc.local
[[email protected] ~]# tail -3 /etc/rc.local
# mysql multi instances
/data/3306/mysql start
/data/3307/mysql start
13、登录数据库
密码默认为空
[[email protected] ~]# mysql -S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.41-log Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.
mysql> select user() ;
+----------------+
| user() |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)
mysql> exit ;
Bye
[[email protected] ~]# mysql -S /data/3307/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.41-log Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.
mysql> select user() ;
+----------------+
| user() |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)
mysql> exit ;
Bye
mysql.sock文件mysql服务端与本地mysql客户端进行通信的Unix套接字文件。
14、mysql多实例数据库的管理
[[email protected] ~]# /data/3306/mysql stop
Stoping MySQL...
[[email protected] ~]# /data/3306/mysql start
Starting MySQL...
[[email protected] ~]# /data/3306/mysql restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[[email protected] ~]# mysqladmin -u root -p -S /data/3306/mysql.sock shutdown
[[email protected] ~]# mysqladmin -u root -p -S /data/3307/mysql.sock shutdown
15、mysql安全配置
(1).分别给mysql不同实例的数据库管理员root用户设置独立密码
[[email protected] ~]# mysqladmin -uroot -S /data/3306/mysql.sock password ‘oldboy3306‘
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# mysqladmin -uroot -S /data/3307/mysql.sock password ‘oldboy3306‘
Warning: Using a password on the command line interface can be insecure.
再次登录数据库
无法登录
[[email protected] ~]# mysql -S /data/3306/mysql.sock
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[[email protected] ~]# mysql -S /data/3307/mysql.sock
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[[email protected] ~]# mysql -uroot -p -S /data/3306/mysql.sock
Enter password: 输入密码
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.6.41-log Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.
mysql> exit ;
Bye
[[email protected] ~]# mysql -uroot -p -S /data/3307/mysql.sock
Enter password: 输入密码
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.6.41-log Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.
mysql> exit ;
Bye
也可以带着密码登录
mysql -uroot -poldboy3306 -S /data/3306/mysql.sock
mysql -uroot -poldboy3306 -S /data/3307/mysql.sock
(2).修改密码
[[email protected] ~]# mysqladmin -uroot -S /data/3307/mysql.sock -poldboy3306 password ‘oldboy3307‘
[[email protected] ~]# mysql -uroot -poldboy3306 -S /data/3307/mysql.sock 登录失败
[[email protected] ~]# mysql -uroot -poldboy3307 -S /data/3307/mysql.sock 登录成功
16、远程连接登录mysql多实例
mysql -uroot -p‘oldboy123‘ -h 192.168.56.11 -P 3306
mysql -ueric -p‘oldboy123‘ -h 192.168.56.11 -P 3306
17、如何再增加一个mysql的实例
mkdir -p /data/3308/data
cp /data/3306/my.cnf /data/3308/
cp /data/3306/mysql /data/3308/
sed -i ‘s#3306#3308#g‘ /data/3308/my.cnf
sed -i ‘s#server-id = 6#server-id = 8#g‘ /data/3308/my.cnf
sed -i ‘s#3306#3308#g‘ /data/3308/mysql
touch /data/3308/mysql_zhouwanchun3308.err
chown -R mysql:mysql /data/3308
chmod 700 /data/3308/mysql
cd /application/mysql/scripts/
./mysql_install_db --defaults-file=/data/3308/my.cnf --basedir=/application/mysql --datadir=/data/3308/data --user=mysql
chown -R mysql:mysql /data/3308
egrep "server-id|log-bin" /data/3308/my.cnf
/data/3308/mysql start
netstat -tlunp | grep 3308
echo "/data/3308/mysql start" >> /etc/rc.local
tail -4 /etc/rc.local
mysql -S /data/3308/mysql.sock
mysqladmin -uroot -S /data/3308/mysql.sock password ‘oldboy3308‘
mysql -uroot -poldboy3308 -S /data/3308/mysql.sock
18、给mysql做基本安全配置
(1).为root用户设置密码(密码已经设置)
(2).清除mysql服务器内无用的用户
[[email protected] ~]# mysql -uroot -poldboy3306 -S /data/3306/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.6.41-log Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.
mysql> select user, host from mysql.user ;
+------+----------------------+
| user | host |
+------+----------------------+
| root | 127.0.0.1 |
| root | ::1 |
| | linux-node1.eric.com |
| root | linux-node1.eric.com |
| | localhost |
| root | localhost |
+------+----------------------+
6 rows in set (0.00 sec)
mysql> drop user [email protected]‘::1‘ ;
Query OK, 0 rows affected (0.01 sec)
mysql> drop user [email protected]‘linux-node1.eric.com‘ ;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ‘‘@‘linux-node1.eric.com‘ ;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ‘‘@‘localhost‘ ;
Query OK, 0 rows affected (0.00 sec)
mysql> select user, host from mysql.user ;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)
(3).删除mysql数据库内无用的test库
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> drop database test ;
Query OK, 0 rows affected (0.01 sec)
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
19、重启Linux操作系统
[[email protected] ~]# reboot
以上是关于Linux平台下源码安装mysql多实例数据库的主要内容,如果未能解决你的问题,请参考以下文章