一 MySQL 源码包安装
1.1 环境准备
首先:检查依赖,当然一下列举的不是所有MySQL安装都必须的。
yuminstall -y autoconf automake zlib libxml libgcrypt libtool
ncurses-develncurses libaio bison gcc gcc-c++ openssl-devel
numactl
其次:下载MySQL源码安装包
mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
然后:检查是否以前装过MySQL,如果装过则删除
rpm-qa|grep mysql
yum-y remove mysql*
1.2 切换root用户,创建mysql用户和组
groupaddmysql
useraddmysql -g mysql
1.3 解压mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
tar-zxf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql
1.4 进入/usr/local/mysql,开始安装数据库
进入mysqlhome目录
cd/usr/local/mysql
创建存放数据的目录
mkdir-p /usr/local/mysql/data
安装数据库
./scripts/mysql_install_db--user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
--user:指定用户
--basedir:指定安装目录
--datadir:指定数据存放目录
在这里我们需要注意:很容易报2个错:
#我们必须进入MySQL home目录安装数据库,否则会报错:
FATALERROR:Couldnotfind./bin/my_print_defaults
Ifyou are using a binary release, you must run this script from
withinthe directory the archive extracted into. If you compiled
MySQLyourself you must run 'make install' first.
# 我们必须解压源码包到安装目录,如果不是也需要创建一个连接指向安装目录,否则报错:
If you are using a binary release, you must eitherbe at the top
level of the extracted archive, or pass the--basedir option
pointing to that location.
解决办法:将解压的目录作为安装路径或者添加软连接
ln -s /opt/tools/db/mysql-5.6.37-linux-glibc2.12-x86_64 /usr/local/mysql
1.5 配置文件并拷贝到/etc
cp /usr/local/mysql/support-files/my-default.cnf/etc/my.cnf
然后拷贝过去之后,就可以配置/etc/my.cnf文件
cp mysql.server /etc/init.d/mysql
1.6 配置环境变量vi/etc/profile
# MYSQL_HOME
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
然后保存退出
source /etc/profile
1.7 添加mysql 到chkconfig服务,并使得开机自启动
chkconfig --add mysql
chkconfig mysql on
1.8 启动mysql服务
service mysql start
或者
/etc/init.id/mysql start
1.9 设置root登录密码
/mysqladmin -u root password '123456'
1.10安装完成,如果考虑到安全问题,需要安全安装执行命令/usr/local/mysql/bin/mysql_secure_installation
1.11 创建新用户并授权
登录mysql
mysql -uroot -p123456
CREATE USER hive@'%' IDENTIFIED BY 'hive';
grantall privileges on *.* to 'hive'@'%' identifiedby 'hive' with grant option;
flush privileges;
二 MySQL RPM 安装
2.1 环境准备
首先:检查依赖,当然一下列举的不是所有MySQL安装都必须的。
yuminstall -y autoconf automake zlib libxml libgcrypt libtool
ncurses-develncurses libaio bison gcc gcc-c++ openssl-devel
numactl
其次:下载MySQLrpm安装包
MySQL-5.6.30-1.el6.x86_64.rpm-bundle.tar
然后:检查是否以前装过MySQL,如果装过则删除
rpm-qa|grep mysql
yum-y remove mysql*
2.2 解压MySQL-5.6.30-1.el6.x86_64.rpm-bundle.tar
tar-xf MySQL-5.6.30-1.el6.x86_64.rpm-bundle.tar
我们可以看到有以下几个rpm包:
MySQL-client-5.6.30-1.el6.x86_64.rpm
MMySQL-devel-5.6.30-1.el6.x86_64.rpm
MySQL-embedded-5.6.30-1.el6.x86_64.rpm
MySQL-server-5.6.30-1.el6.x86_64.rpm
MMySQL-shared-5.6.30-1.el6.x86_64.rpm
MMySQL-shared-compat-5.6.30-1.el6.x86_64.rpm
MySQL-test-5.6.30-1.el6.x86_64.rpm
2.3 RPM安装 MySQL
一般情况下,我们只需安装server,client和devel即可。
# rpm -ivh MySQL-server-5.6.30-1.el6.x86_64.rpm
在这个过程重,提示有一个随机密码产生,可以查看/root/.mysql_secret
第一次连接之后你必须重新设置你的 密码
# rpm -ivh MySQL-client-5.6.30-1.el6.x86_64.rpm
# rpm -ivh MySQL-devel-5.6.30-1.el6.x86_64.rpm
# rpm -ivh MySQL-shared-*
如果需要查看安装到那些目录,可以使用命令:
rpm -qpl MySQL-client-5.6.30-1.el6.x86_64.rpm
安装后对应的目录:
/usr/bin 客户端程序和脚本
/usr/sbin mysqldserver
/var/lib/mysql 日志文件和数据库文件
/var/lib/mysql-files Value of secure_file_priv
/usr/share/info MySQLmanual in Info format
/usr/share/man Unixmanual pages
/usr/include/mysql Include(header) files
/usr/lib/mysql Libraries
/usr/share/mysql Miscellaneoussupport files, including error messages, character set files, sampleconfiguration files, SQL for database installation
/usr/share/sql-bench Benchmarks
2.4 初始化MySQL
/usr/bin/mysql_install_db --user=mysql
2.5 启动以及重新设置root密码
service mysql start
cat /root/.mysql_secret
结果为:
# The random password set for the root user at ThuAug 3 13:06:20 2017 (local time): wpcMzR5StpZEHv0x
登录mysql,更改密码
mysql -uroot -pwpcMzR5StpZEHv0x
setPASSWORD=PASSWORD('123456');
2.6 安装完成,如果考虑到安全问题,需要安全安装执行命令
/usr/ bin/mysql_secure_installation
2.7 创建新用户并授权root和 新用户
登录mysql
mysql -uroot -p123456
CREATE USER hive@'%' IDENTIFIED BY 'hive';
grant all privileges on *.* to 'hive'@'%'identified by 'hive' with grant option;
grant all privileges on *.* to 'root'@'%'identified by '123456' with grant option;
flush privileges;