使用yum安装mysql

之前在Linux中yum和apt-get中列出了linux软件管理的一些常用操作,这里实际以mysql为例进行一次安装演示。

配置mysql扩展源

1
rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm

yum安装mysql

1
yum install mysql-community-server -y

查看mysql状态,启动,开机自启设置

查看mysql状态

1
2
3
4
5
6
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html

启动mysql,并设置开机自启

1
2
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld

再次查看状态可以看到mysql已经启动起来了

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2020-08-12 13:08:40 CST; 22s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 30522 (mysqld)
CGroup: /system.slice/mysqld.service
└─30522 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

8月 12 13:08:34 localhost.localdomain systemd[1]: Starting MySQL Server...
8月 12 13:08:40 localhost.localdomain systemd[1]: Started MySQL Server.

使用mysql初始密码登录

mysql的初始密码在哪儿呢?

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# cat /var/log/mysqld.log 
2020-08-12T05:08:34.775425Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
umentation for more details).2020-08-12T05:08:35.467104Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-12T05:08:35.571012Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-12T05:08:35.636312Z 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: dffd9de5-dc59-11ea-9b06-000c298217df.2020-08-12T05:08:35.639692Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-12T05:08:36.708627Z 0 [Warning] CA certificate ca.pem is self signed.
2020-08-12T05:08:36.737506Z 1 [Note] A temporary password is generated for root@localhost: lug76/d98m!F
2020-08-12T05:08:39.278229Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
umentation for more details).

查看该日志文件,可以看到密码为:lug76/d98m!F

或者使用命令行查看,一样的密码

1
2
[root@localhost ~]# echo $(awk '/temporary password/{print $NF}' /var/log/mysqld.log)
lug76/d98m!F

使用该初始密码进行登录

1
mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)

修改数据库密码

我们尝试直接修改密码:

1
2
 set password for root@localhost = password('123456');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

直接报错,提示不符合密码策略要求。

数据库默认密码规则必须携带大小写字母、特殊符号,字符长度大于8否则会报错。
因此设定较为简单的密码时需要首先修改set global validate_password_policy_length参数值。

1
2
3
4
5
6
 set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
set password for root@localhost = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

登录测试

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# mysql -uroot -p123456
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

至此,mysql已经成功安装在我们linux环境中