当今的互联网企业中,MySQL数据库大多运行在linux系列操作系统。若应用场景不同、版本不同,MySQL数据库的安装方法也会有区别,下面把常见的几种方法介绍给朋友们
1.1 yum、rpm方式安装
MySQL官网提供了不同版本的rpm安装包,且针对不同操作系统平台的安装包也会有区别。
1.1.1 rpm包安装MySQL
rpm包的安装方式相对简单,首先下载安装包
--- rpm包list
mysql-community-server-8.0.16-2.el7.x86_64.rpm
mysql-community-common-8.0.16-2.el7.x86_64.rpm
mysql-community-devel-8.0.16-2.el7.x86_64.rpm
mysql-community-libs-8.0.16-2.el7.x86_64.rpm
mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm
mysql-community-client-8.0.16-2.el7.x86_64.rpm
--- 查看系统中的rpm包
rpm -qa | grep mysql
--- 删除系统中的rpm包
rpm -e mysql-libs-5.1.73-7.el6.x86_64 --nodeps
--- 安装rpm包
rpm -ivh mysql-*.rpm --force
1.1.2 yum安装MySQL
yum方式安装MySQL最便捷,只需执行一条命令'yum install mysql-server -y'即可。
--安装repository-rpm
sudo yum localinstall platform-and-version-specific-package-name.rpm
-- EL6
sudo yum localinstall mysql80-community-release-el6-{version-number}.noarch.rpm
-- EL7
sudo yum localinstall mysql80-community-release-el7-{version-number}.noarch.rpm
-- 核验mysql的repository list,新mysql的repository的安装情况
yum repolist enabled | grep "mysql.*-community.*"
-- 查看mysql的repository list
yum repolist all | grep mysql
-- 禁用repository中5.7版本,启用8.0
sudo yum-config-manager --disable mysql57-community
sudo yum-config-manager --disable mysql80-community
-- 配置repo文件
vi /etc/yum.repos.d/mysql-community.repo
# Enable to use MySQL 8.0
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
-- 查看repository
yum repolist enabled | grep mysql
-- 安装mysql
sudo yum install mysql-community-server
1.2 cmake编译安装MySQL
MySQL 5.4.xx及后续版本的特殊性,采用cmake或gmake编译安装。
--- 安装环境依赖包
yum install bc binutils compat-libcap1 compat-libstdc++-33 glibc glibc-devel ksh libaio libaio-devel libX11 libXau libXi libXtst libXrender-devel libXrender libgcc libstdc++ libstdc++-devel libxcb make nfs-utils net-tools python python-configshell8 python-rtslib python-six smartmontools sysstat targetcli libgcc_s.so.1 libc.so.6
--- 解压软件包
tar -zxvf mysql-8.0.17.tar.gz
--- 进软件目录
cd mysql8.0
--- 配置&编译&安装
cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql8.0 \
-DMYSQL_DATADIR=/app/mysql8.0/data \
-DMYSQL_UNIX_ADDR=/app/mysql8.0/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
make
make install
ln -s /app/mysql8.0 /app/mysql
注:需安装cmake等编译工具
1.4 二进制免编译安装MySQL
二进制方式免编译安装的方法和yum安装类似,适合各类MySQL产品,无需复杂编译配置及编译时间的等待,解压软件包后初始化数据库即可完成并启动。
2.1 安装Mysql软件
2.1.1 依赖包和编译软件
Linux环境情况如下
--- 查看linux版本
cat /etc/redhat-release
--- 查看内核版本
uname -r
--- 查看操作系统位数
uname -m
2.1.1.1 安装依赖包
MySQL需要的依赖包,需提前安装
rpm -qa | grep ncurses libaio
yum install ncurses-devel libaio-devel -y
2.1.1.2 安装编译软件
MySQL 5.5 及以上版本采用特殊的编译方法,需要安装编译工具cmake
rpm -qa | grep cmake
yum install cmake -y
yum install bc binutils compat-libcap1 compat-libstdc++-33 glibc glibc-devel ksh libaio libaio-devel libX11 libXau libXi libXtst libXrender-devel libXrender libgcc libstdc++ libstdc++-devel libxcb make nfs-utils net-tools python python-configshell8 python-rtslib python-six smartmontools sysstat targetcli libgcc_s.so.1 libc.so.6 gcc gcc-c++
2.1.2 安装MySQL软件
此次实例采用相对复杂的源代码安装方式
2.1.2.1 创建MySQL账号
useradd -s /sbin/nologin -M mysql
id mysql
2.1.2.2 获取MySQL软件包
MySQL下载地址:https://downloads.mysql.com/archives/community/
MySQL 源码包:mysql-5.7.24.tar.gz
2.1.2.3 编译&安装MySQL
配置、编译安装步骤如下:
1. 下载mysql软件包
mkdir -p /opt/media
cd /opt/media
wget -q https://downloads.mysql.com/archives/get/file/mysql-8.0.17.tar.gz
ls -rtlh
2. 解压、配置mysql
tar -zxvf mysql-8.0.17.tar.gz -C /opt/media
cd /opt/media/mysql-8.0.17
cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql8.0 \
-DMYSQL_DATADIR=/app/mysql8.0/data \
-DMYSQL_UNIX_ADDR=/app/mysql8.0/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_BOOST=/usr/local/boost \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0 \
-DFORCE_INSOURCE_BUILD=1
3. 编译、安装mysql
--- 编译,启用4核并行编译
make -j 4
make install
4. 配置安装目录的软连接
ln -s /app/mysql8.0 /app/mysql
ls -lrth /app
ls /app/mysql
完成上述步骤,mysql软件安装完成
2.2 创建MySQL数据库配置文件、目录授权
MySQL 5.5 及以前版本默认提供多个配置文件模板,MySQL 5.6的support-files目录中没有提供模板
--- 查看模板信息
ls -rtlh /app/mysql8.0/support-files/*.cnf
ls -rtlh /app/mysql/support-files/*
--- 备份配置文件
mv /etc/my.cnf /etc/cnf.bak
--- 生成新配置文件(5.7版本没有my-default.cnf)
cp /app/mysql8.0/support-files/my-default.cnf /etc/my.cnf
--- 配置my.cnf
vi /etc/my.cnf
[mysqld]
basedir=/app/mysql8.0
datadir=/app/mysql8.0/data
socket=/app/mysql8.0/tmp/mysql.sock
port=3306
plugin_dir=/app/mysql8.0/lib/plugin
log-error=/app/mysql8.0/logs/error.log
skip-name-resolve
server-id = 10
default-storage-engine = innodb
innodb_buffer_pool_size = 512M
character_set_server = utf8
relay-log-space-limit = 2048M
max-connections = 320
query_cache_limit = 4M
query_cache_size = 64M
query_cache_type = 1
wait_timeout = 9000
connect_timeout = 20
thread_cache_size = 256
key_buffer_size = 16M
join_buffer_size = 2M
max_heap_table_size = 16M
low_priority_updates = 1
max_seeks_for_key = 100
sort_buffer_size = 16M
read_buffer_size = 16M
max_connect_errors = 10
myisam_sort_buffer_size = 64M
tmp_table_size = 64M
read_rnd_buffer_size = 1M
--- 软件目录授权
chown -R mysql.mysql /app/mysql
2.3 初始化MySQL数据库文件
2.3.1 初始化
--- MySQL 5.6及以前版本
/app/mysql/bin/mysql_install_db --basedir=/app/mysql --datadir=/app/mysql/data --user=mysql
--- MySQL 5.7版本
/app/mysql/bin/mysqld --no-defaults --initialize --basedir=/app/mysql --datadir=/app/mysql/data --user=mysql
--- MySQL 8.0版本
/app/mysql8/bin/mysqld --defaults-file=/app/mysql8/my.cnf --initialize --basedir=/app/mysql8 --datadir=/app/mysql8/data --lower-case-table-names=1 --user=mysql
/app/mysql8/bin/mysqld --defaults-file=/app/mysql8/my.cnf --initialize --basedir=/app/mysql8 --datadir=/app/mysql8/data --user=mysql
2.3.2 初始化原理、结果说明
初始化是创建基础的库文件,例如,生成mysql库表等。初始化后查看数据目录
ls -rlth /app/mysql/data
yum install tree -y
tree /app/mysql/data
2.3.3 MySQL初始化故障排错
初始化出现的故障都必须排除,必须出现两个yes字样,否则后面数据库服务可以启动,也会出现登录不了MySQL数据库等各种问题。
2.4 配置并启动MySQL数据库
2.4.1 配置启动脚本
--- 复制启动脚本至启动目录
cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
ls -l /etc/init.d/mysqld
2.4.2 启动MySQL数据库
--- 启动MySQL数据库方法1
/etc/init.d/mysqld start
--- 停止MySQL数据库
/etc/init.d/mysqld stop
--- 启动MySQL数据库方法2
/app/mysql/bin/mysqld_safe --user=mysql &
# “&”表示后台运行mysql服务
注:MySQL启动报错:mysqld_safe directory '/app/mysql/tmp' for UNIX socket file don't exists.需创建tmp目录
解决方案
--- 创建tmp目录
mkdir -p /app/mysql/tmp
chown -R mysql.mysql /app/mysql/tmp
2.4.3 检查mysql端口状态
netstat -lntup | grep mysql
2.4.4 查看mysql启动日志
tail -500f /app/mysql/data/ocean.err
2.4.5 配置MySQL开机自启动
--- 方案一
chkconfig --add mysqld
chkconfig --list | grep mysqld
--- 方案二
vi /etc/rc.local
/etc/init.d/mysqld start
2.5 MySQL命令添加到全局路径
之前配置启动和停止mysql都使用的绝对路径‘/app/mysql/bin/mysql’,很繁琐。添加全局路径后可直接敲mysql。下面来看看配置方法
2.5.1 确认mysql路径
ls /app/mysql/bin/mysql
2.5.2 PATH变量中加入mysql路径
--- profile中添加mysql路径
echo 'export PATH=/app/mysql/bin:$PATH' >> /etc/profile
--- 查看profile配置
tail -l /etc/profile
--- 生效变更的配置
source /etc/profile
--- 查看PATH
echo $PATH
2.5.3 软连接方式
--- mysql所有命令软连接到全局路径下
ln -s /app/mysql/bin/* /usr/local/sbin
2.6 登录mysql,验证服务状态
--- 使用临时密码登录
mysql -uroot -p'eFhu7agc5A%W'
--- 首次登录后需更改密码才能查询数据
ALTER USER USER() IDENTIFIED BY 'root1234';
查看数据库配置
--- 查看当前数据库
show databases;
--- 查看当前用户
select user();
--- 退出
ctrl + d
quit
exit
2.7 MySQL 安全配置
2.7.1 root配置密码
MySQL管理员root密码默认为空,通过mysqladmin配置密码
--- root用户配置密码
mysqladmin -u root password 'ocean123'
--- 登录验证
mysql -uroot -p
键入 ocean123
--- 交互模式配置密码
mysql_secure_installation
2.7.2 清理mysql无用账户
--- 查看用户信息
select user,host from mysql.user;
--- 删除无用账户
drop user root@'::1';
drop user root@'node1';
drop user ''@'node1';
drop user ''@'localhost';
--- 验证用户信息
select user,host from mysql.user;
--- DML删除无用账户
delete from mysql.user where user='' and host='MySQL';
flush privileges;
2.7.3 清理无用库
--- 删除无用库
drop database test;
--- 查看库信息
show databases;
1. 系统最小化安装,很多组件包没有安装
yum install bc binutils compat-libcap1 compat-libstdc++-33 glibc glibc-devel ksh libaio libaio-devel libX11 libXau libXi libXtst libXrender-devel libXrender libgcc libstdc++ libstdc++-devel libxcb make nfs-utils net-tools python python-configshell8 python-rtslib python-six smartmontools sysstat targetcli libgcc_s.so.1 libc.so.6 gcc gcc-c++
2. 高版本mysql需要boots库的安装才可以正常运行
cmake的报错信息
CMake Error at cmake/boost.cmake:81 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=
This CMake script will look for boost in . If it is not there,
it will download and unpack it (in that directory) for you.
If you are inside a firewall, you may need to use an http proxy:
export http_proxy=http://example.com:80
Call Stack (most recent call first):
cmake/boost.cmake:238 (COULD_NOT_FIND_BOOST)
CMakeLists.txt:507 (INCLUDE)
下载boost包,创建目录
mkdir -p /usr/local/boost
tar -zxvf boost_1_59_0.tar.gz -C /usr/local/boost
cmake需要添加“-DWITH_BOOST=/usr/local/boost \”
3. 缺少bison组件包
cmake报错信息
CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
Call Stack (most recent call first):
sql/CMakeLists.txt:562 (INCLUDE)
CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
Call Stack (most recent call first):
libmysqld/CMakeLists.txt:194 (INCLUDE)
yum install bison