3 kinds of installation and multi-instance (Centos) step by step _MySQL of

Install MYSQL way

  1. Source: compiler installation
  2. Binary format package: to expand a specific path, and can use a simple configuration through the
  3. Package Manager package
    (1) CentOS installation disc
    (2) project official: https://downloads.mariadb.org/mariadb/repositories/
    (3) Domestic Mirror: HTTPS: //mirrors.tuna.tsinghua. edu.cn/mariadb/yum/
    (4) domestic mirror: https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/

yum install

  1. Yum configuration source
[root]$ vim /etc/yum.repos.d/mariadb.repo
[mariadb]
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.4/centos7-amd64/
gpgcheck=0
enabled=1

#如果怕其它yum源冲突,可以把其它yum源禁用
  1. installation
[root]$ yum install mariadb-server
  1. Start mysql
[root]$ systemctl start mysql
  1. Connect to the database
[root]$ mysql -h 127.0.0.1 -u root -p centos
#-h指定你要连接的mysql服务器,-u指定用户,-p密码
  1. Two kinds of socket address of the server is listening:
     ip socket: listening tcp port 3306, support for remote communication
     unix sock: sock listen on file, only supports native communication
    such as: /var/lib/mysql/mysql.sock,host is localhost, automatically uses unix sock 127.0.0.1 when
[101]$ ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
...   
LISTEN     0      80          :::3306                    :::*
...
  1. mysql security configuration script
#执行这个脚本
[root]$ /usr/bin/mysql_secure_installation
#输入root(mysql)的密码。默认没有,直接回车
Enter current password for root (enter for none): 
#是否切换到unix套接字身份验证[Y/n]
Switch to unix_socket authentication [Y/n] n
#是否设置root密码
Change the root password? [Y/n]
#如果选Y,就输入2次密码
New password:
Re-enter new password:
#是否删除匿名用户?(就是空用户),建议删除
Remove anonymous users? [Y/n]
#是否不允许远程root登录
Disallow root login remotely? [Y/n]
#是否删除test数据库
Remove test database and access to it? [Y/n]
#是否加载权限使之生效
Reload privilege tables now? [Y/n]

Universal Binary format installation process

  1. Ready binaries
    can be downloaded here: official download
tar xf mariadb-VERSION-linux-x86_64.tar.gz -C /usr/local
cd /usr/local
#创建软连接
ln -s mariadb-VERSION-linux-x86_64 mysql
#更改权限
chown -R root:mysql /usr/local/mysql/
  1. Prerequisites
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 –d /data/mysql -s /sbin/nologin mysql
  1. Ready data directory, it is recommended to use Logical Volume
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
  1. Create a database file
cd /usr/local/mysql/
yum -y install libaio
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
  1. Preparation Profile
mkdir /etc/mysql/
cp support-files/my-huge.cnf /etc/mysql/my.cnf
[mysqld]#中添加三个选项:
datadir = /data/mysql
innodb_file_per_table = on
skip_name_resolve = on #禁止主机名解析,建议使用
  1. Ready to serve the script, and start the service
cd /usr/local/mysql
cp ./support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
  1. PATH Path
echo PATH='/user/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
  1. Safe for initialization
/user/local/mysql/bin/mysql_secure_installation

Source compiler installation mariadb

Can be downloaded here: official download

cmake to compile and install
one of the important characteristics is that it is independent of the cmake source (out-of-source) compiler features, namely compilation can be specified in another directory, rather than be in the source directory, it can not guarantee the source directory any impact once compiled, so the same source tree can be compiled different times, such as the compiler for different platforms
compiler options: https://dev.mysql.com/doc/refman/5.7/en/source- configuration-options.html

  1. Installation package
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  libdb-cxx-devel
  1. Preparing user and directory data
useradd -r -s /sbin/nologin -d /data/mysql mysql
mkdir /data/mysql
chown mysql.mysql /data/mysql

wget http://192.168.99.1/mysql/mariadb-10.2.25.tar.gz
tar xvf mariadb-10.2.25.tar.gz
  1. cmake to compile
    suggestions memory allocation> = 2G, the core of> = 4.
[root]$ mkdir -p /app/mysql
[root]$ cd mariadb-10.2.25/
[root]$ cmake . \
-DCMAKE_INSTALL_PREFIX=/app/mysql/ \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/ \
-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]$ make -j 4 && make -j 4 install

Tip: If an error occurs, you need to perform rm -f CMakeCache.txt, and then re-make

  1. Prepare the environment variable
echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
  1. Generate the database file
cd /app/mysql/
scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
  1. Preparation Profile
cp /app/mysql/support-files/my-huge.cnf /etc/my.cnf
  1. Ready to start the script
cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
  1. Start Service
chkconfig --add mysqld && service mysqld start
  1. PATH Path
