1. Download the Mysql installation package
Official download address:
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
2. Decompress Mysql
- Create a new installation directory in the server's /usr/local directory, usually named after msyql+version number.
cd/usr/local/
mkdir mysql5.7.34
-
Place the downloaded installation package in this directory
-
Unzip the downloaded compressed package
tar -xzvf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
- Modify the unzipped folder name
mv mysql-5.7.34-linux-glibc2.12-x86_64 mysql
3. Create users and groups
#建立一个mysql的组
groupadd mysql
#建立mysql用户,并且把用户放到mysql组
useradd -r -g mysql mysql
#还可以给mysql用户设置一个密码
passwd mysql 回车设置mysql用户的密码
#修改/usr/local/mysql 所属的组和用户
chown -R mysql:mysql /usr/local/mysql/
4. Modify the configuration file
- Create mysql configuration file
Create my_default.cnf in the /usr/local/mysql5.7.34/mysql/support-files directory, and then edit
vi my_default.cnf
[mysqld]
basedir = /usr/local/mysql5.7.34/mysql
datadir = /usr/local/mysql5.7.34/mysql/data
port = 3306
socket = /usr/local/mysql5.7.34/mysql/tmp/mysql.sock
character-set-server=utf8
[client]
default-character-set=utf8
socket = /usr/local/mysql5.7.34/mysql/tmp/mysql.sock
[mysql]
default-character-set=utf8
socket = /usr/local/mysql5.7.34/mysql/tmp/mysql.sock
- Esc key, enter: wq to save
- cp my_default.cnf /etc/my.cnf
- To achieve the automatic execution effect of mysqld -install at boot
- copy mysql.server to the /etc/init.d/ directory
cp mysql.server /etc/init.d/mysql
- Modify /etc/init.d/mysql parameters
vi /etc/init.d/mysql
-
Esc key to exit editing mode, enter: wq to save and exit
-
Create a directory to store socket files (the path is the path to the socket configuration in the my.cnf configuration file)
#进入/usr/local/mysql5.7.34/mysql目录创建
mkdir tmp
#修改组和用户为mysql
chown mysql:mysql tmp
- Add service mysql
chkconfig --add mysql
- Set mysql service to automatic
chkconfig mysql on
5. Initialize the mysql service
#进入目录
cd /usr/local/mysql5.7.34/mysql/bin
- initialization
mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7.34/mysql --datadir=/usr/local/mysql5.7.34/mysql/data
Remember this password, this is the initial password for installing mysql
6. Start mysql
/usr/local/mysql5.7.34/mysql/bin/mysqld_safe --user=mysql &
# 过段时间,当不再刷屏时,按Ctrl + C退出后台进程,然后执行
/etc/init.d/mysql restart
- View progress
ps -ef|grep mysql
7. Log in to the mysql database for the first time
- Login
/usr/local/mysql5.7.34/mysql/bin/mysql -uroot -p
- Enter the temporary password obtained during initialization (to prevent incorrect input, it is best to copy the temporary password obtained through previous initialization)
- If this occurs
- implement
#/usr/local/mysql5.7.34/mysql/bin为安装位置
ln -s /usr/local/mysql5.7.34/mysql/bin/mysql /usr/bin
- change Password
set password=password('root用户的密码');
- Exit mysql
mysql> exit;
8. Set up remote access
- Open port 3306 of the firewall (if the firewall is closed, skip this step)
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --permanent --zone=public --add-port=3306/udp
- Effective immediately
firewall-cmd --reload
- Add a remote access user in mysql
- Use mysql library
mysql> use mysql;
- View the host and user information of the user table
mysql>select host,user from user;
- Set the remote account name and password (mysql 8.0 will report an error, please use orange words to set up remote access)
mysql> grant all privileges on *.* to 'root'@'%' identified by '远程访问mysql的密码' with grant option;
- There is a pit in mysql 8.0 version, we can query the mysql.user table
mysql> select host,user,plugin,authentication_string from user;
-
You will find that the encryption method of the root user is "caching_sha2_password". Remote tools generally do not support this encryption method.
-
Therefore, you need to first modify the encryption method of the root user to "mysql_native_password" and use the following command to modify it
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'root用户的密码';
- Refresh configuration
mysql> flush privileges;
- Then you can use connection software such as navicat to try out the connection happily! !