centos7 下mysql的安装

来源:互联网 发布:英国脱欧的影响知乎 编辑:程序博客网 时间:2024/05/22 06:46

前记:

由于centos7开始,默认的数据库软件已经变为了mariadb,并且移除了mysql安装源,


1、清理系统自带的mariadb

rpm -qa | grep mariadb
[lcr@localhost ~]$ rpm -qa | grep mariadb

mariadb-5.5.52-1.el7.x86_64
mariadb-libs-5.5.52-1.el7.x86_64
mariadb-devel-5.5.52-1.el7.x86_64
mariadb-server-5.5.52-1.el7.x86_64
[root@localhost lcr]# yum remove mariadb-libs.x86_64


2、下载官方源

wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

##官方源可能速度太慢了,更换中科大镜像源
wget http://mirrors.ustc.edu.cn/mysql-repo/mysql57-community-release-el7-11.noarch.rpm


3、安装官方源

yum localinstall mysql57-community-release-el7-11.noarch.rpm

[root@localhost ~]# yum localinstall mysql57-community-release-el7-11.noarch.rpm

or
##[root@centOS lcr]# yum localinstall mysql57-community-release-el7-11.noarch.rpm


4、查看源安装状态

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

[root@localhost ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64    MySQL Connectors Community 42
mysql-tools-community/x86_64    MySQL Tools Community 51
mysql57-community/x86_64    MySQL 5.7 Community Server 207


5、选择要安装的MySQL版本

vim /etc/yum.repos.d/mysql-community.repo

[root@localhost ~]# vim /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/

选择要安装的community版本,将:enabled=0,改为enabled=1,其余改为enabled=0


6、安装MySQL

安装:

yum install mysql-community-server

[root@localhost ~]# yum install mysql-community-server

查看安装情况

rpm -qa |grep mysql

[root@centOS ~]# rpm -qa |grep mysql
mysql-community-client-5.7.19-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-server-5.7.19-1.el7.x86_64
mysql-community-common-5.7.19-1.el7.x86_64
mysql-community-libs-5.7.19-1.el7.x86_64


7、启动并设置开机启动

启动mysql

systemctl start mysqld

[root@localhost ~]# systemctl start mysqld


查看mysql启动情况

systemctl status mysqld

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2017-08-31 14:31:18 CST; 29s ago
Process: 14957 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 14936 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 14955 (mysqld_safe)
CGroup: /system.slice/mysqld.service
├─14955 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─15121 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir...
8月 31 14:31:15 localhost.localdomain systemd[1]: Starting MySQL Community Server...
8月 31 14:31:16 localhost.localdomain mysqld_safe[14955]: 170831 14:31:16 mysqld_safe ....
8月 31 14:31:16 localhost.localdomain mysqld_safe[14955]: 170831 14:31:16 mysqld_safe ...l
8月 31 14:31:18 localhost.localdomain systemd[1]: Started MySQL Community Server.
Hint: Some lines were ellipsized, use -l to show in full.


设置开机启动

[root@centOS ~]# systemctl enable mysqld
[root@centOS ~]# systemctl daemon-reload


8、找到mysql配置的初始化密码并修改密码:

查找初始化密码

nano /var/log/mysqld.log

[root@centOS ~]# nano /var/log/mysqld.log
2017-08-31T11:00:12.326998Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-08-31T11:00:14.118974Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-08-31T11:00:14.210958Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-08-31T11:00:14.270194Z 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: 90dced13-8e3b-11e7-a3f5-000c2$
2017-08-31T11:00:14.271751Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-31T11:00:14.272562Z 1 [Note] A temporary password is generated for root@localhost:kl<T=kws0;3x
红色字体部分即为分配的初始化密码


修改初始化密码

mysql -uroot -p

[root@centOS log]# mysql -uroot -p
Enter password:                                         <------输入上面的初始化密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.19
Copyright (c) 2000, 2017, 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> set password for 'root'@'localhost'=password('775120Zhxxxxxx');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
(5.7版本开始的mysql,增加了密码强度检测模块,要求密码包含数字,大小写字母,字符)
mysql> set password for 'root'@'localhost'=password('775120@Zhxxxxxxx');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye



9、配置mysql

mysql_secure_installation

[root@centOS ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :              <--------是否修改密码
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y             <------------是否删除匿名账户
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y             <----------禁止远程登录
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y             <----------删除测试数据
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y             <--------重新加载权限表
Success.
All done!




至此,centos7下,mysql安装完毕