CENTOS 安装MySQL

13 篇文章 1 订阅

CENTOS 安装MySQL

CENTOS 版本 7.3

MYSQL 版本:5.7.29

下载地址:https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

将下载好的安装压缩包解压到/usr/local目录下。PS:初次安装者建议先在/usr/local中操作,熟练以后再自定义到其他目录,因为有很多东西要改的,被目录影响会很不好进行下去

解压 mysql包到/usr/local目录

tar -vzxf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

创建安装包目录的软链

ln -s mysql-5.7.29-linux-glibc2.12-x86_64 mysql

为centos添加mysql用户组和mysql用户(-s /bin/false参数指定mysql用户仅拥有所有权,而没有登录权限)

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

进入mysql目录并为mysql用户赋权

cd /usr/local/mysql
chown -R mysql:mysql ./

安装mysql

 ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
[root@db-7-51 mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

……省略……
2022-07-12T03:27:31.727756Z 1 [Note] A temporary password is generated for root@localhost: yZho-p8Zsmuv
# 命令输出中有生成的临时密码: yZho-p8Zsmuv

启动mysql

./support-files/mysql.server start
[root@db-7-51 mysql]# ./support-files/mysql.server start
Starting MySQL.2022-07-12T03:28:10.588141Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/var/lib/mysql/db-7-51.pid).

报错没有日志文件,创建目录及文件后再次启动。新报错没有/var/lib/mysql/db-7-51.pid文件

[root@db-7-51 mysql]# mkdir -p /var/log/mariadb/
[root@db-7-51 mysql]# touch /var/log/mariadb/mariadb.log
[root@db-7-51 mysql]# chown mysql:mysql /var/log/mariadb/mariadb.log
[root@db-7-51 mysql]# ./support-files/mysql.server start
Starting MySQL.2022-07-12T03:29:34.161359Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
 ERROR! The server quit without updating PID file (/var/lib/mysql/db-7-51.pid).

查看/var/log/mariadb/mariadb.log日志文件

2022-07-12T03:30:04.916037Z 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
……
2022-07-12T03:30:05.591286Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2022-07-12T03:30:05.591295Z 0 [ERROR] Fatal error: Failed to initialize ACL/grant/time zones structures or failed to remove temporary table files.
2022-07-12T03:30:05.591331Z 0 [ERROR] Aborting

修改mysql 配置文件

vim /etc/my.cnf

# 将以下配置复制到my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
socket=/tmp/mysql.sock
 
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld/mysqld.pid
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
[client]
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqld]
log-bin=mysql-bin 
binlog-format=ROW 
server_id=1 
max_connections=1000
 
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

[root@db-7-51 mysql]# mkdir -p /tmp/mysqld/
[root@db-7-51 mysql]# touch /tmp/mysqld/mysqld.pid
[root@db-7-51 mysql]# chown -R mysql:mysql /tmp/mysqld/
[root@db-7-51 mysql]# chmod 777 /tmp/mysqld/mysqld.pid
[root@db-7-51 mysql]# chown -R mysql:mysql /var/log
[root@db-7-51 mysql]# chmod 777 /var/log/mysqld.log

然后再次启动mysql,启动成功

[root@db-7-51 mysql]# ./support-files/mysql.server start
Starting MySQL. SUCCESS!

复制启动命令到 /etc/init.d/mysql,以后就可以使用 service mysql start启动mysql。

[root@db-7-51 mysql]cp support-files/mysql.server /etc/init.d/mysql

将mysql的指令映射到 /usr/bin目录下,就可以直接用mysql命令登录了。

[root@db-7-51 mysql] ln -s  /usr/local/mysql/bin/mysql    /usr/bin

使用刚刚安装mysql得到的初始化密码进行登录

[root@db-7-51 mysql]# mysql -uroot -pyZho-p8Zsmuv
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.29-log

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
# 修改密码为 123456
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)

# 设置密码有效期永不过期
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)

# 修改后可以使用新密码重新登录,可以正常执行命令

[root@db-7-51 mysql]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

设置root用户允许远程登录

# 支持root用户允许远程连接mysql数据库
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;

参考文档:
https://blog.csdn.net/dc282614966/article/details/81098074
https://blog.csdn.net/Hdu_lc14015312/article/details/104540429
https://www.cnblogs.com/linjiqin/p/5270938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值