linux 下 Mysql 安装

1.Mysql 下载(linux)

1. 下载源码包(一个压缩包)

image-20210317163318307

2. 下载.rpm 安装包(四个安装包)

image-20210317164059672

3. 下载.rpm 套件组((含依赖组件)

image-20210317164842225

2. 上传到安装目录

根据安装方式(源码方式或 rpm 方式,ftp 到 /home/spms (spms 用户安装

3. 查看和删除旧版 MySql

1. 查询旧版 mysql

可能结果(假设)

image-20210319100704842

以下为我的电脑查询情况,没有安装 mysql,应该不用卸载。其中 libqt4-sql-mysql Qt 连接 MySQL 数据库的驱动,libmysqlclient 是根据 MySQL client/server 协议,使用 C 语言实现的库。有很多的客户端 api 使用 libmysqlclient 这个库去和 MySQL Server 进行通信。可能是安装 php 时安装上的,可以不用删除?

1
2
3
4
spms@ZFQS:~> rpm -qa | grep mysql
libqt4-sql-mysql-4.6.3-5.25.4
libmysqlclient_r15-5.0.96-0.6.1
spms@ZFQS:~>

2. 卸载旧版本 MySQL

普通删除(依据上述查询结果)

1
rpm -e mysql-libs-5.1.73-8.el6_8.x86_64 mysql-server-5.1.73-8.el6_8.x86_64 mysql-devel-5.1.73-8.el6_8.x86_64

可能失败,如下图所示

image-20210319101026658

强力删除

1
rpm -e --nodeps mysql-libs-5.1.73-8.el6_8.x86_64 mysql-server-5.1.73-8.el6_8.x86_64 mysql-devel-5.1.73-8.el6_8.x86_64

image-20210319101219951

3. 删除 /etc/my.cnf 配置文件

1
rm /etc/my.cnf

image-20210319101950684

配置文件 my.cnf 已经被另存在 my.cnf.rpmsave 中

image-20210319102014249

4.RPM 方式安装 MySql

4 个 rpm 安装包,有依赖关系,按照以下顺序分别安装。

目前为 spms 用户获得超级权限后安装,否则出现权限问题,如下。spms 登录,su root 获得权限。

1
2
3
4
spms@ZFQS:~> rpm -ivh mysql-community-common-5.7.32-1.sles12.x86_64.rpm
warning: mysql-community-common-5.7.32-1.sles12.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 5072e1f5
error: can't create transaction lock on /var/lib/rpm/__db.000
spms@ZFQS:~>

目前 for SUSE 12 的 5.7 的最高版本为 5.7.32 (再 suse 11 中安装时,需要依赖,不知道依赖能不能成功补上)

1
2
3
4
rpm -ivh mysql-community-common-5.7.32-1.sles12.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.32-1.sles12.x86_64.rpm
rpm -ivh mysql-community-client-5.7.32-1.sles12.x86_64.rpm
rpm -ivh mysql-community-server-5.7.32-1.sles12.x86_64.rpm

目前 for SUSE 11 的 5.7 的最高版本为 5.7.25 ,我的机器能正常安装,但最后一个包出现警告,看是否影响使用,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
spms@ZFQS:~> su root
口令:
ZFQS:/home/spms #
ZFQS:/home/spms # rpm -ivh mysql-community-common-5.7.25-1.sles11.x86_64.rpm
warning: mysql-community-common-5.7.25-1.sles11.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 5072e1f5
Preparing... ########################################### [100%]
1:mysql-community-common ########################################### [100%]
ZFQS:/home/spms # rpm -ivh mysql-community-libs-5.7.25-1.sles11.x86_64.rpm
warning: mysql-community-libs-5.7.25-1.sles11.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 5072e1f5
Preparing... ########################################### [100%]
1:mysql-community-libs ########################################### [100%]
ZFQS:/home/spms # rpm -ivh mysql-community-client-5.7.25-1.sles11.x86_64.rpm
warning: mysql-community-client-5.7.25-1.sles11.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 5072e1f5
Preparing... ########################################### [100%]
1:mysql-community-client ########################################### [100%]
ZFQS:/home/spms # rpm -ivh mysql-community-server-5.7.25-1.sles11.x86_64.rpm
warning: mysql-community-server-5.7.25-1.sles11.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 5072e1f5
Preparing... ########################################### [100%]
1:mysql-community-server ########################################### [100%]
insserv: warning: script 'S01ITMAgents1' missing LSB tags
insserv: warning: script 'ITMAgents1' missing LSB tags
ZFQS:/home/spms #

查询安装路径

如果想查询 rpm 包详细的安装文件列表及每个文件的实际存储路径,可通过命令 rpm -ql 来查询,如:

1
2
3
rpm -ql mysql-community-common-5.7.32-1.sles12.x86_64
/usr/bin/mysql
/usr/bin/mysql_config_editor

临时密码

mysql 从 5.7 默认安装之后 root 是有密码的,为了加强安全性,mysql5.7 为 root 用户随机生成一个密码,它在 error log 中。
还有,mysql 只有启动过一次才可以查看临时密码。随机密码保存在 /var/log/mysql/mysqld.log 中,可 cat 或 grep 查看

1
2
grep 'temporary password' /var/log/mysql/mysqld.log
2021-03-19T08:30:03.108793Z 1 [Note] A temporary password is generated for root@localhost: f85WFz<2zt.j

登录 Mysql(rpm 安装完自动启动、自动自启动?)

linux 的 root 安装,spms 可以登录

1
2
3
4
5
6
7
8
9
10
11
12
spms@ZFQS:/> mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.25
​```
Copyright (c) 2000, 2019, 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 的 root 本地登录密码

shell 下:

1
mysqladmin -u root  -p password "Mysql123@xl"

或临时密码登录 mysql 后修改:

1
mysql> set password for 'root'@'localhost'=password('Mysql123@xl'); 

5.MySQL 常用命令

查询密码策略

通过 msyql 环境变量可以查看密码策略的相关信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mysql> show variables like '%password%';
+---------------------------------------+--------+
| Variable_name | Value |
+---------------------------------------+--------+
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| log_builtin_as_identified_by_password | OFF |
| mysql_native_password_proxy_users | OFF |
| old_passwords | 0 |
| report_password | |
| sha256_password_proxy_users | OFF |
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+---------------------------------------+--------+
14 rows in set (0.01 sec)

validate_password_policy:密码策略,默认为 MEDIUM 策略
validate_password_dictionary_file:密码策略文件,策略为 STRONG 才需要
validate_password_length:密码最少长度
validate_password_mixed_case_count:大小写字符长度,至少 1 个
validate_password_number_count :数字至少 1 个
validate_password_special_char_count:特殊字符至少 1 个
上述参数是默认策略 MEDIUM 的密码检查规则。

共有以下几种密码策略:

策略 检查规则
0 or LOW Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file

修改密码策略

在 /etc/my.cnf 文件添加 validate_password_policy 配置,指定密码策略

1
2
3
4
5
# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件



validate_password_policy=0121212

如果不需要密码策略,添加 my.cnf 文件中添加如下配置禁用即可:

1
validate_password = off111

重新启动 mysql 服务使配置生效:

1
systemctl restart mysqld

添加远程登录用户

默认只允许 root 帐户在本地登录,如果要在其它机器上连接 mysql,必须修改 root 允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:

1
mysql> GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION;111

配置默认编码为 utf8

1
mysql> show variables like '%character%';

修改 /etc/my.cnf 配置文件,在 [mysqld] 下添加编码配置,如下所示:

1
2
3
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'123123123

重新启动 mysql 服务,查看数据库默认编码如下所示:

默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket 文件:/var/run/mysqld/mysqld.pid

查看数据库的状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mysql> status
--------------

mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper

Connection id: 4
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: less
Using outfile: ''
Using delimiter: ;
Server version: 5.7.25 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 10 hours 20 min 53 sec

Threads: 1 Questions: 5 Slow queries: 0 Opens: 105 Flush tables: 1 Open tables: 98 Queries per second avg: 0.000
--------------

6. 错误及解决

1. 安装权限

1
2
3
4
spms@ZFQS:~> rpm -ivh mysql-community-common-5.7.32-1.sles12.x86_64.rpm
warning: mysql-community-common-5.7.32-1.sles12.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 5072e1f5
error: can't create transaction lock on /var/lib/rpm/__db.000
spms@ZFQS:~>

实际上是权限问题。su root 后可以安装。

2.mysql 密码策略

1
2
3
4
5
6
spms@ZFQS:/> mysqladmin -u root -p password "Mysql123"
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
mysqladmin: unable to change password; error: 'Your password does not satisfy the current policy requirements'
spms@ZFQS:/>
1
2
3
4
root@linuxidc php-5.6.30]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc  --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif  //安装PHP
[root@linuxidc php-5.6.30]# echo $? //安装完成没有错误用这个检查
[root@linuxidc php-7.1.6]# ./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
6.编译安装:

3.phpMyAdmin 连接错误

phpMyAdmin mysqli_real_connect(): (HY000/2002)

进入 phpMyAdmin 目录,复制配置文件 config.sample.inc.php,重命名为 config.inc.php

1
cp config.sample.inc.php config.inc.php

将配置文件里 host 的值 localhost 改为 127.0.0.1,

1
vi config.inc.php
1
$cfg['Servers'][$i]['host'] = '127.0.0.1';