Implement multiple instances based on yum source
This experiment implements three examples, using port 330633073308 respectively
1. Install MariaDB server, yum
#yum install mariadb-server
2. Create a directory for each instance
#mkdir /data/mysql/{3306,3307,3308}/{etc,data,socket,log,bin,pid} -pv
3. Permission to modify directory and subdirectory
#chown -R mysql.mysql /data/mysql
4. Use script to generate default database and data file
#/usr/bin/mysql_install_db --datadir=/data/mysql/3306/data --user=mysql #/usr/bin/mysql_install_db --datadir=/data/mysql/3307/data --user=mysql #/usr/bin/mysql_install_db --datadir=/data/mysql/3308/data --user=mysql
5. Prepare configuration file
Take 3306 for example, 3307 and 3308 can be modified according to the configuration file of 3306.
#vim /data/mysql/3306/etc/my.cnf
[mysqld] port=3306 datadir=/data/mysql/3306/data socket=/data/mysql/3306/socket/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/data/mysql/3306/log/mariadb.log pid-file=/data/mysql/3306/pid/mariadb.pid # # include all files from the config directory # #!includedir /etc/my.cnf.d
6. Prepare startup script
I prepared a simple script file mysqld in advance
#cp mysqld /data/mysql/3306/bin/
Slightly modified for each instance (take 3306 as an example)
Contents modified in the document:
#!/bin/bash port=3306 mysql_user="root" mysql_pwd="" cmd_path="/usr/bin" mysql_basedir="/data/mysql" mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"
After modification, add execution permission to the program script:
#chmod +x /data/mysql/3306/bin/mysqld #chmod +x /data/mysql/3307/bin/mysqld #chmod +x /data/mysql/3308/bin/mysqld
After that, you can run the startup script to start the instance (take 3306 as an example)
#/data/mysql/3306/bin/mysqld start
After startup, you can check the port opening to verify the success.
If the startup is successful, you can open three different instances through socket. (take 3306 as an example)
#mysql -S /data/mysql/3306/socket/mysql.sock
7. Modify database password
#mysqladmin -uroot -S /data/mysql/3306/socket/mysql.sock password "centos"
Then modify mysqld script again
Appendix: contents of mysqld script file
#!/bin/bash port=3306 mysql_user="root" mysql_pwd="centos" cmd_path="/usr/bin" mysql_basedir="/data/mysql" mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock" function_start_mysql() { if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null & else printf "MySQL is running...\n" exit fi } function_stop_mysql() { if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped...\n" exit else printf "Stoping MySQL...\n" ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown fi } function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql sleep 2 function_start_mysql } case $1 in start) function_start_mysql ;; stop) function_stop_mysql ;; restart) function_restart_mysql ;; *) printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n" esac