MariaDB数据库三种安装方式
Mysql安装
mysql有三种安装方式:yum安装、二进制安装、源码安装,多实例安装
二进制安装
实验环境:Centos7 192.168.37.10
mysql版本:mariadb-10.2.25-linux-x86_64.tar
1、准备用户
# groupadd -r -g 306 mysql
# useradd -r -g 306 -u 306 –d /data/mysql mysql
2、准备数据目录
# mkdir /data/mysql
# chown mysql:mysql /data/mysql
[root@test2 /data]#ll
total 4
drwxr-xr-x. 2 mysql mysql 6 Jul 4 11:04 mysql
3、准备二进制文件
# tar xvf mariadb-VERSION-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -sv mariadb-10.2.25-linux-x86_64/ mysql
# chown -R root:mysql /usr/local/mysql/
[root@test2 /usr/local/mysql]#ll
total 180
drwxr-xr-x. 2 root mysql 4096 Mar 15 01:07 bin
-rw-r--r--. 1 root mysql 17987 Jun 15 02:29 COPYING
-rw-r--r--. 1 root mysql 2354 Jun 15 02:29 CREDITS
drwxrwxr-x. 3 root mysql 18 Jun 15 23:35 data
-rw-r--r--. 1 root mysql 8245 Jun 15 02:29 EXCEPTIONS-CLIENT
drwxrwxr-x. 3 root mysql 19 Jun 15 23:35 include
-rw-r--r--. 1 root mysql 8694 Jun 15 02:29 INSTALL-BINARY
drwxr-xr-x. 5 root mysql 4096 Mar 15 01:07 lib
drwxrwxr-x. 4 root mysql 30 Jun 15 23:35 man
drwxrwxr-x. 11 root mysql 4096 Jun 15 23:35 mysql-test
-rw-r--r--. 1 root mysql 2843 Jun 15 02:29 README.md
-rw-r--r--. 1 root mysql 19477 Jun 15 02:29 README-wsrep
drwxrwxr-x. 2 root mysql 30 Jun 15 23:35 scripts
drwxrwxr-x. 32 root mysql 4096 Jun 15 23:35 share
drwxrwxr-x. 4 root mysql 4096 Jun 15 23:35 sql-bench
drwxrwxr-x. 3 root mysql 275 Jun 15 23:35 support-files
-rw-r--r--. 1 root mysql 86263 Jun 15 02:29 THIRDPARTY
4、准备配置文件
# mkdir /etc/mysql/
# cp support-files/my-large.cnf /etc/mysql/my.cnf #support-files在mysql目录下
# vim /etc/mysql/my.cnf
[mysqld]中添加三个选项:
28 datadir = /data/mysql
29 innodb_file_per_table = on #默认存储引擎innodb
30 skip_name_resolve = on # 禁止主机名解析,建议使用
5、创建数据库文件
# cd /usr/local/mysql/
# ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
6、准备服务脚本,并启动服务
# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# service mysqld start
7、PATH路径
# echo ‘PATH=/user/local/mysql/bin:$PATH’ > /etc/profile.d/env.sh # 环境变量加入到启动脚本中
(8)安全初始化
/user/local/mysql/bin/mysql_secure_installation # mysql安全加固 设置数据库管理员root口令
禁止root远程登录
删除anonymous用户帐号
删除test数据库
源码编译安装
实验环境:Centos7 192.168.37.7
mysql版本:mariadb-10.2.25.tar
1、安装包解决依赖关系
yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel
2、做准备用户和存储数据目录
useradd –r –s /sbin/nologin –d /data/mysql/ mysql
mkdir /data/mysql
chown mysql.mysql /data/mysql
3、解压并cmake 编译安装(过程较久需要耐心等待)
tar xvf mariadb-10.2.18.tar.gz
cmake的重要特性之一是其独立于源码(out-of-source)的编译功能,即编译工作可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译
编译选项:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
cd mariadb-10.2.25/
cmake . \
-DCMAKE_INSTALL_PREFIX=/app/mysql \ # 指定安装目录
-DMYSQL_DATADIR=/data/mysql/ \ # 数据库存放路径
-DSYSCONFDIR=/etc/ \ # 选项文件目录
-DMYSQL_USER=mysql \ # 指定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=utf8mb4 \ #指定字符集
-DDEFAULT_COLLATION=utf8_general_ci
make && make install
提示:如果出错,执行rm -f CMakeCache.txt
准备环境变量
echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
生成数据库文件
cd /app/mysql/
scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
准备配置文件
cp /app/mysql/support-files/my-huge.cnf /etc/my.cnf
准备启动脚本
cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
启动服务
chkconfig --add mysqld
service mysqld start
Mysql多实例
MySQL多实例就是在一台服务器上同时开启多个不同的服务端口(如:3306、3307),同时运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供服务。
这些MySQL多实例共用一套MySQL安装程序,使不同的my.cnf(也可以相同)配置文件、启动程序(也可以相同)和数据文件。在提供服务时,多实例MySQL在逻辑上看来是各自独立的,它们根据配置文件的对应设定值,获得服务器相应数量的硬件资源
实例 :
实验环境:centos7 192.168.37.30
yum源安装MariaDB多实例
1、安装MariaDB
# yum install MariaDB-server MariaDB -y
2、创建⽂件夹,将实例按端口号分开
# mkdir -pv /usr/local/mysql/{3306,3307,3308}/{data,etc,socket,bin,log,pid}
[root@test2 ~]# tree /usr/local/mysql
/usr/local/mysql
├── 3306
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
├── 3307
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
└── 3308
├── bin
├── data
├── etc
├── log
├── pid
└── socket
21 directories, 0 files
3、查询用户,无用户则自行创建,具体步骤参考上一个案例
# id mysql
# ll -d /usr/local/mysql/
drwxr-xr-x 5 mysql mysql 42 May 22 17:18 /usr/local/mysql/
4、为3个MariaDB实例创建初始化数据
# mysql_install_db --user=mysql --datadir=/usr/local/mysql/3306/data
# mysql_install_db --user=mysql --datadir=/usr/local/mysql/3307/data
# mysql_install_db --user=mysql --datadir=/usr/local/mysql/3308/data
5、复制配置文件到多实例并配置
# cp /usr/share/mysql/my-huge.cnf /usr/local/mysql/3306/etc/my.cnf
19 [client]
20 #password = your_password
21 port = 3306
22 socket = /usr/local/mysql/3306/socket/mysql.sock
23
24 # Here follows entries for some specific programs
25
26 # The MySQL server
27 [mysqld]
28 datadir = /usr/local/mysql/3306/data/
29 port = 3306
30 socket = /usr/local/mysql/3306/socket/mysql.sock
31 log-error = /usr/local/mysql/3306/log/mariadb.log
32 pid-file = /usr/local/mysql/3306/pid/mariadb.pid
其他实例复制配置并修改端口号即可:
# cp /usr/local/mysql/3306/etc/my.cnf /usr/local/mysql/3307/etc/my.cnf
# sed -i 's/3306/3307/' /usr/local/mysql/3307/etc/my.cnf
# cp /usr/local/mysql/3306/etc/my.cnf /usr/local/mysql/3308/etc/my.cnf
# sed -i 's/3306/3308/' /usr/local/mysql/3308/etc/my.cnf
6、编写启动脚本
# vim /usr/local/mysql/3306/bin/mysqld
#!/bin/bash
#chkconfig: 345 80 2
port=3306
mysql_user="root"
mysql_basedir="/usr/local/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
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"
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
其实实例复制并配置修改端口号即可
# chmod 755 /usr/local/mysql/3306/bin/mysqld
# cp /usr/local/mysql/3306/bin/mysqld /usr/local/mysql/3307/bin/mysqld
# sed -i 's/3306/3307/' /usr/local/mysql/3307/bin/mysqld
# cp /usr/local/mysql/3306/bin/mysqld /usr/local/mysql/3308/bin/mysqld
# sed -i 's/3306/3308/' /usr/local/mysql/3308/bin/mysqld
7、启动服务
[root@test2 ~]# /usr/local/mysql/3306/bin/mysqld start
Starting MySQL...
[root@test2 ~]# /usr/local/mysql/3307/bin/mysqld start
Starting MySQL...
[root@test2 ~]# /usr/local/mysql/3308/bin/mysqld start
Starting MySQL...
使用ss -ntl查看端口是否已经存在即可成功创建
8、关闭服务
# /usr/local/mysql/3308/bin/mysqld stop
9、 登录和修改密码
设置初始口令:(这是适合在脚本中免密时使用)
mysqladmin -S /usr/lcoal/mysql/3306/socket/mysql.sock password 546803
mysqladmin -S /usr/lcoal/mysql/3307/socket/mysql.sock password 546803
mysqladmin -S /usr/lcoal/mysql/3308/socket/mysql.sock password 546803
登录:mysql -uroot -S /usr/local/mysql/3307/socket/mysql.sock