一、下载MySQL5.6.38安装包
下载地址:https://www.mysql.com/downloads/ 打开网站之后选择Archives
然后再选择开源版本
选择我们要下载的版本:
https://downloads.mysql.com/archives/community/
二、编译安装MySQL5.6.38
# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) # 创建系统帐号 # groupadd -r mysql # useradd -r -g mysql -s /sbin/nologin mysql # 关闭SELinux # setenforce 0 # sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config # 安装依赖包 # yum install ncurses-devel libaio-devel -y # yum install cmake gcc gcc-c++ make autoconf -y # 上面如果没有下载安装包,可直接wget下载 # wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.38.tar.gz # tar xf mysql-5.6.38.tar.gz # cd mysql-5.6.38/ # 进行编译安装 [ ~/mysql-5.6.38]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.38 # 指定安装目录 -DMYSQL_DATADIR=/usr/local/mysql-5.6.38/data # 指定数据目录 -DMYSQL_UNIX_ADDR=/usr/local/mysql-5.6.38/mysql.sock # 指定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_SSL=bundled # 编译其它功能SSL、ZLIB等 -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_EMBEDDED_SERVER=1 -DENABLE_DOWNLOADS=1 -DWITH_DEBUG=0 -DSYSCONFDIR=/etc [ ~/mysql-5.6.38]# make && make install # 编译并安装数据库 # 做软链接并给MySQL目录授权 # ln -s /usr/local/mysql-5.6.38/ /usr/local/mysql # chown -R mysql.mysql /usr/local/mysql
三、初始化数据目录
# MySQL 5.6初始化数据库的命令: # /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data -user=mysql # MySQL 5.7初始化数据库的命令: # /usr/local/mysql/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data -user=mysql
四、拷贝启动服务的脚本
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # chmod 700 /etc/init.d/mysqld
五、改环境变量
# echo 'PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile # source /etc/profile
六、改配置文件,并启动数据库
# vim /etc/my.cnf [mysqld] server_id=1 port=3306 basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/usr/local/mysql/mysql.sock log_bin=/usr/local/mysql/mysql-bin log_error=/var/log/mysql.log character-set-server=utf8 [client]
socket=/usr/local/mysql/mysql.sock
# /etc/init.d/mysqld start
七、设置数据库密码并清空MySQL不安全帐号
# mysqladmin -u root password 123456 # mysql -uroot -p123456 # 清除不安全的用户,先查询用户名为空和没有密码的 > select user,password,host from mysql.user; > drop user root@'127.0.0.1'; > drop user ''@'localhost';