mysql的yum、二进制、源码编译安装

mysql安装

通过RPM包安装

#centos8安装
[root@centos8 ~]# yum -y install mysql-server
[root@centos8 ~]# systemctl enable --now mysqld  #开启服务
#centos7安装(因例实原因centos7官方包中只有mariadb包)
#可以通过源码编译安装或找到mysql专用yum源安装
[root@centos7 ~]# yum -y install mariadb-server
[root@centos7 ~]# systemctl enable --now mariadb
#ubuntu18.04安装
root@ubuntu18:/# apt list mysql*
Listing... Done
mysql-client/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 all
mysql-client-5.7/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 amd64
mysql-client-core-5.7/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 amd64
mysql-common/bionic 5.8+1.0.4 all
mysql-sandbox/bionic 3.2.05-1 all
mysql-server/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 all
mysql-server-5.7/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 amd64
mysql-server-core-5.7/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 amd64
mysql-source-5.7/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 amd64
mysql-testsuite/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 all
mysql-testsuite-5.7/bionic-updates,bionic-security 5.7.37-0ubuntu0.18.04.1 amd64
mysql-utilities/bionic 1.6.4-1 all
mysql-workbench/bionic 6.3.8+dfsg-1build3 amd64
mysql-workbench-data/bionic 6.3.8+dfsg-1build3 all
mysqltcl/bionic 3.052-2 amd64
mysqltuner/bionic 1.7.2-1 all
root@ubuntu18:/# apt -y install mysql-server   #默认开启服务但只能本地访问

二进制安装

二进制安装就是已经编译了源码只要准备运行环境稍加配linux系统置就可以运行程序

通用包下载地址:https://downloads.mysql.com/arcmysql增删改查语句hives/community/

例:mysql-5.7.33安装

1、安装相关依赖包准备centos是什么系统二进制安装通用包

[root@centos7 test]# yum  -y install libaio numactl-libs
[root@centos7 data]# ls
mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

2、创建用户和组

[root@centos7 test]# groupadd mysql
[root@centos7 test]# useradd -r -g mysql -s /bin/false mysql
[root@centos7 data]# id mysql
uid=994(mysql) gid=2022(mysql) groups=2022(mysql)

3、解压安装包并准mysql数据库备文件

[root@centos7 data]# tar -xvf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local   #因编译时指定目录所以解包需指定路径
[root@centos7 data]# cd /usr/local/
[root@centos7 local]# ln -s mysql-5.7.33-linux-glibc2.12-x86_64/ mysql  #因编译时安装路劲没有携带版本号所以需创建软连接去掉版本号
[root@centos7 local]# chown -R root:root /usr/local/mysql/  #修改目录下所有文件的所属

4、编linux系统写配置文件

[root@centos7 mysql]# vim /etc/my.cnf
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock

5centos是什么系统、修改变量

[root@centos7 mysql]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7 mysql]# . /etc/profile.d/mysql.sh
[root@centos7 mysql]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

6、初始化mysql并设置root口令为空

[root@localhost data]# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

7、添加服务启动脚本

[root@localhost data]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost data]# chkconfig --add mysqld
[root@localhost data]# systemctl start mysqld

8、测试登录

[root@localhost data]# mysql   #应设置为空口令所以隐藏-uroot -p(口令)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

源码编译安装

mysql的编译安装需要使用cmake编译

#安装手册
https://dev.mysql.com/doc/refman/5.6/en/
#编译选项
https://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html
#下载地址
https://downloads.mysql.com/archives/community/

例:以mysql5.6ubuntu系统.51为例

关闭firewalld及selinuxcentos是什么系统

1、准备数据库目录并创建mysql用户

[root@centos8 /]# mkdir -p /data/mysql #数据目录
[root@centos8 /]# mkdir /app  #程序目录
[root@centos8 /]# useradd -r -s /sbin/nologin  -d /data/mysql mysql
[root@centos8 /]# chown mysql.mysql /data/mysql

2、安装依赖包

[root@centos8 /]# yum -y install gcc gcc-c++ cmake bison  zlib-devel libcurl-devel boost-devel   ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel perl-Data-Dumper

3、下linux必学的60个命令载并解压源码包

[root@centos8 app]# tar xvf mysql-5.6.51.tar.gz -C /usr/local/src
[root@centos8 app]# cd /usr/local/src/
[root@centos8 src]# cd mysql-5.6.51/
[root@centos8 src]# 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@centos8 src]# make && make install 
#如果出错执行rm -f CMakeCache.txt

4、添加环境变量

[root@centos8 app]# echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos8 app]# . /etc/profile.d/mysql.sh 

5、生成数据库

[root@centos8 app]# cd /app/mysql    #必须在此目录下执行脚本
[root@centos8 mysql]# scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql

6、准备配置文件及服务脚本

[root@centos8 mysql]# cp -b /app/mysql/support-files/my-default.cnf /etc/my.cnf
[root@centos8 mysql]# cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@centos8 mysql]# chkconfig --add mysqld
[root@centos8 mysql]# systemctl start mysqld

7、做安全初始化

[root@centos8 /]# mysql_secure_installation 

MariDB 二进制安装

例:以maridb 10.4.24为例

1、安装依赖包准备程序包linux常用命令

[root@centos8 /]# yum -y install libaio*
[root@centos8 data]# ls
mariadb-10.4.24-linux-x86_64.tar.gz

2、创建账户,官方编译时默认指定运行程序用户为mysql

[root@centos8 /]# useradd -r -s /sbin/nologin  -d /data/mysql mysql

3、解压安装linux删除文件命令包并准备文件

[root@centos8 data]# tar xvf mariadb-10.4.24-linux-x86_64.tar.gz  -C /usr/local
[root@centos8 local]# ln -s /usr/local/mariadb-10.4.24-linux-x86_64/ /usr/local/mysql
[root@centos8 local]# chown -R root:root /usr/local/mysql/

4、准备配置文件mysql数据库

[root@centos8 local]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
basedir=/usr/local/mysql
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

5、修改变量

[root@centos8 local]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos8 local]# . /etc/profile.d/mysql.sh
[root@centos8 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos8 local]# 

6、准备数据库linux删除文件命令目录并运行初始化mysql密码忘记了怎么办linux

[root@centos8 data]#  mkdir mysql
[root@centos8 data]# chown -R mysql:mysql mysql
[root@centos8 data]# cd /usr/local/mysql
[root@centos8 mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql  --datadir=/data/mysql

7、添加服务启动脚本

[root@localhost data]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/maridbd
[root@localhost data]# chkconfig --add maridbd
[root@localhost data]# systemctl start maridbd

8、测试登陆

[root@centos8 mysql]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.4.24-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)]> 
MariaDB [(none)]> status;
--------------
mysql  Ver 15.1 Distrib 10.4.24-MariaDB, for Linux (x86_64) using readline 5.1
Connection id:      10
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server:         MariaDB
Server version:     10.4.24-MariaDB MariaDB Server
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /tmp/mysql.sock
Uptime:         4 min 54 sec
Threads: 6  Questions: 7  Slow queries: 0  Opens: 17  Flush tables: 1  Open tables: 10  Queries per second avg: 0.023
--------------