Linux安装MySQL
-
选择版本
-
下载MySQL5.7安装包
-
解压
tar -zxvf mysql-5.7.33-el7-x86_64.tar.gz
- 重命名
mv mysql-5.7.33-el7-x86_64 mysql-5.7.33
- 创建用户
# 创建用户组
groupadd mysql
# 创建用户
useradd -r -g mysql mysql
- 创建MySQL数据文件夹并授权
mkdir /data/mysql
chown -R mysql:mysql /data/mysql/
- 创建MySQL配置文件
vim /etc/my.cnf
- 添加配置
- 初始化安装
cd mysql-5.7.33/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/tools/mysql-5.7.33/ --datadir=/data/mysql/ --user=mysql --initialize
- 从日志中获取默认密码
cat /data/mysql/mysql.err
2021-03-19T02:56:14.029616Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-03-19T02:56:14.071260Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-03-19T02:56:14.138845Z 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: aaf4895b-885e-11eb-9230-5254004d77d3.
2021-03-19T02:56:14.140141Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-03-19T02:56:14.981359Z 0 [Warning] CA certificate ca.pem is self signed.
2021-03-19T02:56:15.407297Z 1 [Note] A temporary password is generated for root@localhost: 3u;fy%)S<#0<
- 生成MySQL启动脚本
cp /usr/local/tools/mysql-5.7.33/support-files/mysql.server /etc/init.d/mysql
- 启动MySQL
[root@redismaster bin]# service mysql start
Starting MySQL. SUCCESS!
- 查看服务是否启动
[root@redismaster bin]# ps -ef | grep mysql
root 11007 1 0 02:59 pts/0 00:00:00 /bin/sh /usr/local/tools/mysql-5.7.33/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
mysql 11240 11007 0 02:59 pts/0 00:00:00 /usr/local/tools/mysql-5.7.33/bin/mysqld --basedir=/usr/local/tools/mysql-5.7.33 --datadir=/data/mysql --plugin-dir=/usr/local/tools/mysql-5.7.33/lib/plugin --user=mysql --log-error=/data/mysql/mysql.err --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 12593 2619 0 03:03 pts/0 00:00:00 grep --color=auto mysql
- 登录MySQL服务,密码是上面日志中获取到的密码
[root@redismaster bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
- 修改默认密码
mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
- 修改MySQL用户登录访问控制
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>
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
-
尝试使用Navicat登录
-
添加软连接,避免每次都需要到MySQL了目录中执行命令
ln -s /usr/local/mysql/bin/mysql /usr/bin
感谢大佬的分享,我自己实践了一把,并记录一下下,感谢。