mysql的安装(yum源安装、下载RPM包安装、二进制安装)

安装包获取

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

方法一:官方yum源方式安装

yum install mysql80-community-release-el7-3.noarch.rpm
yum list | grep "mysql-community"
yum install mysql-community-client mysql-community-server


方法二:下载RPM包安装

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

常用的国内yum源如下:具体配置方法参考基础课件。
网易163 yum源,安装方法查看:http://mirrors.163.com/.help/
中科大的 yum源,安装方法查看:https://lug.ustc.edu.cn/wiki/mirrors/help
sohu的 yum源,安装方法查看: http://mirrors.sohu.com/help/
阿里云的 yum源,安装方法查看: http://mirrors.aliyun.com/repo/
清华大学的 yum源,安装方法查看: https://mirrors.tuna.tsinghua.edu.cn/
浙江大学的 yum源,安装方法查看: http://mirrors.zju.edu.cn/
中国科技大学yum源,安装方法查看: http://centos.ustc.edu.cn/

安装步骤:

  • 1、卸载Mariadb
    rpm -e mariadb-libs postfix
  • 2、安装
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -g mysql mysql
[root@localhost ~]# mkdir mysql
[root@localhost ~]# tar xf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar -C mysql
[root@localhost ~]# cd mysql
[root@localhost mysql]# yum localinstall mysql-community-client-8.0.16-
2.el7.x86_64.rpm mysql-community-server-8.0.16-2.el7.x86_64.rpm mysql-community-
libs-8.0.16-2.el7.x86_64.rpm mysql-community-common-8.0.16-2.el7.x86_64.rpm
  • 3、启动
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
  • 4、mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:
[root@localhost ~]# grep "temporary password" /var/log/mysqld.log

[root@localhost ~]# 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.16
Copyright (c) 2000, 2019, 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';  #大写,小写,数
字,特殊符号
Query OK, 0 rows affected (0.01 sec)

方法三:通用二进制安装

  • 1、下载glibc版本的Mysql

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

  • 2、安装系统依赖包
[root@localhost ~]# 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
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/'
/etc/selinux/config
[root@localhost ~]# setenforce 0
  • 4、修改系统限制参数
[root@localhost ~]# cat >> /etc/security/limits.conf <
#
###custom
#
 * soft  nofile    20480
 * hard  nofile    65535
 * soft  nproc    20480
 * hard  nproc    65535
EOF
  • 5、 修改内核参数
cat >>/etc/sysctl.conf <<"EOF"
vm.swappiness=0
#增加tcp支持的队列数
net.ipv4.tcp_max_syn_backlog = 65535
#减少断开连接时,资源回收
net.ipv4.tcp_max_tw_buckets = 8000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 10
#改变本地的端口范围
net.ipv4.ip_local_port_range = 1024 65535
#允许更多的连接进入队列
net.ipv4.tcp_max_syn_backlog = 4096 
#对于只在本地使用的数据库服务器
net.ipv4.tcp_fin_timeout = 30
#端口监听队列
net.core.somaxconn=65535
#接受数据的速率
net.core.netdev_max_backlog=65535
net.core.wmem_default=87380
net.core.wmem_max=16777216
net.core.rmem_default=87380
net.core.rmem_max=16777216
EOF
[root@localhost ~]# sysctl -p
  • 6、mysql的安装配置
解压安装包
[root@localhost ~]# tar xJf mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz -C /opt/

做软连接到/usr/bin
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ln -s /opt/mysql-8.0.19-linux-glibc2.12-x86_64 MySQL

创建用户
[root@localhost local]# groupadd mysql
[root@localhost local]# useradd -g mysql mysql -d /home/mysql -s /sbin/nologin

修改目录权限
[root@localhost local]# chown -R mysql.mysql mysql/*

初始化数据库
[root@localhost local]# cd mysql/
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2019-07-19T05:34:49.071877Z 0 [System] [MY-013169] [Server] /opt/mysql-8.0.15-
linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.15) initializing of server in
progress as process 17125
2019-07-19T05:34:51.884095Z 5 [Note] [MY-010454] [Server] A temporary password
is generated for root@localhost: 7OFD=f#e?f8P   --原始密码
2019-07-19T05:34:54.559988Z 0 [System] [MY-013170] [Server] /opt/mysql-8.0.15-
linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.15) initializing of server has
completed

创建配置文件,配置文件若存在,需要先注释
if [ -f /etc/my.cnf ]; then mv /etc/my.cnf "/etc/my.cnf.`date
+%Y%m%d%H%m`.bak";fi

修改配置文件
[root@localhost 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

为mysql提供sysv服务脚本
[root@localhost opt]# cd /usr/local/mysql
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld

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

添加环境变量
cd /etc/profile.d/
vim mysql.sh
添加如下内容
export PATH=$PATH:/usr/local/mysql/bin
执行
source mysql.sh

数据库登录并修改密码
密码在初始化后的输出内容里:
[root@localhost profile.d]# mysql -uroot -p
登录数据库后,修改密码:
alter user 'root'@'localhost' identified by 'ABC123.com';

搞定,请大神们多多指点,喜欢我就关注我哦!

你可能感兴趣的:(MySQL的安装)