本文共 5871 字,大约阅读时间需要 19 分钟。
编译安装MySQL
编译环境 | MySQL版本 |
---|---|
CentOS 7.6 | mariadb-10.2.23 |
yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boostdevel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssldevel libevent-devel libaio-devel -y
[root@localhost ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql
[root@localhost ~]# chmod 700 /data/mysql[root@localhost ~]# chown mysql.mysql /data/mysql
[root@localhost ~]# tar -xf mariadb-10.2.23.tar.gz
此处需要注意如果编译时中间如果出现缺包的错误,需要将此目录下的CMakeCache.txt删除后重新进行编译。
[root@localhost ~]# cd mariadb-10.2.23[root@localhost mariadb-10.2.23]# cmake . \-DCMAKE_INSTALL_PREFIX=/app/mysql \-DMYSQL_DATADIR=/data/mysql/ \-DSYSCONFDIR=/etc/mysql \-DMYSQL_USER=mysql \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_ARCHIVE_STORAGE_ENGINE=1 \-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \-DWITH_PARTITION_STORAGE_ENGINE=1 \-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \-DWITH_DEBUG=0 \-DWITH_READLINE=1 \-DWITH_SSL=system \-DWITH_ZLIB=system \-DWITH_LIBWRAP=0 \-DENABLED_LOCAL_INFILE=1 \-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci[root@localhost mariadb-10.2.23]# make && make install
[root@localhost mariadb-10.2.23]# cd /app/mysql/[root@localhost mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysqlInstalling MariaDB/MySQL system tables in '/data/mysql' ...OK
[root@localhost mysql]# mkdir /etc/mysql #c创建MySQL配置文件目录[root@localhost mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf[root@localhost mysql]# sed -i '/\[mysqld\]/adatadir=/data/mysql' /etc/mysql/my.cnf
[root@localhost mysql]# cp support-files/mysql.service /etc/init.d/mysqld[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# service mysqld startStarting mysqld (via systemctl): [ OK ]
[root@localhost mysql]# echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh[root@localhost mysql]# . /etc/profile.d/mysql.sh
MySQL的多实例环境搭建
在测试环境中通常会需要在一台主机上搭建多个版本的MySQL,此处演示如何搭建多实例的MySQL。
[root@localhost /]# mkdir -pv /mysql/{3306,3307}/{data,etc,socket,bin,log,pid}[root@localhost ~]# chown -R mysql.mysql /mysql[root@localhost ~]# tree /mysql #查看下目录结构/mysql ├── 3306│ ├── bin│ ├── data│ ├── etc│ ├── log│ ├── pid│ └── socket└── 3307 ├── bin ├── data ├── etc ├── log ├── pid └── socket
[root@localhost /]# /app/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/3306/data[root@localhost /]# /app/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/3307/data
配置文件可以参考/etc/my.cnf进行修改
[root@localhost /]# cp /etc/my.cnf /mysql/3306/etc/my.cnf [root@localhost /]# vim /mysql/3306/etc/my.cnf #修改my.cnf,写入配置[mysqld] #注意需要将#!includedir /etc/my.cnf.d注释port=3306datadir=/mysql/3306/datasocket=/mysql/3306/socket/mysql.socksymbolic-links=0[mysqld_safe]log-error=/mysql/3306/log/mariadb.logpid-file=/mysql/3306/pid/mariadb.pid[root@localhost /]# cp /mysql/3306/etc/my.cnf /mysql/3307/etc/ [root@localhost /]# sed -i 's/3306/3307/' /mysql/3307/etc/my.cnf
#!/bin/bashport=3306mysql_user="root"mysql_pwd=""cmd_path="/usr/bin"mysql_basedir="/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 instart) function_start_mysql;;stop) function_stop_mysql;;restart) function_restart_mysql;;*) printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"esac
[root@localhost /]# vim /mysql/3306/bin/mysqld #对端口和cmd_path进行修改port=3306cmd_path="/app/mysql/bin"[root@localhost /]# vim /mysql/3307/bin/mysqld #对端口和cmd_path进行修改port=3307cmd_path="/app/mysql/bin"[root@localhost /]# chmod +x /mysql/3306/bin/mysqld #为服务脚本添加执行权限[root@localhost /]# chmod +x /mysql/3307/bin/mysqld
[root@localhost /]# ss -tnl |grep 3306LISTEN 0 80 :::3306 :::* [root@localhost /]# service mysqld stopStopping mysqld (via systemctl): [ OK ]
[root@localhost /]# /mysql/3306/bin/mysqld startStarting MySQL...[root@localhost /]# /mysql/3307/bin/mysqld startStarting MySQL...[root@localhost /]# ss -tnl | grep 3306LISTEN 0 80 :::3306 :::* [root@localhost /]# ss -tnl | grep 3307LISTEN 0 80 :::3307 :::*
客户端在连接多实例的MySQL时,需要指定端口号和socket文件路径否则报错。
[root@localhost /]# mysql -uroot -p -P3306 -S /mysql/3306/socket/mysql.sock Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 8Server version: 10.2.23-MariaDB Source distributionCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
多实例的MySQL服务在关闭时需要输入MySQL管理员的密码,也可以将管理员密码存放在配置文件的变量mysql_pwd=""中
[root@localhost /]# /mysql/3306/bin/mysqld stopStoping MySQL...Enter password:
转载于:https://blog.51cto.com/11886307/2386367