CentOS7 安装 MySQL5.7

Posted freelancy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7 安装 MySQL5.7相关的知识,希望对你有一定的参考价值。

版本

  • CentOS7.2 64位
  • mysql5.7.17

MySQL安装包

  • 下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
  • 版本选择:Select Operating System: Linux - Generic
  • 本教程使用:mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

安装前的准备

  • CentOS7.2需要安装libaio
yum install libaio -y

开始安装

  • 解压
tar -zxvf /data/software/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
  • 重命名解压目录
cd /usr/local
mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql
  • 进入mysql目录
cd /usr/local/mysql
  • 创建MySQL的data目录
mkdir data
  • 新建mysql的用户和用户组
groupadd mysql       #添加用户组
useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql     #新建msyql用户禁止登录shell
chown -R mysql.mysql /usr/local/mysql/    赋予mysql用户目录权限
  • 初始化数据库
./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
  • 复制配置文件
cp -a ./support-files/my-default.cnf /etc/my.cnf
  • 如果没有 my-default.cnf,则直接编辑my.cnf文件vim /etc/my.cnf,将以下内容写进去
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
collation_server = utf8_general_ci
character_set_server = utf8
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/mysql.pid
user = root
lower_case_table_names=1
back_log = 600
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
query_cache_limit = 2M
key_buffer_size = 4M
ft_min_word_len = 4
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
[mysqldump]
quick
max_allowed_packet = 16M 
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
  • 添加环境变量
vi /etc/profile # 编辑etc目录下的profile文件
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile # 使用source命令使之配置文件生效
  • 设置启动项
systemctl enable mysql.service
  • 启动MySQL
service mysql start

初始化密码

  • 查看初始密码
cat /root/.mysql_secret
  • 修改密码
# 登录
mysql -u root -p # 回车后输入初始密码
# 修改密码,注意替换掉命令中的密码 your_new_password
SET PASSWORD FOR ‘root‘@localhost=PASSWORD(‘your_new_password‘);

设置开机启动

# 将服务文件拷贝到init.d下,并重命名为mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# 赋予可执行权限
chmod +x /etc/init.d/mysql
# 添加服务
chkconfig --add mysql
# 显示服务列表
chkconfig --list
# 如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则键入
chkconfig --level 345 mysql on

远程访问授权

# 登录MySQL
mysql -u root -p # 回车后输入密码
# 授权命令,注意替换掉命令中的密码 root_password
GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘root_password‘ WITH GRANT OPTION;
# 重新载入授权表
FLUSH PRIVILEGES;

以上是关于CentOS7 安装 MySQL5.7的主要内容,如果未能解决你的问题,请参考以下文章

MySql之centos7.2安装mysql5.7.13

centos7.2安装mysql5.7.13及ssl主从复制

centos7安装mysql5.7步骤(图解版)

centos7安装mysql5.7步骤(图解版)

CentOS7安装MYSQL5.7

CentOS7安装MySQL5.7操作说明