Linux实战之MySQL数据库——MySQL安装部署

By | 2020年3月27日

关系型的数据库:mysql,(SQ server)
非关系型数据库:redis,memcached,(rabbitMQ)
与数据存储相关:elasticsearch,kafka,(Hbase,hadoop)

Mysql安装部署

mysql国内镜像下载网址
http://mirrors.sohu.com/mysql/
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/
开源镜像站点汇总
http://segmentfault.com/a/1190000000375848
http://mirrors.ustc.edu.cn/

官方源安装

(安装慢)

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

本地包yum安装

(1) 下载yum包
wget http://repo.mysql.com/mysql-community-release-el7-3.noarch
wget http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar
(2) 配置yum源(网络源,本地源)
(3) 卸载mariadb

 rpm -e  postfix-2:2.10.1-7.el7.x86_64
 rpm -e  mariadb-libs-5.5.60-1.el7_5.x86_64

(4) 创建用户并安装

groupadd mysql
useradd -g mysql mysql
mkdir /root/mysql
tar xf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar -C /root/mysql
[root@nfs01 mysql]# yum localinstall mysql-community-common-8.0.16-2.el7.x86_64.rpm #首先安装本包
mysql-community-libs-8.0.16-2.el7.x86_64.rpm	#共享库
mysql-community-client-8.0.16-2.el7.x86_64.rpm	#客户端
mysql-community-devel-8.0.16-2.el7.x86_64.rpm	#开发库
mysql-community-embedded-compat-8.0.16-2.el7.x86_64.rpm	#嵌入式
mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm
mysql-community-server-8.0.16-2.el7.x86_64.rpm	#服务端
mysql-community-test-8.0.16-2.el7.x86_64.rpm	#测试组件

(5) 启动mysql

systemctl start mysqld
systemctl enable mysqld

(6) 登录并修改密码

[root@nfs01 ~]# grep "temporary" /var/log/mysqld.log
2020-02-18T06:05:25.342294Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: oiI8s3UXC1!)
[root@nfs01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
……
mysql> alter user 'root'@'localhost' identified by 'Zhao123@com';	#大写,小写,数字,特殊符号 
Query OK, 0 rows affected (0.00 sec)

通用二进制安装

关闭seliunx

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

修改系统限制参数

[root@mysql ~]# cat /etc/security/limits.conf
#
###custom
#
*       soft    nofile  20480
*       hard    nofile  65535
*       soft    nproc   20480
*       hard    nproc   65535

修改内核参数

[root@mysql ~]# cat /etc/sysctl.conf
# For more information, see sysctl.conf(5) and sysctl.d(5).
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

sysctl -p
安装系统依赖包

yum -y install make gcc-c++ cmake bison-devel ncurses-devel readline-devel libaio-devel perl libaio wget lrzsz vim libnuma* bzip2 xz

mysql的安装配置

(1) 解压、软链接

[root@mysql ~]# tar xJf mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz -C /opt/
[root@mysql ~]# ln -s /opt/mysql-8.0.16-linux-glibc2.12-x86_64 /usr/local/mysql

(2) 创建用户,修改目录权限

[root@mysql ~]# groupadd mysql
[root@mysql ~]# useradd -g mysql mysql -d /home/mysql -s /sbin/nologin
[root@mysql ~]# chown -R mysql:mysql /usr/local/mysql/*

(3) 初始化数据库

[root@mysql ~]# cd /usr/local/mysql
[root@mysql mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

(4) 创建配置文件

[root@mysql ~]# if [ -f /etc/my.cnf ];then mv /etc/my.cnf /etc/my.cnf`date +%Y%m%d%H%M`.bak;fi
[root@mysql ~]# cat /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

(5) 为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

(6) 启动mysql

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

(7) 将mysql的bin目录加入PATH环境变量,编辑/etc/profile文件

cd /etc/profile.d/
vim mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
source mysql.sh

(8) 数据库登录并修改密码
密码在初始化后的输出内容里

[root@nfs01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
……
mysql> alter user 'root'@'localhost' identified by 'Zhao123@com';	#大写,小写,数字,特殊符号
Query OK, 0 rows affected (0.00 sec)

请关注公众号获取更多资料

发表评论