echo PATH='/user/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
  1. Safe for initialization
/user/local/mysql/bin/mysql_secure_installation

MariaDB multi-instance experiments:

Examples of multi-source mounting yum

  1. yum install MariaDB source (see above first experiment)
yum install MariaDB-server -y
  1. Create a folder to separate multi-instance.
[centos]$ mkdir -p /usr/local/mysql/330{6,7,8}/{data,etc,socket,bin,log,pid}

#查看下
[centos]$ 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
  1. Created to give permission to the file
[centos]$ chown -R mysql.mysql /usr/local/mysql/
[centos]$ ll /usr/local/mysql/
total 0
drwxr-xr-x 8 mysql mysql 76 Jul  4 09:02 3306
drwxr-xr-x 8 mysql mysql 76 Jul  4 09:02 3307
drwxr-xr-x 8 mysql mysql 76 Jul  4 09:02 3308
  1. Creating initialization data were three instances MariaDB
[centos]$ mysql_install_db --user=mysql --datadir=/usr/local/mysql/3306/data/
Installing MariaDB/MySQL system tables in '/usr/local/mysql/3306/data/' ...
OK

[centos]$ mysql_install_db --user=mysql --datadir=/usr/local/mysql/3307/data/
Installing MariaDB/MySQL system tables in '/usr/local/mysql/3307/data/' ...
OK

[centos]$ mysql_install_db --user=mysql --datadir=/usr/local/mysql/3308/data/
Installing MariaDB/MySQL system tables in '/usr/local/mysql/3308/data/' ...
OK

  1. Modify the configuration file
[centos]$ vim /usr/local/mysql/3306/etc/my.cnf
#复制下面的配置
[client]
port = 3306
socket = /usr/local/mysql/3306/socket/mysql.sock
[mysqld]
datadir = /usr/local/mysql/3306/data/
port = 3306
socket = /usr/local/mysql/3306/socket/mysql.sock
log-error = /usr/local/mysql/3306/log/mariadb.log
pid-file = /usr/local/mysql/3306/pid/mariadb.pid
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8
log-bin=mysql-bin
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
  1. Other examples thereof are also the same two modifications, Attention Port Number
[centos]$ cp /usr/local/mysql/3306/etc/my.cnf /usr/local/mysql/3307/etc/my.cnf
[centos]$ cp /usr/local/mysql/3306/etc/my.cnf /usr/local/mysql/3308/etc/my.cnf
[centos]$ sed -i 's/3306/3307/' /usr/local/mysql/3307/etc/my.cnf
[centos]$ sed -i 's/3306/3308/' /usr/local/mysql/3308/etc/my.cnf
  1. Startup script writing service
[centos]$ 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


#给个755权限
[centos]$ chmod 755 /usr/local/mysql/3306/bin/mysqld
  1. To the other two examples also write service startup script (under the modified copy on it)
[centos]$ cp /usr/local/mysql/3306/bin/mysqld /usr/local/mysql/3307/bin/mysqld
[centos]$ cp /usr/local/mysql/3306/bin/mysqld /usr/local/mysql/3308/bin/mysqld

[centos]$ sed -i 's/3306/3307/' /usr/local/mysql/3307/bin/mysqld
[centos]$ sed -i 's/3306/3308/' /usr/local/mysql/3308/bin/mysqld
  1. Start Service
#启动3306端口
[centos]$ /usr/local/mysql/3306/bin/mysqld start
Starting MySQL...
#启动3307端口
[centos]$ /usr/local/mysql/3307/bin/mysqld start
Starting MySQL...
#启动3308端口
[centos]$ /usr/local/mysql/3308/bin/mysqld start
Starting MySQL...
#查看启动的端口
[centos]$ ss -ntl
State    Recv-Q Send-Q   Local Address:Port   Peer Address:Port     
...
LISTEN     0      80          :::3306                    :::*
LISTEN     0      80          :::3307                    :::*
LISTEN     0      80          :::3308                    :::*
...
  1. Start Mysql
#这里要注意了,如果直接用mysql是启动不了的。要加上套接字的路径
#要启动哪个端口的实例就附上那个路径
[root@localhost ~]$ mysql -S /usr/local/mysql/3307/socket/mysql.sock

Here Insert Picture Description

Multi-instance binary installation

Note: this time with the binary installation of a little difference, so here begin at the beginning:
Note: The following experiment, / usr / local / mysql directory is a binary installation directory, / usr / local / mysql_multi is multi-instance directory , urged to pay attention

  1. Ready binaries
    can be downloaded here: official download

  2. There is also need a package

[centos]$ yum -y install libaio
  1. Guide to virtual machine files, extract to/usr/local
[centos]$ tar xf mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local
[centos]$ cd /usr/local
###创建软连接
[centos]$ ln -s mariadb-10.2.25-linux-x86_64 mysql
  1. Prerequisites
