环境:CentOS 6.5+MySQL5.6
本文介绍CentOS系统上常用的两种安装MySQL的方法:yum安装和源码安装。yum安装的优势在于操作简单,只需要执行“yum install 软件名”命令,系统就自动根据yum源配置文件中的镜像位置去下载安装包,并可以自动分析所需的软件依赖关系,自动安装所需的依赖软件包。而源码安装方式是需要自己到网上下载源码包,然后解压安装,此方式可以指定配置参数,更加灵活方便,兼容性更强。
方法一:yum方式安装MySQL
1. 检测系统是否自带安装mysql
# yum list installed | grep mysql
2. 如果发现有系统自带mysql,果断执行如下命令卸载
# yum -y remove mysql-libs.x86_64
3. 搭建MySQL5.6的yum源,执行如下命令下载如下rpm文件
4. 接着执行如下命令,解释一下,这个rpm还不是mysql的安装文件,只是两个yum源文件,执行后,在/etc/yum.repos.d/ 这个目录下多出mysql-community-source.repo和mysql-community.repo
# rpm -ivhmysql-community-release-el6-5.noarch.rpm
5. 这个时候,可以用yum repolist mysql这个命令查看一下是否已经有mysql可安装文件
#yum repolist all | grep mysql
6. 安装mysql 服务器命令(一路yes):
# yum install mysql-community-server
7. 安装成功后启动MySQL服务
# service mysqld start
8. 由于mysql刚刚安装完的时候,mysql的root用户的密码默认是空的,所以我们需要及时用mysql的root用户登录(第一次回车键,不用输入密码),并修改密码
# mysql -u root
# use mysql;
# update user setpassword=PASSWORD("这里输入root用户密码") whereUser='root';
# flush privileges;
9. 查看mysql是否自启动,并且设置开启自启动命令(根据需要执行)
# chkconfig --list | grep mysqld
# chkconfig mysqld on
方法二:源码方式安装MySQL
1. 安装编译源码所需的工具和库
[root@centos65 ~]# yum install gccgcc-c++ ncurses-devel perl
[root@centos65 ~]# yum groupinstall -y"Development tools" "Desktop Platform Development""Server Platform Development"
[root@centos65 ~]# yum -y install cmake
2. 删除系统自带的MySQL
[root@centos65 ~]# rpm -qa | grep mysql
mysql-libs-5.1.73-3.el6_5.x86_64
[root@centos65 ~]# rpm -e --nodepsmysql-libs
3. 创建数据库目录、mysql用户,并修改数据目录的属主
[root@centos65 ~]# mkdir -pv/mydata/data
[root@centos65 ~]# useradd -s/sbin/nologin mysql
[root@centos65 ~]# chown -R mysql.mysql/mydata/data/
4.下载MySQL源码tar包解压
[root@centos65 ~]#wgethttps://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37.tar.gz
[root@centos65 ~]# tar -xfmysql-5.6.37.tar.gz -C /usr/local/
5. 设置编译参数,编译,安装
[root@centos65 ~]# cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data/-DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1-DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1-DWITH_SSL=system -DWITH_ZLIB=system -DMYSQL_UNIX_ADDR=/tmp/mysql.sock-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
[root@centos65 ~]# make
[root@centos65 ~]# make install
6. 初始化数据库
[root@centos65 mysql-5.6.37]# cd/usr/local/mysql/scripts/
[root@centos65 scripts]#./mysql_install_db --user=mysql --basedir=/usr/local/mysql/--datadir=/mydata/data/
7. 复制MySQL服务启动脚本
[root@centos65 scripts]# cd/usr/local/mysql/support-files/
[root@centos65 support-files]# cpmysql.server /etc/init.d/mysqld
[root@centos65 support-files]# chmoda+x /etc/init.d/mysqld
8. 设置MySQL开机启动
[root@centos65 support-files]#chkconfig --add mysqld
[root@centos65 support-files]#chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
9. 优化MySQL命令的执行路径
[root@centos65 ~]# vi /etc/profile
在/etc/profile文件末尾添加如下两行
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
添加完成后退出编辑,执行如下命令使配置生效
[root@centos65 ~]# source /etc/profile
10. 复制MySQL配置文件
[root@centos65 ~]# cp /usr/local/mysql/support-files/my-default.cnf/etc/my.cnf
在my.cnf文件中增加如下配置:
[root@centos65 ~]# vi /etc/my.cnf
innodb_file_per_table = 1
datadir = /mydata/data
port = 3306
user = mysql
server_id = 1
socket = /tmp/mysql.sock
11. 启动MySQL服务
[root@centos65 ~]# service mysqldrestart
[root@centos65 ~]# netstat -antp | grepmysql
tcp 0 0 :::3306 :::* LISTEN34016/mysqld
12. 设置root用户密码
[root@centos65 bin]# /usr/local/mysql/bin/mysql_secure_installation
13. 连接数据库
[root@centos65 bin]# mysql -uroot -pjack
Warning: Using a password on the command lineinterface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.6.37 Source distribution
Copyright (c) 2000, 2017, Oracle and/or itsaffiliates. All rights reserved.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarks of theirrespective
owners.
Type 'help;' or '\h' for help. Type '\c' to clearthe current input statement.
备注:这里root密码是jack
14. 查看mysql是否自启动,并且设置开启自启动命令(根据需要执行)
# chkconfig --list | grep mysqld
# chkconfig mysqld on
以上就是CentOS上常用的两种安装MySQL的方法了,若有疑问,欢迎添加测试交流群交流。
关注公众号,了解更多测试技术
领取专属 10元无门槛券
私享最新 技术干货