Administrator
发布于 2023-09-11 / 24 阅读 / 0 评论 / 0 点赞

MySql 自表查询插入 测试

2019年源于某大佬考我问 , Oracle 支持insert table A from select from table A

Oracle 不会报错 ,但是MySql 会报错。

当时有空就测试了下 ,结论如下

经测试 insert tableA select * from tableA ;

5.1.72-community 也一样。(2013年)

5.1.30-community 也一样 (2008年)

截止到现在(2019),官网可以下载的版本最低到5.0.15 (2005年)

自表查询插入在以上各个 版本都可以正常执行, 执行结果是复制一份, 结果集翻倍。

以下是测试安装各个版本的MySql ,基于 redhat6.5 的安装 MySql 5.7 ,共计尝试3种方式安装

1.手动rpm包安装

2.使用yum安装

3.Generic Binaries 二进制文件安装方式。

1.手动rpm包安装

官网安装文档链接如下:

https://dev.mysql.com/doc/mysql-linuxunix-excerpt/5.7/en/linux-installation-rpm.html

https://dev.mysql.com/downloads/mysql/直接下载 对应的文件,免费使用的是社区版community。

需要下载至少4个,安装顺序是 mysql-community-common/libs/client/server ,

或者可以下载这个文件 RPM Bundle ,是tar包包含所有的文件。

安装前检查,通过rpm -qa |grep mysql 得到3个结果

[root@svr128 ~]# rpm -qa |grep mysql

mysql-devel-5.1.71-1.el6.x86_64

mysql-5.1.71-1.el6.x86_64

mysql-libs-5.1.71-1.el6.x86_64

不知道 mysql-5.1.71-1.el6.x86_64 这个是干什么的, 但是几乎所有的帖子都说 需要删除mysql-libs

分支a ,删除libs

以下操作参考了网址 https://www.centos.org/forums/viewtopic.php?t=22214

yum remove mysql-libs

提示节选如下

Removing:

mysql-libs x86_64 5.1.71-1.el6

Removing for dependencies:

389-ds-base x86_64 1.2.11.15-29.el6

等等共计27个

回答 Y ,结果如下

Removed:

mysql-libs.x86_64 0:5.1.71-1.el6

Dependency Removed:

389-ds-base.x86_64 0:1.2.11.15-29.el6 cronie.x86_64 0:1.4.4-12.el6

cronie-anacron.x86_64 0:1.4.4-12.el6 crontabs.noarch 0:1.10-33.el6

ipa-server.x86_64 0:3.0.0-37.el6 ipa-server-selinux.x86_64 0:3.0.0-37.el6

libcgroup.x86_64 0:0.40.rc1-5.el6 libcgroup-devel.x86_64 0:0.40.rc1-5.el6

mysql.x86_64 0:5.1.71-1.el6 mysql-devel.x86_64 0:5.1.71-1.el6

numad.x86_64 0:0.5-9.20130814git.el6 pki-ca.noarch 0:9.0.3-32.el6

pki-common.noarch 0:9.0.3-32.el6 pki-silent.noarch 0:9.0.3-32.el6

policycoreutils-python.x86_64 0:2.0.83-19.39.el6 postfix.x86_64 2:2.6.6-2.2.el6_1

redhat-lsb.x86_64 0:4.0-7.el6 redhat-lsb-compat.x86_64 0:4.0-7.el6

redhat-lsb-core.x86_64 0:4.0-7.el6 redhat-lsb-graphics.x86_64 0:4.0-7.el6

redhat-lsb-printing.x86_64 0:4.0-7.el6 slapi-nis.x86_64 0:0.40-4.el6

sysstat.x86_64 0:9.0.4-31.el6 tomcat6.noarch 0:6.0.24-62.el6

tomcatjss.noarch 0:2.1.0-2.el6 webalizer.x86_64 0:2.21_02-3.3.el6

Complete!

依次安装 mysql-community-common/libs/client/server ,未报错。

rpm -ivh xxxxxxxx

还是建议yum install mysql-community-{client,common,libs}-* 安装,即方法2

似乎在本地无法访问源时, 会自动找当前文件夹的安装包。

如果源正常时,yum安装会首先从源下载,即使当前文件夹有安装包。

安装完成

分支b,不删除libs

未删除lib ,采用 rpm -ivh xxx.rpm --nodeps --force方式强制安装

安装过程中的 警告信息如下,4个文件中除libs没有提示警告,其余3个都提示了。

warning: mysql-community-common-5.7.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY

