mysql的安装

mysql的安装

Mysql安装:

1、通过二进制的方式安装
二进制安装方式中,包括rpm版本以及glibc版本。
rpm版本就是在特定linux版本下编译的,如果你的linux版本匹配,就可以安装;
glibc版本是基于特定的glibc版本编译的;
glibc是GNU发布的libc库,即c运行库。glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc。
优点:安装和维护都比较方便,不需要编译。
缺点:可定制性差,可移植性差,不灵活

2、通过源代码编译的安装(mysql-xx.tar.gz)
优点:可定制性强(安装可以根据用户的需求,只安装所需要的功能)
缺点:安装复杂,所需要的时间比二进制的安装要长得多

3、构建自己的二进制rpm包
优点:根据需求定制成自己的rpm包,方便安装维护。
缺点:前期构建耗时较长,相对比较复杂

MySQL国内镜像下载地址:
http://mirrors.sohu.com/mysql/

http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/

开源镜像站点汇总

http://segmentfault.com/a/1190000000375848

http://mirrors.ustc.edu.cn/

版本的选择,潜规则

MySQL5.6:GA 6-12个月 小版本是偶数版本

MySQL5.7:GA 6-12个月 小版本是偶数版本 5.7.17版本以上(MGR MySQL自带的高可用)

源码安装

#0.安装依赖
[root@db01 ~]# yum install cmake -y
[root@db01 ~]# yum install -y ncurses-devel libaio-devel
[root@db01 ~]# yum install gcc gcc-c++ glibc -y

#1.解压
[root@db01 ~]# tar xf mysql-5.6.40.tar.gz 
[root@db01 ~]# ll mysql-5.6.40
total 256
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 BUILD
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 client
drwxr-xr-x  4 7161 31415  4096 2018-02-26 20:50 cmake
-rw-r--r--  1 7161 31415 23275 2018-02-26 20:46 CMakeLists.txt
drwxr-xr-x  3 7161 31415    21 2018-02-26 20:50 cmd-line-utils
-rw-r--r--  1 7161 31415 19838 2018-02-26 20:46 config.h.cmake
-rw-r--r--  1 7161 31415 40929 2018-02-26 20:46 configure.cmake
-rw-r--r--  1 7161 31415 17987 2018-02-26 20:46 COPYING
drwxr-xr-x  2 7161 31415   312 2018-02-26 20:50 dbug
drwxr-xr-x  2 7161 31415    80 2018-02-26 20:50 Docs
-rw-r--r--  1 7161 31415 65958 2018-02-26 20:46 Doxyfile-perfschema
drwxr-xr-x  3 7161 31415   213 2018-02-26 20:50 extra
drwxr-xr-x  4 7161 31415  4096 2018-02-26 20:50 include
-rw-r--r--  1 7161 31415   333 2018-02-26 20:46 INSTALL
drwxr-xr-x  7 7161 31415  4096 2018-02-26 20:50 libevent
drwxr-xr-x  3 7161 31415   224 2018-02-26 20:50 libmysql
drwxr-xr-x  3 7161 31415   204 2018-02-26 20:50 libmysqld
drwxr-xr-x  2 7161 31415   221 2018-02-26 20:50 libservices
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 man
drwxr-xr-x 10 7161 31415   305 2018-02-26 20:50 mysql-test
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 mysys
drwxr-xr-x  2 7161 31415   300 2018-02-26 20:50 mysys_ssl
drwxr-xr-x  9 7161 31415   113 2018-02-26 20:50 packaging
drwxr-xr-x 11 7161 31415   187 2018-02-26 20:50 plugin
-rw-r--r--  1 7161 31415  2496 2018-02-26 20:46 README
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 regex
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 scripts
drwxr-xr-x  4 7161 31415 12288 2018-02-26 20:50 sql
drwxr-xr-x  5 7161 31415  4096 2018-02-26 20:50 sql-bench
drwxr-xr-x  2 7161 31415   155 2018-02-26 20:50 sql-common
drwxr-xr-x 13 7161 31415   169 2018-02-26 20:50 storage
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 strings
drwxr-xr-x  5 7161 31415  4096 2018-02-26 20:50 support-files
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 tests
drwxr-xr-x  5 7161 31415    70 2018-02-26 20:50 unittest
-rw-r--r--  1 7161 31415    88 2018-02-26 20:46 VERSION
drwxr-xr-x  3 7161 31415   298 2018-02-26 20:50 vio
drwxr-xr-x  2 7161 31415    32 2018-02-26 20:50 win
drwxr-xr-x  2 7161 31415  4096 2018-02-26 20:50 zlib

#2.生成
[root@db01 ~]# mkdir /application
[root@db01 ~]# cd mysql-5.6.40/
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.40 \
-DMYSQL_DATADIR=/application/mysql-5.6.40/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.40/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0

