官方网站:
https://www.mysql.com
软件包下载:从官网下载
官方文档:
https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
安装前准备:
# 关闭防火墙、selinux
$ echo -e "\033[31m 1\. 防火墙 Selinux 设置 \033[0m" \
&& service iptables stop \
&& chkconfig iptables off \
&& setenforce 0 \
&& sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
# 检查是否安装NySQL,如果安装 卸载之
rpm -qa |grep mysql
yum remove mysql*
# 检查是否安装MariaDB,如果安装 卸载之(重要)
rpm -qa |grep mariadb
yum remove mariadb*
安装过程:
#依赖包:
MySQL依赖于libaio 库。如果未在本地安装此库,则数据目录初始化和后续服务器启动步骤将失败。如有必要,请使用适当的包管理器进行安装
# yum install libaio
#创建用户组、用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
# 解压:
cd /usr/local
tar zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
ln -s mysql-5.7.26-linux-glibc2.12-x86_64 mysql
[root@node1 local]# cd mysql
[root@node1 mysql]# ls
bin COPYING docs include lib man README share support-files
mkdir /usr/local/mysql/{data,binlogs,log,etc,run}
chown -R mysql.mysql /usr/local/mysql/{data,binlogs,log,etc,run}
配置my.cnf文件
[root@node1 ~]# cat /usr/local/mysql/etc/my.cnf
[client]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
[mysqld]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
pid_file = /usr/local/mysql/run/mysql.pid
datadir = /usr/local/mysql/data
default_storage_engine = InnoDB
max_allowed_packet = 512M
max_connections = 2048
open_files_limit = 65535
#skip-grant-tables
skip-name-resolve
lower_case_table_names=1
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 2048M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0
key_buffer_size = 64M
log-error = /usr/local/mysql/log/mysql_error.log
log-bin = /usr/local/mysql/binlogs/mysql-bin
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/log/mysql_slow_query.log
long_query_time = 5
tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 0
query_cache_size = 0
server-id=1
# 初始化mysql
bin/mysqld --initialize --user=mysql --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
#查看初始化密码
grep 'temporary password' /usr/local/mysql/log/mysql_error.log
2019-06-10T05:25:22.448676Z 1 [Note] A temporary password is generated for root@localhost: w;wiSjezq39C
**由上可知,初****始化的root 密码为****:****wiSjezq39C**
# 开启ssl
bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# 启动数据库:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/
/etc/init.d/mysql.server start
或者自己写的启动脚本:
cd /usr/lib/systemd/system
touch mysqld.service
shell> cat mysqld.service
# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# systemd service file for MySQL forking server#
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/run/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables#ExecStartPre=/usr/bin/mysqld_pre_systemd
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
加载:
systemctl daemon-reload
systemctl enable mysqld.service
systemctl is-enabled mysqld
启动:
systemctl start mysqld.service
配置PATH:
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profilesource /etc/profile
使环境变量生效
source /etc/profile
登录:
mysql -uroot -p'okrzVsA(u4h6'
修改密码:
set password=password('newpassword');
安全加固
/usr/local/mysql/bin/mysql_secure_installation
将密码写到文件里面,直接键入my就可以登录了
[root@node1 ~]# more /usr/bin/my
mysql -uroot -p'zshj26@1200'
网友评论