MySQL安装部署——centos7

(本节重点: MySQL的两种安装方法 ①yum安装MySQL②二进制安装MySQL)

一、yum安装MySQL(有两种方法)

1.方法一:

下载rpm包:https://dev.mysql.com/downloads/mysql/

注意:该方法安装时,虽然安装包下载到了本地,但是安装的过程中存在依赖,因此要修改安装网络源来接触依赖。

安装步骤:

1.卸载mariadb(linux默认的数据库)

   [[email protected] ~]#rpm -e mariadb-libs postfix

2.安装

[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -g mysql mysql     #添加MySQL用户组,和用户
[[email protected] ~]# mkdir mysql
[[email protected] ~]#tar xvf mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar.tar -C mysql  (解压-C:指定目录)

3.启动

[[email protected] ~]# systemctl start mysqld
[[email protected] ~]# systemctl enable mysqld           #开机启动

4.查看默认密码在修改密码

方法二:官方源安装(速度慢)

https://dev.mysql.com/downloads/repo/yum/

[[email protected] ~]# yum install mysql80-community-release-el7-3.noarch.rpm

[[email protected] ~]# yum list |grep "mysql-community"

[[email protected] ~]# yum install mysql-community-client mysql-community-server     

二、通用二进制安装

1.下载glibc版本的mysql

http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-8.0/

2.安装系统依赖包

[[email protected] ~]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel readline-devel libaio-devel perl libaio wget lrzsz vim libnuma* bzip2 xz
3.关闭selinux

[[email protected] ~]# setenforce 0   #暂时关闭

[[email protected] ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/'  #永久关闭

4.修改配置文件的参数

[[email protected] ~]# vim /etc/security/limits.conf

5.修改内核的参数

[[email protected] ~]# sysctl -p   #立即生效

5.mysql的安装配置

(1)解压安装包

[[email protected] ~]# tar xjf mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz -C /opt/

(2)做软连接到/usr/bin

[[email protected] ~]# cd /opt/
[[email protected] opt]# cd /usr/local/
[[email protected] local]# ln -s /opt/mysql-8.0.19-linux-glibc2.12-x86_64 mysql/

 

(3)创建用户

[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -g mysql mysql     -d /home/mysql  -s  /sbin/nologin

(4)修改目录权限

[[email protected] local]# chown -R mysql.mysql mysql/*

(5)初始化数据库

[[email protected] local]# cd mysql/
[[email protected] mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

(6)创建配置文件,配置文件若存在需要先注释

[[email protected] mysql]# if [ -f /etc/my.cnf ]; then mv /etc/my.cnf "/etc/my.cnf. `date +%Y%m%d%H%m`.bak"; fi

(7)修改配置文件

[[email protected] profile.d]# vim /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
server_id=10
port = 3306
user = mysql
character-set-server = utf8
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/mysqld.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
#####====================================[innodb]==============================
innodb_buffer_pool_size = 1024M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 30
innodb_data_file_path=ibdata1:10M:autoextend

#####====================================[log]==============================
log_error = /var/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /var/log/mysql-slow.log

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 

(8)为MySQL提供sysv服务脚本

[[email protected] profile.d]# cd /usr/local/mysql/
[[email protected] mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[[email protected] mysql]# chmod +x /etc/rc.d/init.d/mysqld

(9)启动MySQL

[[email protected] mysql]# chkconfig --add mysqld      --添加为系统服务
[[email protected] mysql]# chkconfig mysqld on   --开机自启动
[[email protected] mysql]# systemctl start mysqld

(10)将MySQL的bin目录加入PATH环境变量,编辑/etc/profile文件,使用mysql客户端命令

[[email protected] ~]# cd /etc/profile.d/
[[email protected] ~]# vim mysql.rc

export PATH=$PATH:/usr/local/mysql/bin  #添加这一条

[email protected] ~]# source mysql.sh

(11)数据库登录并改密码

[[email protected] mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user 'root'@'localhost' identified by 'ABC123.com';  #修改密码为ABC23.com