MYSQL5.7-安装

176 阅读3分钟

下载地址

downloads.mysql.com/archives/co… 根据系统选择不同的安装包

安装步骤

  1. 准备工作
tar -xzvf mysql-5.7.31-el7-x86_64.tar.gz -C /usr/local
cd /usr/local/
ln -s mysql-5.7.31-el7-x86_64/ mysql

cd mysql
mkdir data
groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /usr/local/mysql-5.7.31-el7-x86_64/
  1. 启动
cd /usr/local/mysql/bin

./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

报错:
./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

yum install -y libaio

再次执行mysql安装命令
[root@dawn-100 bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2024-02-04T03:16:01.556710Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-02-04T03:16:01.868316Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-02-04T03:16:01.928357Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-02-04T03:16:02.000816Z 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: b98ae444-c30b-11ee-8a1b-5254003c2a1e.
2024-02-04T03:16:02.003798Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-02-04T03:16:02.925075Z 0 [Warning] CA certificate ca.pem is self signed.
2024-02-04T03:16:03.405441Z 1 [Note] A temporary password is generated for root@localhost: ip2w&Dfj:yA)
  1. 制作启动文件
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
cp /usr/local/mysql/bin/my_print_defaults /usr/bin

vim /etc/init.d/mysqld

填写对应目录路径和端口号
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
port=3306

vim /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
socket=/tmp/mysql.sock
user=mysql
tmpdir=/tmp/
# 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=/usr/local/mysql/data/error.log
pid-file=/usr/local/mysql/data/mysql.pid

vim /etc/profile 添加环境变量

export MYSQL_BASE_DIR=/usr/local/mysql
export PATH="$PATH:$MYSQL_BASE_DIR/bin"

source /etc/profile

启动mysql

[root@dawn /]# service mysqld start
Starting MySQL. SUCCESS!

# 使用临时密码登录mysql
 mysql -uroot -p

修改密码

mysql> alter user 'root'@'localhost' IDENTIFIED BY 'cipher';
Query OK, 0 rows affected (0.03 sec)

同一局域网的其他网址无法访问mysql数据库

[HY000][1130] null, message from server: "Host '192.168.10.1' is not allowed to connect to this MySQL server".

原因:登录用户针对的是:登录主机+用户名

解决:修改root用户对应的host

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host = '%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

临时密码无法登录解决方案

  1. 修改/etc/my.cnf 添加skip-grant-tables
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
socket=/tmp/mysql.sock
user=mysql
tmpdir=/tmp/
skip-grant-tables
  1. 重新启动mysql
[root@dawn data]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
  1. mysql -uroot -p后不输入密码直接回车
[root@dawn data]#  mysql -uroot -p
Enter password:
  1. 将root密码重置为空
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string = '' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
  1. 再次打开/etc/my.cnf; 删除[mysqld] 下添加的 skip-grant-tables
  2. 重新启动mysql
  3. 进入mysql修改密码
[root@dawn data]# mysql -uroot -p
Enter password:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user user() identified by 'cuiqiaoqiao';
Query OK, 0 rows affected (0.00 sec)