下载源码:[MySQL安装包](MySQL :: Download MySQL Community Server)
安装指南:MySQL安装指南
官方文档:MySQL官方文档
OS环境:(操作系统最小化安装,并且安装开发工具包)
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
hostnamectl set-hostname <host_name>
vi /etc/hosts
追加以下内容:
<IP> <HOSTNAME>
防火墙状态:
# systemctl status firewalld.service
关闭防火墙:
#关闭防火墙:(临时关闭)
systemctl stop firewalld.service
#禁用防火墙:(永久关闭,需要重启生效)
systemctl disable firewalld.service
#禁用防火墙输出如下:
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
修改后查看防火墙状态:
# systemctl list-unit-files|grep firewalld.service
firewalld.service disabled
查看selinux状态:
getenforce
临时关闭selinux:
setenforce 0
禁用selinux:
vi /etc/selinux/config
更改SELINUX=disabled(重启后生效)
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
groupadd -g 1000 mysql
useradd -u 1000 -g mysql -d /home/mysql mysql
mkdir /data
mkdir /mysql
chown mysql:mysql /data
chown mysql:mysql /mysql
chmod 750 /data
chmod 750 /mysql
备份配置文件
cd /etc/yum.repos.d
mkdir repo
mv ./*.repo ./repo
创建yum配置文件
vi /etc/yum.repos.d/centos_7.6.repo
添加以下内容
[CentOS7.6]
name=CentOS7.6_YUM
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
创建挂载目录并清理YUM缓存
mkdir /mnt/cdrom
mount /dev/sr0 /mnt/cdrom
yum clean all
yum install -y libaio ntp nmap-ncat net-tools lsof
启动NTP服务:
systemctl start ntpd
开机自启动:
systemctl enable ntpd.service
编辑ntp.conf,注释 server 0. 1. 2.等等:
vi /etc/ntp.conf
增加以下内容:(时钟同步服务器IP):
server <GATEWAY_IP> iburst
启用NTP微调:
vi /etc/sysconfig/ntpd
编辑以上文件,添加 **-x **如下:
# Command line options for ntpd
OPTIONS="-x -g"
重启NTP服务:
systemctl restart ntpd.service
查看NTP同步:
ntpq -p
查看NTP状态:
systemctl status ntpd
关闭并禁用chronyd服务:
systemctl stop chronyd.service
systemctl disable chronyd.service
不禁用在重启系统时自动启动会导致ntpd服务无法启动
su - mysql
vi ~/.bash_profile
export MYSQL_HOME=/mysql
export PATH=$PATH:$MYSQL_HOME/bin
LD_LIBRARY_PATH=$MYSQL_HOME/lib:$LD_LIBRARY_PATH
使环境变量生效
source ~/.bash_profile
上传安装包(mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz)到/mysql
目录下,使用mysql用户进行安装操作
cd /mysql
tar -zxvf /mysql/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
cd mysql-5.7.35-linux-glibc2.12-x86_64
mv ./* /mysql
rm -rf /mysql/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
rm -rf /mysql/mysql-5.7.35-linux-glibc2.12-x86_64
mkdir /mysql/etc
cp /etc/my.cnf /mysql/etc/my.cnf
vi /mysql/etc/my.cnf
[mysqld]
user= mysql
port= 3306
server_id= 11
character_set_server= utf8mb4
basedir= /mysql
datadir= /data
socket= /mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links= 0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error= /mysql/mariadb/mariadb.log
pid-file= /mysql/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
mkdir -p /mysql/mariadb
touch /mysql/mariadb/mariadb.log
touch /mysql/mariadb/mariadb.pid
(指定my.cnf文件)
$ mysqld --defaults-file=/mysql/etc/my.cnf --initialize --user=mysql
2022-09-04T07:06:35.113493Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2022-09-04T07:06:35.113597Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
2022-09-04T07:06:35.113737Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-09-04T07:06:35.379994Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-09-04T07:06:35.429408Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-09-04T07:06:35.490945Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 1cf778b0-2c20-11ed-b711-000c29217f4f.
2022-09-04T07:06:35.492421Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-09-04T07:06:36.009198Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-09-04T07:06:36.009249Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-09-04T07:06:36.009869Z 0 [Warning] CA certificate ca.pem is self signed.
2022-09-04T07:06:36.310190Z 1 [Note] A temporary password is generated for root@localhost: !d2:asJ&NADn
$ mysql_ssl_rsa_setup --datadir=/data --uid=mysql
Generating a 2048 bit RSA private key
...+++
.+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
...................................+++
.........................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
......+++
..............................................................+++
writing new private key to 'client-key.pem'
-----
$ mysqld_safe --defaults-file=/mysql/etc/my.cnf --user=mysql &
[1] 20834 2022-09-04T07:07:31.473677Z mysqld_safe Logging to '/mysql/mariadb/mariadb.log'.
2022-09-04T07:07:31.525748Z mysqld_safe Starting mysqld daemon with databases from /data
$ mysql -uroot -p -S /mysql/mysql.sock
Enter password:
初次登录需要输入密码,改密码在初始化数据库时候生成“)3QI#e&YEm/Z”
$ mysql -uroot -p -S /mysql/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
mysql> alter user 'root'@'localhost' identified by 'root123';
Query OK, 0 rows affected (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.35 |
+-----------+
1 row in set (0.00 sec)
(root执行):
cp /mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
编辑配置文件:
vi /etc/init.d/mysql.server
修改以下内容:
basedir=/mysql
datadir=/data
重新加载daemon
systemctl daemon-reload
1、启动MySQL服务
1、系统命令
systemctl start mysqld
service mysql.server start
2、mysql命令
mysqld_safe --defaults-file=/mysql/etc/my.cnf --user=mysql &
mysqld_safe是一个shell 脚本,会调用mysqld启动mysql服务器,并监听服务器。 如果mysqld进程异常终止,mysqld_safe将自动重启mysqld
注意:使用非默认路径的参数文件可以使用–defaults-file参数
2、关闭MySQL服务
1、系统命令
systemctl stop mysqld
service mysql.server stop
2、mysql命令
mysqladmin -uroot -p -S/mysql/mysql.sock shutdown
注意 :socket路径按照实际环境设置