[centos]$ groupadd -r -g 306 mysql
[centos]$ useradd -r -g 306 -u 306 -d /data/mysql -s /sbin/nologin mysql
  1. Change directory permissions
[centos]$ chown -R root:mysql /usr/local/mysql/
  1. Ready data directory, it is recommended to use Logical Volume
[centos]$ mkdir -p /data/mysql
[centos]$ chown -R mysql:mysql /data/mysql

The following section of the multi-instance start

  1. Create a database file,
[centos]$ mkdir -p /usr/local/mysql_multi/330{6,7,8}/{data,etc,socket,bin,log,pid}
[centos]$ chown -R mysql.mysql /usr/local/mysql_multi/
[centos]$ cd /usr/local/mysql/
  1. Creating initialization data were three instances MariaDB
[centos]$ scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql_multi/3306/data/
Installing MariaDB/MySQL system tables in '/usr/local/mysql_multi/3306/data/' ...
OK

[centos]$ scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql_multi/3307/data/
Installing MariaDB/MySQL system tables in '/usr/local/mysql_multi/3307/data/' ...
OK

[centos]$ scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql_multi/3308/data/
Installing MariaDB/MySQL system tables in '/usr/local/mysql_multi/3307/data/' ...
OK
  1. Modify the configuration file
[centos]$ cp /usr/local/mysql/support-files/my-huge.cnf  /usr/local/mysql_multi/3306/etc/my.cnf

[centos]$ vim /usr/local/mysql_multi/3306/etc/my.cnf
#复制下面的配置
[client]
port        = 3306
socket      = /usr/local/mysql_multi/3306/socket/mysql.sock
# The MySQL server
[mysqld]
datadir=/usr/local/mysql_multi/3306/data/
port = 3306
socket = /usr/local/mysql_multi/3306/socket/mysql.sock
log-error = /usr/local/mysql_multi/3306/log/mariadb.log
pid-file = /usr/local/mysql_multi/3306/pid/mariadb.pid
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8
log-bin=mysql-bin
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

  1. Other examples thereof are also the same two modifications, Attention Port Number
[centos]$ cp /usr/local/mysql_multi/3306/etc/my.cnf /usr/local/mysql_multi/3307/etc/my.cnf
[centos]$ cp /usr/local/mysql_multi/3306/etc/my.cnf /usr/local/mysql_multi/3308/etc/my.cnf
[centos]$ sed -i 's/3306/3307/' /usr/local/mysql_multi/3307/etc/my.cnf
[centos]$ sed -i 's/3306/3308/' /usr/local/mysql_multi/3308/etc/my.cnf
  1. Startup script writing service
[centos]$ vim /usr/local/mysql_multi/3306/bin/mysqld
#!/bin/bash
#chkconfig: 345 80 2
port=3306
mysql_user="root"
mysql_basedir="/usr/local/mysql_multi"
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


#给个755权限
[centos]$ chmod 755 /usr/local/mysql_multi/3306/bin/mysqld
  1. To the other two examples also write service startup script (under the modified copy on it)
[centos]$ cp /usr/local/mysql_multi/3306/bin/mysqld /usr/local/mysql_multi/3307/bin/mysqld
[centos]$ cp /usr/local/mysql_multi/3306/bin/mysqld /usr/local/mysql_multi/3308/bin/mysqld

[centos]$ sed -i 's/3306/3307/' /usr/local/mysql_multi/3307/bin/mysqld
[centos]$ sed -i 's/3306/3308/' /usr/local/mysql_multi/3308/bin/mysqld
  1. Adding Environment Variables
[centos]$ echo PATH='/usr/local/mysql/bin:$PATH' >> /etc/profile.d/mysql.sh
[centos]$ source /etc/profile.d/mysql.sh
  1. Start Service
#启动3306端口
[centos]$ /usr/local/mysql_multi/3306/bin/mysqld start
Starting MySQL...
#启动3307端口
[centos]$ /usr/local/mysql_multi/3307/bin/mysqld start
Starting MySQL...
#启动3308端口
[centos]$ /usr/local/mysql_multi/3308/bin/mysqld start
Starting MySQL...
#查看启动的端口
[centos]$ ss -ntl
State    Recv-Q Send-Q   Local Address:Port   Peer Address:Port     
...
LISTEN     0      80          :::3306                    :::*
LISTEN     0      80          :::3307                    :::*
LISTEN     0      80          :::3308                    :::*
...
  1. Start Mysql
#这里要注意了,如果直接用mysql是启动不了的。要加上套接字的路径
#要启动哪个端口的实例就附上那个路径
[root@localhost ~]$ mysql -S /usr/local/mysql_multi/3307/socket/mysql.sock

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_42758707/article/details/94590502