在公司资源紧张的情况下,需要在一台服务器上部署多个数据库实例,现在就来实战一下该情况。
需要启动两个不同的端口,分别是3306和3307
[root@node1 ~]# mkdir /u01/mysql/{3306,3307} -p
[root@node1 ~]# tree /u01/
/u01/
└── mysql
├── 3306
└── 3307
[root@node1 3306]# pwd
/u01/mysql/3306
[root@node1 3306]# vim my.cnf
[root@node1 3306]# grep -v '^#' my.cnf
[mysqld]
datadir=/u01/mysql/3306/data
basedir=/u01/mysql/3306
socket=/u01/mysql/3306/mysql.sock
port=3306
user=root
log_error=/u01/mysql/3306/mysqld.log
symbolic-links=0
初始化数据库:
[root@node1 3306]# mysqld --defaults-file=/u01/mysql/3306/my.cnf --initialize
启动数据库:
[root@node1 3306]# mysqld_safe --defaults-file=/u01/mysql/3306/my.cnf &
数据库的初始密码存在mysqld.log中
[root@node1 3306]# more mysqld.log |grep password
2019-10-21T02:45:19.489156Z 1 [Note] A temporary password is generated for root@localhost: Olp6<bu#op3J
[root@node1 3306]# mysql -uroot -p -S mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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> alter user 'root'@'localhost' identified by 'password'
[root@node1 3306]# netstat -tlunp|grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 4401/mysqld
[root@node1 3306]# ps -ef|grep mysqld
root 4245 1 0 10:46 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/u01/mysql/3306/my.cnf
root 4401 4245 0 10:46 ? 00:00:01 /usr/local/src/mysql-5.7.27-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/u01/mysql/3306/my.cnf --basedir=/u01/mysql/3306 --datadir=/u01/mysql/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=root --log-error=/u01/mysql/3306/mysqld.log --pid-file=node1.pid --socket=/u01/mysql/3306/mysql.sock --port=3306
这样3306端口的数据库就可以正常使用了。
3307端口的也是一样的,修改一下配置文件,
[root@node1 3307]# pwd
/u01/mysql/3307
[root@node1 3307]# vim my.cnf
root@node1 3307]# grep -v '^#' my.cnf
[mysqld]
datadir=/u01/mysql/3307/data
basedir=/u01/mysql/3307
socket=/u01/mysql/3307/mysql.sock
port=3307
user=root
log_error=/u01/mysql/3307/mysqld.log
symbolic-links=0
!includedir /etc/my.cnf.d
初始化:
[root@node1 3307]# mysqld --defaults-file=/u01/mysql/3307/my.cnf --initialize
启动数据库:
[root@node1 3307]# mysqld_safe --defaults-file=/u01/mysql/3307/my.cnf &
然后查看初始化密码,登陆进数据库,重置密码。
[root@node1 3307]# netstat -tlunp|grep mysqld
tcp6 0 0 :::3307 :::* LISTEN 5426/mysqld
tcp6 0 0 :::3306 :::* LISTEN 4401/mysqld
[root@node1 3307]# ps -ef|grep mysqld
root 4245 1 0 10:46 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/u01/mysql/3306/my.cnf
root 4401 4245 0 10:46 ? 00:00:02 /usr/local/src/mysql-5.7.27-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/u01/mysql/3306/my.cnf --basedir=/u01/mysql/3306 --datadir=/u01/mysql/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=root --log-error=/u01/mysql/3306/mysqld.log --pid-file=node1.pid --socket=/u01/mysql/3306/mysql.sock --port=3306
root 5268 4147 0 10:58 pts/3 00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/u01/mysql/3307/my.cnf
root 5426 5268 0 10:58 pts/3 00:00:01 /usr/local/src/mysql-5.7.27-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/u01/mysql/3307/my.cnf --basedir=/u01/mysql/3307 --datadir=/u01/mysql/3307/data --plugin-dir=/usr/local/mysql/lib/plugin --user=root --log-error=/u01/mysql/3307/mysqld.log --pid-file=node1.pid --socket=/u01/mysql/3307/mysql.sock --port=3307
root 6155 4147 0 11:41 pts/3 00:00:00 grep --color=auto mysqld
停止数据库:
[root@node1 3307]# mysqladmin -uroot -pWpw303@123 -S /u01/mysql/3307/mysql.sock shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
2019-10-21T03:42:10.468422Z mysqld_safe mysqld from pid file /u01/mysql/3307/data/node1.pid ended
[1]+ Done mysqld_safe --defaults-file=/u01/mysql/3307/my.cnf (wd: /u01/mysql/3306)
(wd now: /u01/mysql/3307)
[root@node1 3307]# netstat -tlunp|grep mysqld 只有一个实例了。
tcp6 0 0 :::3306 :::* LISTEN 4401/mysqld
[root@node1 3307]# ps -ef|grep mysqld
root 4245 1 0 10:46 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/u01/mysql/3306/my.cnf
root 4401 4245 0 10:46 ? 00:00:02 /usr/local/src/mysql-5.7.27-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/u01/mysql/3306/my.cnf --basedir=/u01/mysql/3306 --datadir=/u01/mysql/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=root --log-error=/u01/mysql/3306/mysqld.log --pid-file=node1.pid --socket=/u01/mysql/3306/mysql.sock --port=3306
root 6209 4147 0 11:43 pts/3 00:00:00 grep --color=auto mysqld