Install
1. Check whether it is installed
rpm -qa | grep mysql
查看出来有东西,可以使用下面命令将其删除(xxx 为文件全名)
rpm -e xxx
2. Install
2.1 Download the official Mysql package
Official website link: https://downloads.mysql.com/archives/community/
2.2 Upload and decompress (the installation package can be deleted after decompression to save space)
tar -zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
2.3 Check whether there are mysql user groups and mysql users, if not, add or ignore:
groups mysql
Add user groups and users
groupadd mysql && useradd -r -g mysql mysql
4. Create a data directory and grant permissions
mkdir -p /data/mysql
chown mysql:mysql -R /data/mysql
5. Modify the configuration file vim /etc/my.cnf (if not, create a new one)
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
6. Initialization
The decompressed mysql-5.7.35-linux-glibc2.12-x86_64 file is moved to /usr/local/mysql (the folder name is changed to mysql without version number information)
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
--defaults-file:指定配置文件
--basedir:
--datadir:指定数据目录
--user: 指定启动用户
--initialize:
7. View the initial password
cat /data/mysql/mysql.err
- start mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start
change Password
1. 开启免密码登陆 修改my.cnf文件 默认在/etc/my.cnf。
vim /etc/my.cnf 在【mysqld】模块下面添加:skip-grant-tables 保存退出。
2. 重启服务,使配置生效 。
service mysql restart
3. 登陆 /usr/local/mysql/bin/mysql -u root -p //不输入密码直接敲回车键
- Refresh rules to allow external access
use mysql #选择访问mysql库
update user set host = '%' where user = 'root'; #使root能再任何host访问
FLUSH PRIVILEGES; #刷新
5. Change password
ALTER USER "root"@"%" IDENTIFIED BY "1234";
FLUSH PRIVILEGES; #刷新
- quit quit
Delete /etc/my.cnf password-free.
重启服务 service mysql restart
- Log in to /usr/local/mysql/bin/mysql -u root -p //Enter the newly modified password 1234 and hit the Enter key