下載yum源
官網地址:http://dev.mysql.com/downloads/repo/yum/
centos7系統:
http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@localhost ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm --2019-08-10 21:38:37-- https://dev.mysql.com/get/mysql57-community-release-el7-11.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|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm [following] --2019-08-10 21:38:59-- https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm Resolving repo.mysql.com (repo.mysql.com)... 104.74.21.42 Connecting to repo.mysql.com (repo.mysql.com)|104.74.21.42|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 25680 (25K) [application/x-redhat-package-manager] Saving to: ‘mysql57-community-release-el7-11.noarch.rpm’ 100%[===================================================================================================================>] 25,680 --.-K/s in 0.1s 2019-08-10 21:39:00 (253 KB/s) - ‘mysql57-community-release-el7-11.noarch.rpm’ saved [25680/25680] [root@localhost ~]# ls anaconda-ks.cfg mysql57-community-release-el7-11.noarch.rpm
檢測系統是否自帶安裝mysql
# yum list installed | grep mysql
刪除系統自帶的mysql
# yum -y remove mysql-libs.x86_64
這個mysql的yum源服務器在國外,所以下載速度會比較慢,mysql5.6只有79M大,而mysql5.7就有182M。
安裝mysql57-community-release-el7-11.noarch.rpm包
# yum -y install mysql57-community-release-el7-11.noarch.rpm
[root@localhost ~]# yum -y install mysql57-community-release-el7-11.noarch.rpm Loaded plugins: fastestmirror Examining mysql57-community-release-el7-11.noarch.rpm: mysql57-community-release-el7-11.noarch Marking mysql57-community-release-el7-11.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql57-community-release.noarch 0:el7-11 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================= Package Arch Version Repository Size ============================================================================================================================================================= Installing: mysql57-community-release noarch el7-11 /mysql57-community-release-el7-11.noarch 31 k Transaction Summary ============================================================================================================================================================= Install 1 Package Total size: 31 k Installed size: 31 k Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql57-community-release-el7-11.noarch 1/1 Verifying : mysql57-community-release-el7-11.noarch 1/1 Installed: mysql57-community-release.noarch 0:el7-11 Complete!
安裝完這個包后,會獲得兩個mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo
[root@localhost ~]# ls /etc/yum.repos.d/ centos.repo mysql-community.repo mysql-community-source.repo
# cat /etc/yum.repos.d/mysql-community.repo
[root@localhost ~]# cat /etc/yum.repos.d/mysql-community.repo [mysql-connectors-community] name=MySQL Connectors Community baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-community] name=MySQL Tools Community baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.5 [mysql55-community] name=MySQL 5.5 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-preview] name=MySQL Tools Preview baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-7.5-community] name=MySQL Cluster 7.5 Community baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-7.6-community] name=MySQL Cluster 7.6 Community baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# cat /etc/yum.repos.d/mysql-community-source.repo
[root@localhost ~]# cat /etc/yum.repos.d/mysql-community-source.repo [mysql-connectors-community-source] name=MySQL Connectors Community - Source baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-community-source] name=MySQL Tools Community - Source baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql55-community-source] name=MySQL 5.5 Community Server - Source baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql56-community-source] name=MySQL 5.6 Community Server - Source baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql57-community-source] name=MySQL 5.7 Community Server - Source baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql80-community-source] name=MySQL 8.0 Community Server - Source baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-preview-source] name=MySQL Tools Preview - Source baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-7.5-community-source] name=MySQL Cluster 7.5 Community - Source baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-cluster-7.6-community-source] name=MySQL Cluster 7.6 Community - Source baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/SRPMS enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
用yum repolist mysql這個命令查看一下是否已經有mysql可安裝文件
# yum repolist all | grep mysql
[root@localhost ~]# yum repolist all | grep mysql mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 118 mysql-connectors-community-source MySQL Connectors Community - disabled mysql-tools-community/x86_64 MySQL Tools Community enabled: 95 mysql-tools-community-source MySQL Tools Community - Sourc disabled mysql-tools-preview/x86_64 MySQL Tools Preview disabled mysql-tools-preview-source MySQL Tools Preview - Source disabled mysql55-community/x86_64 MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - disabled mysql56-community/x86_64 MySQL 5.6 Community Server disabled mysql56-community-source MySQL 5.6 Community Server - disabled mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 364 mysql57-community-source MySQL 5.7 Community Server - disabled mysql80-community/x86_64 MySQL 8.0 Community Server disabled mysql80-community-source MySQL 8.0 Community Server - disabled
安裝mysql
# yum install mysql-server -y
比較慢,耐心等待
Installed: mysql-community-libs.x86_64 0:5.7.27-1.el7 mysql-community-libs-compat.x86_64 0:5.7.27-1.el7 mysql-community-server.x86_64 0:5.7.27-1.el7 Dependency Installed: mysql-community-client.x86_64 0:5.7.27-1.el7 mysql-community-common.x86_64 0:5.7.27-1.el7 net-tools.x86_64 0:2.0-0.24.20131004git.el7 Dependency Updated: openssl.x86_64 1:1.0.2k-16.el7_6.1 openssl-libs.x86_64 1:1.0.2k-16.el7_6.1 postfix.x86_64 2:2.10.1-7.el7 Replaced: mariadb-libs.x86_64 1:5.5.44-2.el7.centos Complete!
安裝mysql指定版本,例如安裝mysql5.7.22版
# yum install mysql-community-server-5.7.22 -y
下載rpm安裝包到/opt/pages
# yum install --downloadonly --downloaddir=/opt/pages mysql-community-server-5.7.22
加入開機自啟動
# systemctl enable mysqld
啟動mysql服務進程
# systemctl start mysqld
查看mysql服務進程
# systemctl status mysqld
[root@localhost ~]# systemctl start mysqld [root@localhost ~]# [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 Sat 2019-08-10 22:15:24 CST; 7s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 11810 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 11736 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 11813 (mysqld) CGroup: /system.slice/mysqld.service └─11813 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Aug 10 22:15:17 localhost.localdomain systemd[1]: Starting MySQL Server... Aug 10 22:15:24 localhost.localdomain systemd[1]: Started MySQL Server.
進入mysql
# mysql
[root@localhost ~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
MySQL服務器初始化(從MySQL 5.7開始):在服務器初始啟動時,如果服務器的數據目錄為空,則會發生以下情況:
(1)、服務器已初始化
(2)、在數據目錄中生成SSL證書和密鑰文件
(3)、該validate_password插件安裝並啟用
(4)、將'root'@'localhost'
創建一個超級用戶帳戶。設置超級用戶的密碼並將其存儲在日志文件中。要顯示它,請使用以下命令:
# grep "password" /var/log/mysqld.log
[root@localhost ~]# grep "password" /var/log/mysqld.log 2019-08-10T14:15:20.529782Z 1 [Note] A temporary password is generated for root@localhost: #&kdOvHin6gP 2019-08-10T14:16:10.145451Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
使用用戶名密碼進入mysql
[root@localhost ~]# mysql -uroot -p Enter password: #輸入初始密碼 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.27 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數據庫,此時不能做任何事情,因為MySQL默認必須修改密碼之后才能操作數據庫
mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement
修改mysql密碼
注意:
MySQL的 validate_password 插件默認安裝。這將要求密碼包含至少一個大寫字母,一個小寫字母,一個數字和一個特殊字符,並且密碼總長度至少為8個字符。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> SET PASSWORD = PASSWORD('Admin123!');
mysql> SET PASSWORD = PASSWORD('Admin123!'); Query OK, 0 rows affected, 1 warning (0.00 sec)
MySQL完整的初始密碼規則可以通過如下命令查看:
mysql> SHOW VARIABLES LIKE 'validate_password%';
mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | 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 | +--------------------------------------+--------+ 7 rows in set (0.01 sec)
密碼的長度是由validate_password_length決定的,而validate_password_length的計算公式是:
validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)
如果只想設置簡單密碼需要修改兩個全局參數:
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
mysql> set global validate_password_policy=0; Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_length=1; Query OK, 0 rows affected (0.00 sec)
再次查看mysql密碼規則
mysql> SHOW VARIABLES LIKE '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.00 sec)
此時密碼就可以設置的很簡單,例如1234之類的
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec)
設置遠程可以登錄
授權遠程登錄
grant all privileges on *.* to root@'%' identified by 'password' with grant option;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'password' WITH GRANT OPTION;
第一個是你的用戶名root,第二個password是遠程的密碼
mysql> grant all privileges on *.* to root@'%' identified by '123456' with grant option
flush命令使配置立即生效
mysql> flush privileges;
mysql> select user,host from mysql.user; +---------------+-----------+ | user | host | +---------------+-----------+ | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+-----------+ 3 rows in set (0.00 sec) mysql> grant all privileges on *.* to root@'%' identified by '123456' with grant option; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +---------------+-----------+ | user | host | +---------------+-----------+ | root | % | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+-----------+ 4 rows in set (0.00 sec)
或者使用一下語句修改授權遠程客戶端登錄
use mysql;
show tables;
select Host,User from user \G
update user set host = '%' where Host = "localhost" and user = "root";
修改一些簡單的配置:
默認的字符集是拉丁字符集,每次創建數據庫的時候要設置字符集;默認還不支持group by語句,默認的時區也不是我們現在的北京時間(東八區),會導致我們的時間差了13個點。
打開配置文件,yum安裝的默認在/etc文件夾下:/etc/my.cnf
# vi /etc/my.cnf
默認配置如下:
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
在[mysqld]下面添加,不需要分號
字符集:注意是utf8而不是utf-8!
character-set-server=utf8
查看mysql字符集
mysql> show variables like 'char%';
默認字符集如下
mysql> show variables like 'char%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
設置時區為東八區
default-time_zone = '+8:00'
sql支持group by語句
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
附yum安裝源:
centos7_mysql5.7.repo
[mysql-connectors-community] name=MySQL Connectors Community baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/x86_64/ enabled=1 gpgcheck=0 [mysql-tools-community] name=MySQL Tools Community baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/x86_64/ enabled=1 gpgcheck=0 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/ enabled=1 gpgcheck=0
參考博客:
centos7.4安裝MySQL5.7
https://blog.csdn.net/pzl_pzl/article/details/82015144
centos7下使用yum源安裝mysql5.7記錄
https://www.jianshu.com/p/531cc35b15e7