美文网首页
MySQL(版本5.7)安装到CentOS7

MySQL(版本5.7)安装到CentOS7

作者: 伊夫_艾尔斯 | 来源:发表于2021-04-17 18:20 被阅读0次
msyqllogo.jpg

1. 从MySQL官方网站下载:

https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

2. 解压文件:

MySQL压缩文件放到你想要解压的服务器目录: /home/software/

tar -zxvf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

3. 准备文件夹:

把解压的MySQL文件夹软连接到: /usr/local/mysql

ln -sv /home/software/mysql-5.7.33-linux-glibc2.12-x86_64/ /usr/local/mysql

特别提醒: 请使用完整路径,否则软连接会异常,报红色警告,找不到文件夹。

创建MySQL数据文件夹:

mkdir /home/software/data/mysql -p

4. 新建mysql用户组和mysql用户:

groupadd mysql
useradd -g mysql -r -s /sbin/nologin -M -d /home/software/data/mysql mysql

可以使用查看命令确认: groups mysql

5. 文件夹赋权限:

chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /home/software/data/mysql/

可以使用命令确认文件夹是否已经更改了所属用户:
drwxr-xr-x. 2 mysql mysql 6 4月 17 17:31 mysql

6. 初始化mysqld:

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

操作示例:

[root@localhost data]# cd /usr/local/mysql/
[root@localhost mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/home/software/data/mysql --initialize
2021-04-17T09:39:25.872760Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-04-17T09:39:26.643075Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-04-17T09:39:26.716741Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-04-17T09:39:26.809413Z 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: cce3db4e-9f60-11eb-aeae-000c29abf484.
2021-04-17T09:39:26.811560Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-04-17T09:39:28.735641Z 0 [Warning] CA certificate ca.pem is self signed.
2021-04-17T09:39:28.867276Z 1 [Note] A temporary password is generated for root@localhost: w7zkw>Sq>8Cb

特别提示: 记录运行最后一句,里面包含了MySQL的初始用户名密码: A temporary password is generated for root@localhost: w7zkw>Sq>8Cb

7. 编辑配置文件及将mysql的服务脚本放到系统服务中,并配置环境变量让系统可以直接使用mysql的相关命令

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# ldconfig
[root@localhost mysql]# echo "PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
[root@localhost mysql]# source /etc/profile.d/mysql.sh
[root@localhost mysql]# chkconfig mysqld on

8. 修改MySQL配置文件: /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/home/software/data/mysql
socket=/tmp/mysql.sock
user=mysql
server_id=1
port=3306

9. 启动MySQL服务:

service mysqld start

运行效果:

[root@localhost mysql]# service mysqld start
Starting MySQL.Logging to '/home/software/data/mysql/localhost.localdomain.err'.
. SUCCESS!

特别提示: mysqld 是MySQL服务端, mysql是MySQL客户端

10. 登录MySQL并修改初始密码:

[root@localhost mysql]# mysql -uroot -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> set password='abcdef';
Query OK, 0 rows affected (0.00 sec)

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

11. 修改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> 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)

特别提示: 注意服务器防火墙对端口3306的限制,可能会导致远程连接不上。

相关文章

网友评论

      本文标题:MySQL(版本5.7)安装到CentOS7

      本文链接:https://www.haomeiwen.com/subject/ndhplltx.html