---------------------------------------------------------------
#程序存放位置
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.38 \
#数据存放位置
-DMYSQL_DATADIR=/application/mysql-5.6.38/data \
#socket文件存放位置
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.38/tmp/mysql.sock \
#使用utf8字符集
-DDEFAULT_CHARSET=utf8 \
#校验规则
-DDEFAULT_COLLATION=utf8_general_ci \
#使用其他额外的字符集
-DWITH_EXTRA_CHARSETS=all \
#支持的存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
#禁用的存储引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
#启用zlib库支持(zib、gzib相关)
-DWITH_ZLIB=bundled \
#启用SSL库支持(安全套接层)
-DWITH_SSL=bundled \
#启用本地数据导入支持
-DENABLED_LOCAL_INFILE=1 \
#编译嵌入式服务
-DWITH_EMBEDDED_SERVER=1 \
# mysql5.6支持了google的c++mock框架了,允许下载,否则会安装报错。
-DENABLE_DOWNLOADS=1 \
#禁用debug(默认为禁用)
-DWITH_DEBUG=0
#编译
------------------------------------------------------------------

    
#3.编译安装
[root@db01 mysql-5.6.40]# make && make install
[root@db01 application]# ll mysql-5.6.40/
# mysql所有相关命令
drwxr-xr-x  2 root root  4096 2019-11-25 12:19 bin
# 数据存放目录
drwxr-xr-x  3 root root    18 2019-11-25 12:18 data
# mysql初始化命令目录
drwxr-xr-x  2 root root    30 2019-11-25 12:19 scripts
# 额外的文件
drwxr-xr-x  2 root root   136 2019-11-25 12:19 support-files

#4.创建系统用户
[root@db01 /application/mysql-5.6.40/data]# useradd mysql -s /sbin/nologin -M

#5.做软链接
[root@db01 /application/mysql-5.6.40]# ln -s /application/mysql-5.6.40 /application/mysql

#6.拷贝配置文件(覆盖)
[root@db01 ~]# cd /application/mysql-5.6.40/support-files
[root@db01 /application/mysql-5.6.40/support-files]# cp my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y

#7.拷贝启动脚本
[root@db01 /application/mysql-5.6.40/support-files]# cp mysql.server /etc/init.d/mysqld 

#8.初始化数据库
[root@db01 /application/mysql-5.6.40]# cd scripts/
[root@db01 scripts]# ./mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
如果失败 下载一下这个
[root@db01 scripts]# yum install -y autoconf

#9.创建socket文件存放目录
[root@db01 /application/mysql-5.6.40/scripts]# mkdir /application/mysql-5.6.40/tmp

#10.授权
[root@db01 /application/mysql-5.6.40/scripts]# chown -R mysql.mysql /application/mysql*

#11.启动数据库
[root@db01 /application/mysql-5.6.40/scripts]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 

#12.添加环境变量
[root@db01 /application/mysqlscripts]# vim /etc/profile.d/mysql.sh
export PATH="/application/mysql/bin:$PATH"

#13.加载环境变量
[root@db01 /application/mysqlscripts]# source /etc/profile

#14.使用systemd管理MySQL
[root@db01 /application/mysql-5.6.40/scripts]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
    
#15.使用systemd启动MySQL
[root@db01 /application/mysql-5.6.40/scripts]# systemctl start mysqld

二进制安装

#0.安装依赖包
[root@db02 ~]# yum install libaio-devel -y

#1.上传二进制包并解压
[root@db02 ~]# ls
mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
[root@db02 ~]# tar xf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz

#2.创建程序安装目录
[root@db02 ~]# mkdir /application

#3.将mysql程序移动到安装目录
[root@db02 ~]# mv mysql-5.6.40-linux-glibc2.12-x86_64 /application/mysql-5.6.40

#4.做个软连接
[root@db02 ~]# ln -s /application/mysql-5.6.40 /application/mysql

#5.创建系统用户
[root@db02 ~]# useradd mysql -s /sbin/nologin -M

#6.进脚本 拷贝配置文件
[root@db02 ~]# cd /application/mysql/support-files/
[root@db02 /application/mysql/support-files]# cp my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y

#7.拷贝启动脚本
[root@db02 /application/mysql/support-files]#  cp mysql.server /etc/init.d/mysqld

#8.做初始化
[root@db02 /application/mysql/support-files]# cd ..
[root@db02 /application/mysql]# cd scripts/
[root@db02 /application/mysql/scripts]# ./mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data

直接启动会失败
[root@db02 /application/mysql/scripts]# /etc/init.d/mysqld start
/etc/init.d/mysqld: line 244: my_print_defaults: command not found
/etc/init.d/mysqld: line 264: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

#9.修改mysql启动脚本和程序
[root@db02 /application/mysql/scripts]# sed -i 's#/usr/local#/application#g' /etc/init.d/mysqld /application/mysql/bin/mysqld_safe

这样启动就成功了
[root@db02 /application/mysql/scripts]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/application/mysql/data/db02.err'.
. SUCCESS! 

#10.添加一个环境变量
[root@db02 scripts]# vim /etc/profile.d/mysql.sh
export PATH="/application/mysql/bin:$PATH"

#11.加载环境变量
[root@db02 /application/mysql/scripts]# source /etc/profile

#12.给MySQL的root用户设置密码
[root@db02 scripts]# mysqladmin -uroot -p password '123'

#13.连接MySQL
[root@db01 ~]# mysql -uroot -p123

#14.查看MySQL中所有用户
mysql> select user,host from mysql.user;

二进制安装如何sys管理?

[root@db04 ~]# vim /etc/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/application/mysql/data
[root@db04 ~]# systemctl start mysqld
posted @ 2019-11-27 14:31  干瘪的柠檬  阅读(180)  评论(0)    收藏  举报