配置 mysql 多实例

配置 mysql 多实例

本次试验说明:
	1)试验环境:
		centos 7
		5.5.68-MariaDB MariaDB Server
	2)试验目的:
		使用 yum 安装 mariadb 之后,另外增加两个 mysql 实例,监听不同的端口(例如:默认监听 3306,新增监听 3307 3308)
	3)查看监听结果

1. 安装 mysql

# 这里是测试使用,安装 mariadb
yum install mariadb-server.x86_64 -y

# 启动数据库并设置开机自启
[root@zabbix ~]# systemctl start mariadb.service
[root@zabbix ~]# systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
# 使用 mysql_secure_installation 命令配置数据库
mysql_secure_installation

[root@zabbix ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

# 输入当前数据库用户 root 密码,开始时没有密码,直接回车
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
# 是否设置 root 密码
Set root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
# 是够删除 anonymous 用户
Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
# 是否禁止 root 远程登陆
Disallow root login remotely? [Y/n] y  
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
# reload
Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

# 登陆查看
[root@zabbix ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.68-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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)

MariaDB [(none)]> 

2. 配置 mysql 多实例

2.1 添加配置文件

# 复制默认的配置文件 /etc/my.conf 并修改文件内容

# 增加监听 3307 端口实例
cp /etc/my.cnf /etc/my3307.cnf
vim /etc/my3307.cnf	
# 添加如下内容
[mysqld]

datadir=/data/3307/

socket=/data/3307/mysql.sock

port=3307

user=mysql

symbolic-links=0

[mysqld_safe]

log-error=/data/3307/mysqld.log

pid-file=/data/3307/mysqld.pid


# 增加监听 3308 端口实例
cp /etc/my3307.cnf /etc/my3308.cnf
sed -i 's#3307#3308#g' /etc/my3308.cnf

2.2 初始化数据库 3307

mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf

mysqld_safe --defaults-file=/etc/my3307.cnf &

2.3 初始化数据库 3308

mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf

mysqld_safe --defaults-file=/etc/my3308.cnf &

2.4 检查多实例是否正常

[root@zabbix-web ~]# netstat -lnatp | grep mysqld
tcp        0      0 0.0.0.0:3307            0.0.0.0:*               LISTEN      2159/mysqld         
tcp        0      0 0.0.0.0:3308            0.0.0.0:*               LISTEN      2452/mysqld         
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1787/mysqld

2.5 增加 3306 端口 sock 的软连接

# 放在同一个目录下,便于对比
mkdir /data/3306
ln -s /var/lib/mysql/mysql.sock /data/3306/

3. 登陆不同实例数据库

# 查看不同监听的 sock 文件
[root@zabbix-web ~]# find /data -name *.sock
/data/3307/mysql.sock
/data/3308/mysql.sock
/data/3306/mysql.sock

# 登陆监听端口为 3306 的数据库
mysql -uroot
# 默认监听 3306 所以不加其他参数就可以默认登陆
# 登陆监听端口为 3307 的数据库
mysql -S /data/3307/mysql.sock -uroot
# 登陆监听端口为 3308 的数据库
mysql -S /data/3308/mysql.sock -uroot

 
 
 
 
 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值