CentOS7 安装配置 MySQL5.7.18
参考说明:http://www.centoscn.com/mysql/2017/0424/8736.html
安装版本:
Linux版本:CentOS 7
MySQL版本:mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
软件下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
安装MySQL前最好把系统带的一系统文件夹、文件删除。有时不删除会报错。
MySQL完全删除,请查看MySQL完全删除。
配置步骤:
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
tar -zxvf 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
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
mkdir -p /usr/local/mysql/{log,etc,run}
mkdir -p /data/mysql/{data,binlogs}
ln -s /data/mysql/data /usr/local/mysql/data
ln -s /data/mysql/binlogs /usr/local/mysql/binlogs
chown -R mysql.mysql /usr/local/mysql/{data,binlogs,log,etc,run}
chown -R mysql.mysql /data/mysql
rm -f /etc/my.cnf
在/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-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
初始化数据库
执行下面这条语句,进行数据库初始化。会生成一个随机密码,在日志文件里查看这个密码,并记录下来,第一次登录时使用。
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
在日志文件里会提示一个临时密码,记录这个密码
grep 'temporary password' /usr/local/mysql/log/mysql_error.log
到这一步已经完成了MySQL的安装配置,可以启动了。
打开目录
cd /usr/local/mysql/support-files/
启动MySql
./mysqld.service start
此刻就可以正常的登录mysql了,但是一般情况下,会做一些系统的配置。比如:开机启动等
设置启动项
不同版本的Linux,进行配置的方式不完全一样
CentOS 7
cd /usr/lib/systemd/system
touch mysqld.service
打开新建的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
此时已经可以完成了开机启动的设置,在以后要重启MySQL服务时,执行service mysqld.service start/stop 即可。
登录MySQL
mysql -uroot -p
系统会提示输入密码。此密码为初始化数据库时生成的随机密码。
set password = password('root'); //root 为设置的新密码
开放3306端口号
添加新端口号
firewall-cmd --zone=public --add-port=3306/tcp
重新加载
firewall-cmd --reload
检查是否生效
firewall-cmd --zone=public --query-port=3306/tcp
列出所有的开放端口
firewall-cmd --list-all
mysql 开放登录权限
进行授权操作:
#root是设置的密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
也可以用下面的命令进行配置
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');
重载授权表:
FLUSH PRIVILEGES;