CentOS7安装mysql
[root@bd005 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm --2018-11-08 18:43:55-- http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11 Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm [following] --2018-11-08 18:43:56-- https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm [following] --2018-11-08 18:43:57-- https://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm Resolving repo.mysql.com (repo.mysql.com)... 23.50.35.125 Connecting to repo.mysql.com (repo.mysql.com)|23.50.35.125|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 9116 (8.9K) [application/x-redhat-package-manager] Saving to: ‘mysql57-community-release-el7-8.noarch.rpm’ 100%[====================================================================================================================================================================================================================================>] 9,116 --.-K/s in 0s 2018-11-08 18:43:57 (130 MB/s) - ‘mysql57-community-release-el7-8.noarch.rpm’ saved [9116/9116] [root@bd005 ~]# ll total 20 -rw-------. 1 root root 1276 Aug 18 2017 anaconda-ks.cfg -rw-r--r--. 1 root root 837 Dec 7 2017 initialize_server.sh -rw-r--r-- 1 root root 9116 Apr 11 2016 mysql57-community-release-el7-8.noarch.rpm [root@bd005 ~]# mv mysql57-community-release-el7-8.noarch.rpm /usr/local/softs/ [root@bd005 ~]# cd /usr/local/softs/ [root@bd005 softs]# ll total 9416 -rw-r--r-- 1 root root 9625824 Nov 6 17:37 apache-tomcat-8.5.34.tar.gz -rw-r--r-- 1 root root 9116 Apr 11 2016 mysql57-community-release-el7-8.noarch.rpm drwxr-xr-x 9 root root 220 Nov 8 17:34 tomcat8534 [root@bd005 softs]# mkdir rpms [root@bd005 softs]# mv mysql57-community-release-el7-8.noarch.rpm rpms/ [root@bd005 softs]# ll total 9404 -rw-r--r-- 1 root root 9625824 Nov 6 17:37 apache-tomcat-8.5.34.tar.gz drwxr-xr-x 2 root root 56 Nov 8 18:44 rpms drwxr-xr-x 9 root root 220 Nov 8 17:34 tomcat8534 [root@bd005 softs]# yum localinstall rpms/mysql57-community-release-el7-8.noarch.rpm Loaded plugins: fastestmirror Examining rpms/mysql57-community-release-el7-8.noarch.rpm: mysql57-community-release-el7-8.noarch Marking rpms/mysql57-community-release-el7-8.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql57-community-release.noarch 0:el7-8 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================================================================================================================================== Installing: mysql57-community-release noarch el7-8 /mysql57-community-release-el7-8.noarch 8.2 k Transaction Summary ============================================================================================================================================================================================================================================================================== Install 1 Package Total size: 8.2 k Installed size: 8.2 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql57-community-release-el7-8.noarch 1/1 Verifying : mysql57-community-release-el7-8.noarch 1/1 Installed: mysql57-community-release.noarch 0:el7-8 Complete! [root@bd005 softs]# yum install mysql-community-server Loaded plugins: fastestmirror mysql-connectors-community | 2.5 kB 00:00:00 mysql-tools-community | 2.5 kB 00:00:00 mysql57-community | 2.5 kB 00:00:00 (1/3): mysql-connectors-community/x86_64/primary_db | 29 kB 00:00:00 (2/3): mysql-tools-community/x86_64/primary_db | 48 kB 00:00:00 (3/3): mysql57-community/x86_64/primary_db | 162 kB 00:00:01 Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.com Resolving Dependencies --> Running transaction check ---> Package mysql-community-server.x86_64 0:5.7.24-1.el7 will be installed --> Processing Dependency: mysql-community-common(x86-64) = 5.7.24-1.el7 for package: mysql-community-server-5.7.24-1.el7.x86_64 --> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.24-1.el7.x86_64 --> Processing Dependency: net-tools for package: mysql-community-server-5.7.24-1.el7.x86_64 --> Running transaction check ---> Package mysql-community-client.x86_64 0:5.7.24-1.el7 will be installed --> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.24-1.el7.x86_64 ---> Package mysql-community-common.x86_64 0:5.7.24-1.el7 will be installed ---> Package net-tools.x86_64 0:2.0-0.22.20131004git.el7 will be installed --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be obsoleted --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 --> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 ---> Package mysql-community-libs.x86_64 0:5.7.24-1.el7 will be obsoleting --> Running transaction check ---> Package mysql-community-libs-compat.x86_64 0:5.7.24-1.el7 will be obsoleting --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================================================================================================================================== Installing: mysql-community-libs x86_64 5.7.24-1.el7 mysql57-community 2.2 M replacing mariadb-libs.x86_64 1:5.5.56-2.el7 mysql-community-libs-compat x86_64 5.7.24-1.el7 mysql57-community 2.0 M replacing mariadb-libs.x86_64 1:5.5.56-2.el7 mysql-community-server x86_64 5.7.24-1.el7 mysql57-community 165 M Installing for dependencies: mysql-community-client x86_64 5.7.24-1.el7 mysql57-community 24 M mysql-community-common x86_64 5.7.24-1.el7 mysql57-community 274 k net-tools x86_64 2.0-0.22.20131004git.el7 base 305 k Transaction Summary ============================================================================================================================================================================================================================================================================== Install 3 Packages (+3 Dependent packages) Total download size: 194 M Is this ok [y/d/N]: y Downloading packages: warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.24-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY ] 362 kB/s | 280 kB 00:09:08 ETA Public key for mysql-community-common-5.7.24-1.el7.x86_64.rpm is not installed (1/6): mysql-community-common-5.7.24-1.el7.x86_64.rpm | 274 kB 00:00:01 (2/6): mysql-community-libs-5.7.24-1.el7.x86_64.rpm | 2.2 MB 00:00:02 (3/6): mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm | 2.0 MB 00:00:02 (4/6): net-tools-2.0-0.22.20131004git.el7.x86_64.rpm | 305 kB 00:00:01 (5/6): mysql-community-server-5.7.24-1.el7.x86_64.rpm | 165 MB 00:01:57 (6/6): mysql-community-client-5.7.24-1.el7.x86_64.rpm | 24 MB 00:04:28 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Total 739 kB/s | 194 MB 00:04:28 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql Importing GPG key 0x5072E1F5: Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>" Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5 Package : mysql57-community-release-el7-8.noarch (installed) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql Is this ok [y/N]: y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql-community-common-5.7.24-1.el7.x86_64 1/7 Installing : mysql-community-libs-5.7.24-1.el7.x86_64 2/7 Installing : mysql-community-client-5.7.24-1.el7.x86_64 3/7 Installing : net-tools-2.0-0.22.20131004git.el7.x86_64 4/7 Installing : mysql-community-server-5.7.24-1.el7.x86_64 5/7 Installing : mysql-community-libs-compat-5.7.24-1.el7.x86_64 6/7 Erasing : 1:mariadb-libs-5.5.56-2.el7.x86_64 7/7 Verifying : net-tools-2.0-0.22.20131004git.el7.x86_64 1/7 Verifying : mysql-community-libs-compat-5.7.24-1.el7.x86_64 2/7 Verifying : mysql-community-libs-5.7.24-1.el7.x86_64 3/7 Verifying : mysql-community-client-5.7.24-1.el7.x86_64 4/7 Verifying : mysql-community-server-5.7.24-1.el7.x86_64 5/7 Verifying : mysql-community-common-5.7.24-1.el7.x86_64 6/7 Verifying : 1:mariadb-libs-5.5.56-2.el7.x86_64 7/7 Installed: mysql-community-libs.x86_64 0:5.7.24-1.el7 mysql-community-libs-compat.x86_64 0:5.7.24-1.el7 mysql-community-server.x86_64 0:5.7.24-1.el7 Dependency Installed: mysql-community-client.x86_64 0:5.7.24-1.el7 mysql-community-common.x86_64 0:5.7.24-1.el7 net-tools.x86_64 0:2.0-0.22.20131004git.el7 Replaced: mariadb-libs.x86_64 1:5.5.56-2.el7 Complete! [root@bd005 softs]# systemctl start mysqld [root@bd005 softs]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2018-11-08 18:58:01 CST; 6s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 1276 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 1203 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 1280 (mysqld) CGroup: /system.slice/mysqld.service └─1280 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Nov 08 18:57:57 bd005 systemd[1]: Starting MySQL Server... Nov 08 18:58:01 bd005 systemd[1]: Started MySQL Server. [root@bd005 softs]# grep 'temporary password' /var/log/mysqld.log //生成临时密码 2018-11-08T10:57:58.087434Z 1 [Note] A temporary password is generated for root@localhost: /GUeg#cGo3MU [root@bd005 softs]# mysql -uroot -p/GUeg#cGo3MU [root@bd005 softs]# mysql> mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '/GUeg#cGo3MU'; Query OK, 0 rows affected (0.00 sec) mysql> set password for 'root'@'localhost'=password('/GUeg#cGo3MU'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all on *.* to 'root'@'%' identified by '/GUeg#cGo3MU'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> quit; Bye [root@bd005 softs]# mysql -uroot -p/GUeg#cGo3MU mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ rows in set (0.00 sec)
## 设置简单的密码
查看当前密码策略
mysql> SHOW VARIABLES LIKE 'validate_password%';
修改mysql配置参数
mysql> set global validate_password_policy=0;
mysql> set global validate_password_mixed_case_count=0;
mysql> set global validate_password_number_count=3;
mysql> set global validate_password_special_char_count=0;
mysql> set global validate_password_length=3;
验证修改后的密码策略
mysql> SHOW VARIABLES LIKE 'validate_password%';
修改简单密码
mysql> use mysql
mysql> SET PASSWORD = PASSWORD('123456');
mysql> flush privileges;
重启mysql
[root@bd005 softs]# systemctl mysqld restart
## 远程登录设置
授权允许root从任何地址登录设置
mysql> set global validate_password_policy=0;
mysql> set global validate_password_mixed_case_count=0;
mysql> set global validate_password_number_count=3;
mysql> set global validate_password_special_char_count=0;
mysql> set global validate_password_length=3;
mysql> SHOW VARIABLES LIKE 'validate_password%';
授权允许root从任何地址登录
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
mysql> flush privileges;
MySQL的一些函数使用小例子:
1、使用随机数函数 RAND
生成0-100的随机整数:SELECT FLOOR((RAND() * 100))
把用户id为0的错误数据使用随机函数改成随机整数ID:UPDATE tbl_logs SET global_user_id=FLOOR(2 + (RAND() * 100)) WHERE global_user_id=0;
2、使用替换函数 REPLACE
将My@SQ@L中的@替换为空字符串:SELECT REPLACE('My@SQ@L', '@', '')
把cookie_text字段中的My@SQ@L修改为MySQL:UPDATE tbl_logs SET cookie_text = REPLACE('My@SQ@L', '@', '') WHERE user_id=0;
3、使用拼接函数 CONCAT和CONCAT_WS
CONCAT函数:
合并3个字符串为一个字符串:SELECT CONCAT('My','S','QL');
CONCAT_WS函数(CONCAT With Separator,带有分隔符的CONCAT函数):
将name,gender,age拼接为name_gender_age:SELECT CONCAT_WS('_',name,gender,age);
4、大小写转换LOWER和UPPER
小写转换-将MySQL转换为mysql:SELECT LOWER('MySQL')
大写转换-将mysql转换为MYSQL:SELECT UPPER('mysql')
5、获取字符串左右指定长度的字符
获取右边4个字符串(YHQ20190720V900N0013中的0013):SELECT RIGHT('YHQ20190720V900N0013',4)
获取左边3个字符串(YHQ20190720V900N0013中的YHQ):SELECT LEFT('YHQ20190720V900N0013',3)
6、去除字符串首尾空格
将' BMW '修改为'BMW':SELECT TRIM(' BMW ')
7、时间和时间戳相互转换
将Date类型的当前时间换换位时间戳:SELECT UNIX_TIMESTAMP(NOW())
将时间戳转换为Date类型:SELECT FROM_UNIXTIME('1565962224')
生成随机指定时间范围内(近1个月)的时间:
SELECT ADDDATE(MAKEDATE(2019, FLOOR(200 + RAND() * 30)), -- 1-30号
INTERVAL MAKETIME(
FLOOR(RAND()*24), -- 0-23点
FLOOR(RAND()*60), -- 0-59分
FLOOR(RAND()*60) -- 0-59秒
) HOUR_SECOND) AS rand_dt;
8、MySQL表的自增ID批量修改
将已有数据的ID修改为从1开始自增:
将表的自增ID设置从1开始:ALTER TABLE tbl_users AUTO_INCREMENT=1
修改表数据的ID:UPDATE tbl_users t SET t.id = (SELECT @n := @n + 1 FROM (SELECT @n := 0) r)
9、MySQL表的时间字段修改为近一个月内的时间
UPDATE tbl_logs_201608 SET log_time = ADDDATE(MAKEDATE(2016, FLOOR(200 + RAND() * 30)), INTERVAL MAKETIME(FLOOR(RAND()*24),FLOOR(RAND()*60),FLOOR(RAND()*60)) HOUR_SECOND)
10、MySQL的case when field=value then value1 else value2 end
SELECT
CASE FLOOR(1+RAND()*20)
WHEN 1 THEN '银色'
WHEN 2 THEN '香槟金色'
WHEN 3 THEN '黑色'
WHEN 4 THEN '白色'
WHEN 5 THEN '梦境极光【卡其金】'
WHEN 6 THEN '梦境极光【布朗灰】'
WHEN 7 THEN '粉色'
WHEN 8 THEN '金属灰'
WHEN 9 THEN '金色'
WHEN 10 THEN '乐享金'
WHEN 11 THEN '布鲁钢'
WHEN 12 THEN '月光银'
WHEN 13 THEN '时尚光谱【浅金棕】'
WHEN 14 THEN '香槟色'
WHEN 15 THEN '香槟金'
WHEN 16 THEN '灰色'
WHEN 17 THEN '樱花粉'
WHEN 18 THEN '蓝色'
WHEN 19 THEN '金属银'
WHEN 20 THEN '玫瑰金'
ELSE '其他' END
AS color