CentOS 7.8离线安装MySQL(以MySQL5.7为例)

tech2023-09-25  96

一、下载MySQL5.7 rpm组合包

mysql5.7下载地址 mysql8.0下载地址

二、安装及配置

挂载iso镜像,并安装以依赖包;mysql-community-server依赖net-tools、perf,若已经安装过vim,可不用额外安装perf。(vim依赖perf)

mkdir /media/CentOS mount -t iso9660 -o ro,loop /opt/CentOS-7-x86_64-DVD-1810.iso /media/CentOS ls /media/CentOS yum --disablerepo=\* --enablerepo=c7-media install -y net-tools perf

安装MySQL

rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64 rpm -ivh ./mysql-community-common-5.7.31-1.el6.x86_64.rpm rpm -ivh ./mysql-community-libs-5.7.31-1.el6.x86_64.rpm rpm -ivh ./mysql-community-client-5.7.31-1.el6.x86_64.rpm rpm -ivh ./mysql-community-server-5.7.31-1.el6.x86_64.rpm

开启3306端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload

关闭SeLinux

setenforce 0 getenforce vi /etc/selinux/config # enforcing修改为disabled SELINUX=disabled

三、配置MySQL

# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql skip-name-resolve # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 lower_case_table_names=1 sql-mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [client] socket=/var/lib/mysql/mysql.sock

mysql登录时报socket找不到终极解决方案

四、修改MySQL数据目录

cd /home mkdir -p /home/data/mysql chown -R mysql:mysql data/mysql vi /etc/my.cnf # 修改datadir数据目录(记得关闭SeLinux) # datadir=/var/lib/mysql datadir=/home/data/mysql

注意: 若已经初始化过MySQL之后,需要复制原始数据目录到新的数据目录,然后启动MySQL服务。

cd /home/data/mysql cp -pr /var/lib/mysql/* .

五、启动MySQL并修改root密码

首次启动MySQL时,会对MySQL进行初始化。所以,请尽量一次性的配置好/etc/my.cnf文件。避免额外的工作步骤。

#查看自动生成的临时密码 cat /var/log/mysqld.log |grep password 查询结果如下: 2020-09-03T07:51:34.045157Z 1 [Note] A temporary password is generated for root@localhost: ZuoOejf5Vj#d #登录 mysql -uroot -p'ZuoOejf5Vj#d' mysql> grant all privileges on *.* to 'root'@'localhost' identified by '123456' with grant option; mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option; mysql> flush privileges;

MySQL8.0创建用户的操作已经不支持grant的同时创建用户的方式,需先创建用户再进行授权。

修改密码: mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; 修改密码并指定加密方式: mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; mysql> flush privileges; 创建用户: mysql> create user 'root'@'%' identified by '123456'; mysql> grant all on *.* to 'root'@'%' WITH GRANT OPTION;; mysql> flush privileges;

六、启动MySQL并设置开机启动

systemctl enable mysqld systemctl start mysqld
最新回复(0)