1. 安装编译源码所需的工具和库
root@iZbp16tff364has58lxi61Z ~]# yum -y install gcc gcc-c++ ncurses-devel perl
root@iZbp16tff364has58lxi61Z ~]# yum -y groupinstall "Development tools" "Desktop Platform Development" "Server Platform Development"
root@iZbp16tff364has58lxi61Z ~]# yum -y install cmake
root@iZbp16tff364has58lxi61Z ~]# yum -y install openssl openssl-devel ncurses ncurses-devel
2. 删除系统自带的MySQL
root@iZbp16tff364has58lxi61Z ~]# rpm -qa | grep mariadb
root@iZbp16tff364has58lxi61Z ~]# yum -y remove mari*
root@iZbp16tff364has58lxi61Z ~]# rm -rf /var/lib/mysql/*
root@iZbp16tff364has58lxi61Z ~]# rpm -qa | grep mariadb
3. 创建数据库目录、mysql用户,并修改数据目录的属主
root@iZbp16tff364has58lxi61Z ~]# mkdir -pv /mydata/data
root@iZbp16tff364has58lxi61Z ~]# useradd -s /sbin/nologin mysql
root@iZbp16tff364has58lxi61Z ~]# chown -R mysql:mysql /mydata/data/
4. 下载MySQL源码tar包解压
root@iZbp16tff364has58lxi61Z ~]# cd /usr/local/src
root@iZbp16tff364has58lxi61Z src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz(若没有则下载)
root@iZbp16tff364has58lxi61Z src]# tar -zxvf mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
5. 设置编译参数,编译,安装(不走这步骤)
root@iZbp16tff364has58lxi61Z src]# cd /usr/local/mysql-5.6.37
root@iZbp16tff364has58lxi61Z mysql-5.6.37]# 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@iZbp16tff364has58lxi61Z mysql-5.6.37]# make
root@iZbp16tff364has58lxi61Z mysql-5.6.37]# make install
6.初始化数据库
[root@iZbp16tff364has58lxi61Z mysql-5.6.51]# cd /usr/local/
[root@iZbp16tff364has58lxi61Z local]# mv mysql-5.6.51-linux-glibc2.12-x86_64/ mysql/
[root@iZbp16tff364has58lxi61Z local]# cd /usr/local/mysql/scripts/
[root@iZbp16tff364has58lxi61Z local]# chown -R mysql:mysql /usr/local/mysql/data/
[root@iZbp16tff364has58lxi61Z local]# cd /usr/local/mysql/scripts/
[root@iZbp16tff364has58lxi61Z scripts]# yum install libaio*
[root@iZbp16tff364has58lxi61Z scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
7. 复制MySQL服务启动脚本
[root@iZbp16tff364has58lxi61Z scripts]# cd /usr/local/mysql/support-files/
[root@iZbp16tff364has58lxi61Z support-files]# cp mysql.server /etc/init.d/mysqld
[root@iZbp16tff364has58lxi61Z support-files]# chmod a+x /etc/init.d/mysqld
8. 配置MySQL环境变量
[root@iZbp16tff364has58lxi61Z support-files]# vi /etc/profile
在/etc/profile文件末尾添加如下两行
export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
添加完成后退出编辑,执行如下命令使配置生效
[root@iZbp16tff364has58lxi61Z support-files]# source /etc/profile
9. 复制MySQL配置文件
[root@iZbp16tff364has58lxi61Z support-files]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
在my.cnf文件中增加如下配置,添加到[mysqld]之后:
[root@iZbp16tff364has58lxi61Z support-files]# vi /etc/my.cnf
innodb_file_per_table = 1
datadir = /usr/local/mysql/data/
port = 3306
user = mysql
server_id = 1
socket = /tmp/mysql.sock
10. 启动MySQL服务
[root@iZbp16tff364has58lxi61Z support-files]# service mysqld start
执行如下命令查看MySQL服务是否正常运行,显示is running...表示正在运行。
[root@iZbp16tff364has58lxi61Z support-files]# service mysqld status
SUCCESS! MySQL running (1971)
11. 设置数据库的root用户密码
[root@iZbp16tff364has58lxi61Z support-files]# /usr/local/mysql/bin/mysql_secure_installation
备注:安装后MySQL的默认密码为空,所以在输入当前密码时直接回车即可。
回车后实例:
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... skipping.
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
12. 设置MySQL服务开机自启动
添加MySQL到开机自启动服务
[root@iZbp16tff364has58lxi61Z support-files]# chkconfig --add mysqld
设置MySQL服务开机自启动
[root@iZbp16tff364has58lxi61Z support-files]# chkconfig mysqld on
查看是否设置成功,执行如下命令查询,如果显示结果与以下结果一致,表示已完成设置开机自启动MySQL
[root@iZbp16tff364has58lxi61Z support-files]# chkconfig --list | grep mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
13. 连接数据库
[root@centos65 bin]# mysql -uroot -p