参考网上的讨论,说是centos不支持什么签名,可以忽略(参考上面删除libs的做法,觉得这个错误就是没删除系统自带的5.1版本libs导致的)。

最终可以启动mysqld服务,

chkconfig --list 也能正常显示,貌似正常。

检查新增了用户mysql和用户组mysql

2.使用yum安装

方式1 自备rpm安装包

需要删除 自带的5.1 的libs,否则报错.还需要把4个rpm文件下载好。

For Red Hat Enterprise Linux/Oracle Linux/CentOS 5 systems, there is an extra package (mysql-version-el5-arch.rpm) to be installed; use the following command:

yum install mysql-community-{server,client,common,libs}-* mysql-5.*

方式2 自动从网上下载安装包

按照官方文档说法

https://dev.mysql.com/doc/mysql-linuxunix-excerpt/5.7/en/linux-installation-yum-repo.html

Go to the Download MySQL Yum Repository page (http://dev.mysql.com/downloads/repo/yum/) in the MySQL Developer Zone.

我下载了 mysql57-community-release-el6-11.noarch.rpm

然后执行 yum localinstall mysql57-community-release-el6-11.noarch.rpm

提示This system is not registered to Red Hat Subscription Management.

不过在 /etc/yum.repos.d/ 新增了2个源

mysql-community-source.repo

mysql-community.repo

yum repolist enabled | grep "mysql.*-community.*"

yum repolist all | grep mysql

通过以上2个命令,可以查看源中的安装版本 enable / disable

不是必须的。

利用yum安装旧版本mysql的方法。

可以手动编辑 /etc/yum.repos.d/mysql-community.repo ,

修改5.5项目为 enable=1, 5.7项目enable=0 , 再执行下列安装语句,可以安装5.5版本。

保证任何时间只有一个 enable=1 。

再执行下面的安装命令,

提示 [Errno 14] PYCURL ERROR 6 - "Couldn't resolve host 'repo.mysql.com'"

yum install mysql-community-{server,client,common,libs}-*

vi /etc/resolv.conf 新增 nameserver 8.8.8.8 保存后,可以执行下载安装。

经测试发现,这种安装方式除了会安装指定4个包外,还会多安装 devel / libs-compat ,

并且自动删除 系统自带的 5.1 的3个包

Running Transaction

Installing : mysql-community-common-5.7.18-1.el6.x86_64 1/9

Installing : mysql-community-libs-5.7.18-1.el6.x86_64 2/9

Installing : mysql-community-client-5.7.18-1.el6.x86_64 3/9

Installing : mysql-community-server-5.7.18-1.el6.x86_64 4/9

Installing : mysql-community-devel-5.7.18-1.el6.x86_64 5/9

Installing : mysql-community-libs-compat-5.7.18-1.el6.x86_64 6/9

Erasing : mysql-devel-5.1.71-1.el6.x86_64 7/9

Erasing : mysql-5.1.71-1.el6.x86_64 8/9

Erasing : mysql-libs-5.1.71-1.el6.x86_64 9/9

Verifying : mysql-community-devel-5.7.18-1.el6.x86_64 1/9

Verifying : mysql-community-libs-compat-5.7.18-1.el6.x86_64 2/9

Verifying : mysql-community-server-5.7.18-1.el6.x86_64 3/9

Verifying : mysql-community-libs-5.7.18-1.el6.x86_64 4/9

Verifying : mysql-community-common-5.7.18-1.el6.x86_64 5/9

Verifying : mysql-community-client-5.7.18-1.el6.x86_64 6/9

Verifying : mysql-devel-5.1.71-1.el6.x86_64 7/9

Verifying : mysql-libs-5.1.71-1.el6.x86_64 8/9

Verifying : mysql-5.1.71-1.el6.x86_64 9/9

Installed:

mysql-community-client.x86_64 0:5.7.18-1.el6 mysql-community-common.x86_64 0:5.7.18-1.el6

mysql-community-devel.x86_64 0:5.7.18-1.el6 mysql-community-libs.x86_64 0:5.7.18-1.el6

mysql-community-libs-compat.x86_64 0:5.7.18-1.el6 mysql-community-server.x86_64 0:5.7.18-1.el6

Replaced:

mysql.x86_64 0:5.1.71-1.el6 mysql-devel.x86_64 0:5.1.71-1.el6 mysql-libs.x86_64 0:5.1.71-1.el6

Complete!

**********************************************************************************************************8

启动服务

[root@svr128 ~]# service mysqld start

Initializing MySQL database: [ OK ]

Installing validate password plugin: [ OK ]

Starting mysqld: [ OK ]

安装完毕后,需要重置root密码,root的初始密码在安装log中

grep 'temporary password' /var/log/mysqld.log

登录mysql

mysql -uroot -p

在提示密码时,输入上一步的临时密码,是可以复制输入的。

修改密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'ecloud'

最好也执行这个命令,不用退出和重启。

flush privileges;

要修改成功, 需要使用下方技巧。

****使用临时密码登录后,必须修改密码,执行查询会得到一个错误提示********

mysql> select user();

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

如果只是修改为一个简单的密码,会报以下错误:

mysql> ALTER USER USER() IDENTIFIED BY 'ecloud';

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

Policy

Tests Performed

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

mysql> set global validate_password_policy=0;

mysql> set global validate_password_length=1; --最小值是4,如果设置了小于4的值

5.7 默认安装了validate_password插件,检查是否安装命令

mysql> SHOW VARIABLES LIKE 'validate_password%';

+--------------------------------------+-------+

| Variable_name | Value |

+--------------------------------------+-------+

| validate_password_check_user_name | OFF |

| validate_password_dictionary_file | |

| validate_password_length | 4 |

| validate_password_mixed_case_count | 1 |

| validate_password_number_count | 1 |

| validate_password_policy | LOW |

| validate_password_special_char_count | 1 |

+--------------------------------------+-------+

7 rows in set (0.01 sec)

或者,直接update 相应的字段,注意5.7的mysql数据库下的user表中已经没有Password字段

update mysql.user set authentication_string=password('ecloud') where user='root' and host='localhost';

最好使用 上面的alter 命令修改,而不是直接改存密码的字段。

其他2个修改密码的方法

格式:mysql> set password for 用户名@localhost = password('新密码');

格式:mysqladmin -u用户名 -p旧密码 password 新密码

5.6 默认没有安装validate_password插件 , user表字段名是password

密码失效问题解决

ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER

http://dev.mysql.com/doc/refman/5.7/en/password-expiration-policy.html

*****************************************************************************

默认只允许root账户 在本机localhost 访问,外部访问需要授权。

方式1

mysql> update user set host = '%' where user = 'root';

方式2

mysql> GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

方式2会在user表重新生成一条记录,实际上是新增了一个用户。

3.Generic Binaries 二进制文件安装方式

https://dev.mysql.com/downloads/mysql/

操作系统类型 选 Linux - .Generic ,我下载的是x86_64bit ,名称为TAR 大小为652M

https://dev.mysql.com/doc/mysql-linuxunix-excerpt/5.7/en/binary-installation.html

官方二进制安装文档

先决条件2个

删除本地自带的mysql lib

yum remove mysql-libs

删除本机/etc/my.cnf

rm /etc/my.cnf

新建用户和组 ,参数指定mysql用户不可登陆

groupadd mysql

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

在 /usr/local 目录中解压缩二进制包,生成目录mysql-5.7.18-linux-glibc2.5-x86_64

tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.gz

创建链接指向 mysql 目录

ln -s /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64 mysql

新增路径

export PATH=$PATH:/usr/local/mysql/bin --当前有效

以下永久生效

echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile

source /etc/profile

创建mysql-files , 为了安全,指定导入导出必须在这个目录(参数配置中设置)

cd mysql

mkdir mysql-files

chmod 750 mysql-files

设置mysql目录权限给mysql用户和组

chown -R mysql .

chown -R mysql .

初始化mysql

bin/mysqld --initialize --user=mysql

[root@svr128 mysql]# bin/mysqld --initialize --user=mysql

2017-06-01T07:21:30.591161Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-06-01T07:21:34.120998Z 0 [Warning] InnoDB: New log files created, LSN=45790

2017-06-01T07:21:34.628589Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2017-06-01T07:21:34.738879Z 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: f16c0fd5-469a-11e7-90e0-000c291013e8.

2017-06-01T07:21:34.752044Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2017-06-01T07:21:34.775411Z 1 [Note] A temporary password is generated for root@localhost: Pbs1c*i8/ejh

最后一行有临时密码

执行bin/mysql_ssl_rsa_setup ,应该是安全相关rsa加密的设置。

把除data和mysql-files 目录外的其他目录权限还给root

chown -R root .

chown -R mysql data mysql-files

设置自启动服务

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig --list mysqld

经测试,5.7.18不需要默认的my.cnf 即可启动,

测试安装后 ,根目录find -iname my.cnf 没有返回结果.

附选择指定版本下载

下载所有版本:

http://downloads.mysql.com/archives/community/

进行操作: Downloads-->Archives-->选择版本