The complete process of Centos building Mysql offline, with detailed explanations in pictures and texts
1. Prepare the installation package
1. libaio installation package
download link:
http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-0.3.109-13.el7.x86_64.rpm
2. mysql installation package
Go to the official website to download the following installation package in advance
Official website download address: official address
The homepage only displays the latest version 8.0 package, switch to the boss version to download through the following operations. Download the corresponding package according to the system type.
2. Transfer installation package
After downloading, use the remote transfer tool to import the file into the centos system.
# 放置安装包的目录
/usr/local/src
# 查看
cd /usr/local/src;ls
(Tools that can be used: mobaxterm, filezilla, xftp. Or use vmware to set up virtual machine shared folders by yourself. You can check Baidu for specific operations.)
After the transfer is complete, it can be found in the corresponding directory.
3. Unzip the installation package
# 解压安装包
tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
# 修改文件夹名称
mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql
4. Installation process
1. Install the libaio package
# 防止初始化报错
rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
2. Create user group mysql
# 创建用户组mysql
groupadd mysql
# 创建用户mysql并将其添加到用户组mysql
useradd -r -g mysql mysql
# dir1、创建data目录,存放初始化后的数据
mkdir /usr/local/src/mysql/data
# 赋予读写权限
chown -R mysql mysql/
# 修改文件用户组
chgrp -R mysql mysql/
3. Create a configuration file
# file1、mysql配置文件
vi /etc/my.cnf
# file2、创建mysqld.log日志文件
cd /var/log/
# wq!保存
vim mysqld.log
# 赋予文件读写权限
chmod 777 mysqld.log
# 更改文件用户及用户组
chown mysql:mysql mysqld.log
# dir2、创建mysqld.pid文件路径
cd /var/run/
mkdir mysqld
# 更改文件夹权限
chmod 777 mysqld
cd mysqld
# file3、新建mysqld.log文件
vim mysqld.pid
# 赋予mysql权限
chmod 777 mysqld.pid
chown mysql:mysql mysqld.pid
After execution, the execution permissions of the mysqld.pid file are as follows:
Configuration file content:
[mysqld]
port=3306
user=mysql
basedir=/usr/local/src/mysql
datadir=/usr/local/src/mysql/data
socket=/usr/local/src/mysql/data/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
bind-address=0.0.0.0
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character_set_server=utf8
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
collation-server=utf8_unicode_ci
[client]
port = 3306
socket=/usr/local/src/mysql/data/mysql.sock
default-character-set = utf8
[mysql]
default-character-set = utf8
4. Initialize the database
# 初始化数据库(重点)
/usr/local/src/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/src/mysql --datadir=/usr/local/src/mysql/data --lc_messages_dir=/usr/local/src/mysql/share
# 部分设备用以上初始化命令会报错,可直接简单初始化
/usr/local/src/mysql/bin/mysqld --initialize --user=mysql
5. View the initial password
cat /var/log/mysqld.log
# 或直接截取内容查找
cat /var/log/mysqld.log | grep "temporary password"
The password is on the last line, pay attention here: root@localhost: initial password
6. Configure quick start
- 1. Create the mysql.sock file
# 初始化之后创建,否则会导致初始化失败
cd /usr/local/src/mysql/data/
vim mysql.sock
# 赋予文件读写权限
chmod 777 mysql.sock
chown mysql:mysql mysql.sock
# 建立之后重启服务
/usr/local/src/mysql/support-files/mysql.server restart
- 2. At this time, you can use the following command to start mysql, but you cannot use the service and systemctl commands to quickly start it, and you need to establish a soft link.
# 启动服务
/usr/local/src/mysql/support-files/mysql.server start
- 3. Create a soft link
# 添加软链接,方便快捷启动
ln -s /usr/local/src/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/src/mysql/bin/mysql /usr/bin/mysql
- 4. Check whether the soft link is successful
ll /etc/init.d/mysql;ll /usr/bin/mysql
7. Start the service
Enter mysql, modify the initial password, and run the remote connection (after the execution here, the password will become: the new password you set)
# 启动服务
systemctl start mysql
service mysql start
# 查看服务状态
systemctl status mysql
service mysql status
# 停止服务
systemctl stop mysql
service mysql stop
# 重启服务
systemctl restart mysql
service mysql restart
log in to mysql
# 登录mysql,执行之后输入初始密码
mysql -u root -p
5. Modify the default password
# 密码设置为123456,永不过期
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
# 刷新权限
flush privileges;
6. Open remote connection
# 以下操作均在mysql中输入
# 切换至mysql数据库
use mysql;
# 远程用户建立,更新登录的用户所允许的IP地址为任意
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
# 或执行以下命令
update user set user.Host='%' where user.User='root';
# 刷新权限
flush privileges;
# 查看配置是否生效
select host,user from user;
7. Add system path (environment variable)
vim /etc/profile
export PATH=/usr/local/src/mysql/bin:$PATH
source /etc/profile
8. Set the boot to start
# 将服务文件拷贝到init.d下,并重命名为mysql
cp /usr/local/src/mysql/support-files/mysql.server /etc/init.d/mysqld
# 赋予可执行权限
chmod 777 /etc/init.d/mysqld
# 添加服务
chkconfig --add mysqld
9. Test connection
Remote login, just log in according to the configured account and password, port 3306.
So far, the construction is complete, and you're done~~