CentOS7 安装 MySQL 和简单优化
[0x0]环境及其软件介绍
- 操作系统:CentOS7.0 纯净版
- MySQL 版本:5.7.16
[0x1]关闭SELinux
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux 历史上最杰出的新安全子系统。在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上。虽然 SELinux 很好用,但是在多数情况我们还是将其关闭,因为在不了解其机制的情况下使用 SELinux 会导致软件安装或者应用部署失败。
一般如果使用云服务器,已经默认关闭了该服务,如果是虚拟机自己安装 CentOS 的话,那么需要自己手动关闭。如果不关闭,则影响后面的操作。
- 查看 selinux 的模式
[root@virtual data]# getenforce Enforcing [root@virtual data]#
- 关闭 selinux
- 永久性关闭
永久性关闭则需要重启服务器后生效
# vim /etc/selinux/config #编辑这个文件,下面修改的内容 #============================================================ # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. #SELINUX=enforcing #这个是系统默认的 SELINUX=disabled #这是自己修改的,表示禁用该服务 # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted# vim /etc/selinux/config #编辑这个文件,下面修改的内容 #============================================================ # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. #SELINUX=enforcing #这个是系统默认的 SELINUX=disabled #这是自己修改的,表示禁用该服务 # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
保存文件,然后重启服务器吧。- 临时性关闭
临时性关闭仅对当前有效且立即生效,重启服务器之后无效。
setenforce 0
命令表示设置 selinux 为 permissive 模式(即关闭);
setenforce 1
命令表示设置 selinux 为 enforcing 模式(即开启)
- 永久性关闭
[0x2]下载 MySQL
MySQL 的官网下载地址是http://dev.mysql.com/downloads/mysql/,选择Red Hat Enterprise Linux / Oracle Linux
(因为 CentOS 属于 RHEL 的一个分支),如下图所示
然后下面选择 CentOS7 的全家桶,选择全家桶比较方便,当安装的时候如果提示缺少包,一般可以在全家桶里直接找到的。
老套路,可以直接到微云下载(密码:yv8Fib)。
将下载成功mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
上传到您的服务器,下面就准备开始安装了。
[0x3]安装 MySQL
首先将上传的 MySQLmysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
解压出来。
[root@virtual mysql]# ls mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar [root@virtual mysql]# tar xvf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm mysql-community-devel-5.7.16-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.16-1.el7.x86_64.rpm mysql-community-libs-5.7.16-1.el7.x86_64.rpm mysql-community-common-5.7.16-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.16-1.el7.x86_64.rpm mysql-community-test-5.7.16-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.16-1.el7.x86_64.rpm mysql-community-server-minimal-5.7.16-1.el7.x86_64.rpm mysql-community-server-5.7.16-1.el7.x86_64.rpm mysql-community-client-5.7.16-1.el7.x86_64.rpm mysql-community-embedded-5.7.16-1.el7.x86_64.rpm [root@virtual mysql]#
安装 MySQL 数据库的服务器版本,即mysql-community-server-5.7.16-1.el7.x86_64.rpm
,安装命令这里使用yum localinstall
,该命令会自动分析软件依赖包,非常的方便。
[root@virtual mysql]# yum localinstall mysql-community-server- mysql-community-server-5.7.16-1.el7.x86_64.rpm mysql-community-server-minimal-5.7.16-1.el7.x86_64.rpm [root@virtual mysql]# yum localinstall mysql-community-server-5.7.16-1.el7.x86_64.rpm Loaded plugins: fastestmirror Examining mysql-community-server-5.7.16-1.el7.x86_64.rpm: mysql-community-server-5.7.16-1.el7.x86_64 Marking mysql-community-server-5.7.16-1.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql-community-server.x86_64 0:5.7.16-1.el7 will be installed --> Processing Dependency: mysql-community-common(x86-64) = 5.7.16-1.el7 for package: mysql-community-server-5.7.16-1.el7.x86_64 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.yun-idc.com --> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.16-1.el7.x86_64 --> Processing Dependency: net-tools for package: mysql-community-server-5.7.16-1.el7.x86_64 --> Running transaction check ---> Package mysql-community-server.x86_64 0:5.7.16-1.el7 will be installed --> Processing Dependency: mysql-community-common(x86-64) = 5.7.16-1.el7 for package: mysql-community-server-5.7.16-1.el7.x86_64 --> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.16-1.el7.x86_64 ---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed --> Finished Dependency Resolution Error: Package: mysql-community-server-5.7.16-1.el7.x86_64 (/mysql-community-server-5.7.16-1.el7.x86_64) Requires: mysql-community-common(x86-64) = 5.7.16-1.el7 Error: Package: mysql-community-server-5.7.16-1.el7.x86_64 (/mysql-community-server-5.7.16-1.el7.x86_64) Requires: mysql-community-client(x86-64) >= 5.7.9 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest [root@virtual mysql]#
根据上面的错误信息可以知道,安装 MySQL 服务器mysql-community-server-5.7.16-1.el7.x86_64.rpm
需要先安装mysql-community-client(x86-64)
软件,这个时候体现出全家桶的好处,发现全家桶里面带有这个软件,所以直接先安装这个软件即可。
[root@virtual mysql]# yum localinstall mysql-community-client-5.7.16-1.el7.x86_64.rpm Loaded plugins: fastestmirror Examining mysql-community-client-5.7.16-1.el7.x86_64.rpm: mysql-community-client-5.7.16-1.el7.x86_64 Marking mysql-community-client-5.7.16-1.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql-community-client.x86_64 0:5.7.16-1.el7 will be installed --> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.16-1.el7.x86_64 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.yun-idc.com --> Finished Dependency Resolution Error: Package: mysql-community-client-5.7.16-1.el7.x86_64 (/mysql-community-client-5.7.16-1.el7.x86_64) Requires: mysql-community-libs(x86-64) >= 5.7.9 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest [root@virtual mysql]#
套路出来了,根据错误信息,很显然先安装mysql-community-libs(x86-64)
,走起:
[root@virtual mysql]# yum localinstall mysql-community-libs- mysql-community-libs-5.7.16-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm [root@virtual mysql]# yum localinstall mysql-community-libs-5.7.16-1.el7.x86_64.rpm Loaded plugins: fastestmirror Examining mysql-community-libs-5.7.16-1.el7.x86_64.rpm: mysql-community-libs-5.7.16-1.el7.x86_64 Marking mysql-community-libs-5.7.16-1.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.44-2.el7.centos will be obsoleted --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.yun-idc.com --> 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.16-1.el7 will be obsoleting --> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64 --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.44-2.el7.centos will be updated ---> Package mariadb-libs.x86_64 1:5.5.50-1.el7_2 will be an update ---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting --> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64 Removing mariadb-libs.x86_64 1:5.5.50-1.el7_2 - u due to obsoletes from mysql-community-libs.x86_64 0:5.7.16-1.el7 - u --> Restarting Dependency Resolution with new changes. --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.50-1.el7_2 will be an update --> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 ---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting --> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64 --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.47-1.el7_2 will be an update ---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting --> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64 Removing mariadb-libs.x86_64 1:5.5.47-1.el7_2 - u due to obsoletes from mysql-community-libs.x86_64 0:5.7.16-1.el7 - u --> Restarting Dependency Resolution with new changes. --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.47-1.el7_2 will be an update --> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 ---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting --> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64 --> Finished Dependency Resolution Error: Package: mysql-community-libs-5.7.16-1.el7.x86_64 (/mysql-community-libs-5.7.16-1.el7.x86_64) Requires: mysql-community-common(x86-64) >= 5.7.9 Error: Package: 2:postfix-2.10.1-6.el7.x86_64 (@anaconda) Requires: libmysqlclient.so.18(libmysqlclient_18)(64bit) Removing: 1:mariadb-libs-5.5.44-2.el7.centos.x86_64 (@anaconda) libmysqlclient.so.18(libmysqlclient_18)(64bit) Obsoleted By: mysql-community-libs-5.7.16-1.el7.x86_64 (/mysql-community-libs-5.7.16-1.el7.x86_64) Not found Updated By: 1:mariadb-libs-5.5.50-1.el7_2.x86_64 (updates) libmysqlclient.so.18(libmysqlclient_18)(64bit) Available: 1:mariadb-libs-5.5.47-1.el7_2.x86_64 (updates) libmysqlclient.so.18(libmysqlclient_18)(64bit) Error: Package: 2:postfix-2.10.1-6.el7.x86_64 (@anaconda) Requires: libmysqlclient.so.18()(64bit) Removing: 1:mariadb-libs-5.5.44-2.el7.centos.x86_64 (@anaconda) libmysqlclient.so.18()(64bit) Obsoleted By: mysql-community-libs-5.7.16-1.el7.x86_64 (/mysql-community-libs-5.7.16-1.el7.x86_64) ~libmysqlclient.so.20()(64bit) Updated By: 1:mariadb-libs-5.5.50-1.el7_2.x86_64 (updates) libmysqlclient.so.18()(64bit) Available: 1:mariadb-libs-5.5.47-1.el7_2.x86_64 (updates) libmysqlclient.so.18()(64bit) You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest [root@virtual mysql]#
这次出现的错误不太一样,出现了两个错误,一个错误仍然提示缺少依赖包,mysql-community-common(x86-64)
,另外一个错误是与mariadb-libs-5.5.44-2.el7.centos.x86_64
发生冲突,所以需要卸载这个软件。
[root@virtual mysql]# yum remove mariadb-libs Loaded plugins: fastestmirror Resolving Dependencies --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.50-1.el7_2 will be erased --> 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 --> Running transaction check ---> Package postfix.x86_64 2:2.10.1-6.el7 will be erased --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================= Package Arch Version Repository Size ========================================================================================================================================================= Removing: mariadb-libs x86_64 1:5.5.50-1.el7_2 @updates 4.4 M Removing for dependencies: postfix x86_64 2:2.10.1-6.el7 @anaconda 12 M Transaction Summary ========================================================================================================================================================= Remove 1 Package (+1 Dependent package) Installed size: 17 M Is this ok [y/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : 2:postfix-2.10.1-6.el7.x86_64 1/2 Erasing : 1:mariadb-libs-5.5.50-1.el7_2.x86_64 2/2 Verifying : 1:mariadb-libs-5.5.50-1.el7_2.x86_64 1/2 Verifying : 2:postfix-2.10.1-6.el7.x86_64 2/2 Removed: mariadb-libs.x86_64 1:5.5.50-1.el7_2 Dependency Removed: postfix.x86_64 2:2.10.1-6.el7 Complete! [root@virtual mysql]#
卸载完成后解决最后一个依赖问题mysql-community-common-5.7.16-1.el7.x86_64.rpm
[root@virtual mysql]# yum localinstall mysql-community-common-5.7.16-1.el7.x86_64.rpm Loaded plugins: fastestmirror Examining mysql-community-common-5.7.16-1.el7.x86_64.rpm: mysql-community-common-5.7.16-1.el7.x86_64 Marking mysql-community-common-5.7.16-1.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql-community-common.x86_64 0:5.7.16-1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================= Package Arch Version Repository Size ========================================================================================================================================================= Installing: mysql-community-common x86_64 5.7.16-1.el7 /mysql-community-common-5.7.16-1.el7.x86_64 2.5 M Transaction Summary ========================================================================================================================================================= Install 1 Package Total size: 2.5 M Installed size: 2.5 M Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql-community-common-5.7.16-1.el7.x86_64 1/1 Verifying : mysql-community-common-5.7.16-1.el7.x86_64 1/1 Installed: mysql-community-common.x86_64 0:5.7.16-1.el7 Complete! [root@virtual mysql]#
接下来就是倒着安装相关依赖,首先安装mysql-community-libs-5.7.16-1.el7.x86_64.rpm
[root@virtual mysql]# yum localinstall mysql-community-libs-5.7.16-1.el7.x86_64.rpm Loaded plugins: fastestmirror Examining mysql-community-libs-5.7.16-1.el7.x86_64.rpm: mysql-community-libs-5.7.16-1.el7.x86_64 Marking mysql-community-libs-5.7.16-1.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================= Package Arch Version Repository Size ========================================================================================================================================================= Installing: mysql-community-libs x86_64 5.7.16-1.el7 /mysql-community-libs-5.7.16-1.el7.x86_64 9.4 M Transaction Summary ========================================================================================================================================================= Install 1 Package Total size: 9.4 M Installed size: 9.4 M Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql-community-libs-5.7.16-1.el7.x86_64 1/1 Verifying : mysql-community-libs-5.7.16-1.el7.x86_64 1/1 Installed: mysql-community-libs.x86_64 0:5.7.16-1.el7 Complete! [root@virtual mysql]#
继续安装mysql-community-client-5.7.16-1.el7.x86_64.rpm
[root@virtual mysql]# yum localinstall mysql-community-client-5.7.16-1.el7.x86_64.rpm Loaded plugins: fastestmirror Examining mysql-community-client-5.7.16-1.el7.x86_64.rpm: mysql-community-client-5.7.16-1.el7.x86_64 Marking mysql-community-client-5.7.16-1.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql-community-client.x86_64 0:5.7.16-1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================= Package Arch Version Repository Size ========================================================================================================================================================= Installing: mysql-community-client x86_64 5.7.16-1.el7 /mysql-community-client-5.7.16-1.el7.x86_64 106 M Transaction Summary ========================================================================================================================================================= Install 1 Package Total size: 106 M Installed size: 106 M Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql-community-client-5.7.16-1.el7.x86_64 1/1 Verifying : mysql-community-client-5.7.16-1.el7.x86_64 1/1 Installed: mysql-community-client.x86_64 0:5.7.16-1.el7 Complete! [root@virtual mysql]#
word 的天哪,终于到了安装mysql-community-server-5.7.16-1.el7.x86_64.rpm
的时候了。
[root@virtual mysql]# yum localinstall mysql-community-server-5.7.16-1.el7.x86_64.rpm Loaded plugins: fastestmirror Examining mysql-community-server-5.7.16-1.el7.x86_64.rpm: mysql-community-server-5.7.16-1.el7.x86_64 Marking mysql-community-server-5.7.16-1.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql-community-server.x86_64 0:5.7.16-1.el7 will be installed --> Processing Dependency: net-tools for package: mysql-community-server-5.7.16-1.el7.x86_64 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.yun-idc.com --> Running transaction check ---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================= Package Arch Version Repository Size ========================================================================================================================================================= Installing: mysql-community-server x86_64 5.7.16-1.el7 /mysql-community-server-5.7.16-1.el7.x86_64 691 M Installing for dependencies: net-tools x86_64 2.0-0.17.20131004git.el7 base 304 k Transaction Summary ========================================================================================================================================================= Install 1 Package (+1 Dependent package) Total size: 691 M Total download size: 304 k Installed size: 692 M Is this ok [y/d/N]: y Downloading packages: net-tools-2.0-0.17.20131004git.el7.x86_64.rpm | 304 kB 00:00:06 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : net-tools-2.0-0.17.20131004git.el7.x86_64 1/2 Installing : mysql-community-server-5.7.16-1.el7.x86_64 2/2 Verifying : mysql-community-server-5.7.16-1.el7.x86_64 1/2 Verifying : net-tools-2.0-0.17.20131004git.el7.x86_64 2/2 Installed: mysql-community-server.x86_64 0:5.7.16-1.el7 Dependency Installed: net-tools.x86_64 0:2.0-0.17.20131004git.el7 Complete! [root@virtual mysql]#
至此 MySQL 的服务器软件安装完毕,并且一并的安装了 MySQL 客户端软件。
[0x4]启动 MySQL
默认安装完成后可以检查下 MySQL 是否启动了。
[root@virtual mysql]# systemctl status mysqld.service ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: inactive (dead) [root@virtual mysql]#
Active: inactive (dead)
表示没启动,所以先启动 MySQL 服务。
[root@virtual mysql]# systemctl start mysqld [root@virtual mysql]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2016-11-23 21:36:10 CST; 31s ago Process: 12769 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 12752 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 12773 (mysqld) CGroup: /system.slice/mysqld.service └─12773 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Nov 23 21:36:09 virtual.m1 systemd[1]: Starting MySQL Server... Nov 23 21:36:10 virtual.m1 systemd[1]: Started MySQL Server. [root@virtual mysql]#
Active: active (running)
表示启动成功了。
[0x5]登录 MySQL
好像 MySQL 每个版本对于 root 的初始密码存放目录有变更,MySQL5.7 的 root 初始密码位置在/var/log/mysqld.log
,查看初始密码
[root@virtual mysql]# cat /var/log/mysqld.log | grep 'password' 2016-11-23T13:35:47.379026Z 1 [Note] A temporary password is generated for root@localhost: 8Elr*VgB7I?E 2016-11-23T13:36:08.112362Z 0 [Note] Shutting down plugin 'validate_password' 2016-11-23T13:36:09.489444Z 0 [Note] Shutting down plugin 'sha256_password' 2016-11-23T13:36:09.489450Z 0 [Note] Shutting down plugin 'mysql_native_password' [root@virtual mysql]#
有了 root 初始密码就可以登录了。
[root@virtual mysql]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.16 Copyright (c) 2000, 2016, 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 密码。
mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql>
以前的 password()
函数将会被抛弃,官方建议使用下面的命令来修改密码ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
;同时,如果你设置的密码过于简单也会报错。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql>
对于 MySQL5.7 用户的密码规则大致如下:
- 长度要大于 8
- 同时要有数字,大小写,特殊字符
也可以暂时不在这里设置 root 密码,下面的安装优化里也会涉及到 root 密码的修改。
PS:MySQL5.6 的 root 初始密码位置/root/.mysql_secret
[0x6]加强 MySQL 安全
默认安装完 MySQL 服务器之后,一般需要执行这个脚本mysql_secure_installation
,这里就会涉及到 root 的密码修改。
[root@virtual mysql]# mysql_secure_installation Securing the MySQL server deployment. Enter password for user root: The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password: Sorry, passwords do not match. New password: Re-enter new password: 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) : y New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y 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! [root@virtual mysql]#
下面列出mysql_secure_installation
做的事情
- Set root password? [Y/n] Y
- Remove anonymous users? [Y/n] Y
- Disallow root login remotely? [Y/n] Y
- Remove test database and access to it? [Y/n] Y
- Reload privilege tables now? [Y/n] Y
其中设置 root 密码根据情况而来吧,可以修改也可以不修改。
[0x7]设置默认字符集
一般都是设置为utf8
,MySQL 的配置文件路径是/etc/my.cnf
,下面就是设置字符集的相关的配置。
# vim /etc/my.cnf [mysqld] #下面的配置是属于mysqld的,所以一定要配置在这个下面 #====设置服务端字符集==== #设置数据库字符集UTF8 character-set-server=utf8 #设置数据库字符集排序规则,注意要和character-set-server对应 collation-server=utf8_general_ci #设置连接是的字符集 init-connect= 'SET NAMES utf8'
保存配置文件,然后重启 MySQL(systemctl restart mysqld
),然后登录 MySQL,查看现在的字符集。
[root@virtual ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> show variables like '%character%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.10 sec) mysql>
[0x8]修改 MySQL 数据目录
迁移数据库目录 MySQL 数据库默认路径/var/lib/mysql
,实际工作中,常常需要定制数据库路径,比如/data/mysql
,或者/opt/mysql
,一般是单独的数据盘或者分区,这样有利于性能调优和保护数据安全,同时也方便进行维护。
- 首先在数据盘创建一个目录专门用来存放 MySQL 的数据的
mkdir -p /data/mysql
- 分配目录的权限
chown -R mysql:mysql /data/mysql
- 关闭 MySQL 服务
systemctl stop mysqld
- 编辑 MySQL 配置文件
[mysqld] #下面是系统默认的数据目录位置 #datadir=/var/lib/mysql #下面这个是修改后的数据目录位置 datadir=/data/mysql #socket=/var/lib/mysql/mysql.sock socket=/data/mysql/mysql.sock
- 迁移数据,即将
/var/lib/mysql
下的所有数据移动到/data/mysql
下,执行命令mv /var/lib/mysql/* /data/mysql/
这个时候如果执行重启命令,会提示错误。
[root@virtual data]# systemctl start mysqld Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details. [root@virtual data]#
查看错误日志,看到其中有这样的日志2016-11-24T14:01:36.732839Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
突然明白没有关闭SELinux
服务,所以如果按照这个笔记来的话,则不会出现这个问题啦。关闭SELinux
然后重启服务器,就可以正常启动 MySQL 服务了。
[0x9]结束
至此 MySQL 的安装和简单优化操作完成了,总体来看,MySQL 的安装比较简单,繁琐的地方还是在配置,如果想要优化的更好的话,那还有一坨事情等着呢,不过这样已经满足一般需求了。
作者:mzlion
链接:https://hacpai.com/article/1479965479377
来源:黑客派
协议:CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0/
CentOS7 安装 MySQL 和简单优化相关推荐
- 生产环境centos7 安装mysql 5.7.35
生产环境centos7 安装mysql 5.7.35 1.初始化系统,并重启 2.卸载原有mysql 2.安装依赖 4.创建mysql的用户 5.创建mysql的数据目录 6.安装 mysql 7. ...
- Centos7安装Mysql详细步骤
本文主要介绍如何在Centos7下安装Mysql. 一.下载Mysql 使用wget命令下载mysql安装包.确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装. ...
- CentOS7安装MySQL数据库、设置防火墙
CentOS7安装MySQL数据库步骤 MySQL数据库安装 下载wget命令 删除已安装的MySQL服务 安装MySQL安装包 可能遇到的问题 问题一 问题二 MySQL数据库配置 获取临时密码 · ...
- centos7 安装mysql php_Centos7安装mysql与php的方法
本文主要和大家分享Centos7安装mysql与php的方法,希望能帮助到大家. 相关mysql视频教程推荐:<mysql教程> 官网下载安装mysql-server 依次使用下面三个命令 ...
- linux 用root安装mysql数据库_Linux上安装Mysql及简单的使用详解
1. 安装mysql sudo apt-get update sudo apt-get install mysql-server sudo apt-get install python-mysqldb ...
- Ubuntu 安装mysql和简单操作
2019独角兽企业重金招聘Python工程师标准>>> ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-s ...
- centos7安装mysql缺失依赖_CentOS7安装mysql5.7不成功,解决依赖包之后还是无法安装成功...
[root@xg79 ~]# yum -y installmysql-community-server ............. Error: Package:mysql-community-ser ...
- centos7 安装mysql php,Centos7安装mysql与php的方法
本文主要和大家分享Centos7安装mysql与php的方法,希望能帮助到大家. 相关mysql视频教程推荐:<mysql教程> 官网下载安装mysql-server 依次使用下面三个命令 ...
- CentOS7安装MySQL(完整版)
CentOS7安装MySQL(完整版) https://blog.csdn.net/qq_36582604/article/details/80526287 安装 MySQL[还没有弄完]
最新文章
- 机器学习中的各种距离
- 又是找 Bug 的一天! | 每日趣闻
- SharePoint 2010 技巧系列: 控制Ribbon菜单权限(SiteActions的例子)
- 获得H.264视频分辨率的方法
- flutter字体不跟随系统_flutter和native共用字体
- 更新view是可以update到表的
- java基础69 JavaScript产生伪验证码(网页知识)
- k8s secret使用_Java Secret:使用枚举构建状态机
- GY歌谣之读懂每行代码(飞智)
- JDBC数据对象存储
- 本地部署Bootstrap3
- 【Codeforces】Gym 101173B Bipartite Blanket 霍尔定理+状压DP
- 动态分级存储特性-SmartTier
- zic2xpm - 将 ZIICS 象棋片段 (chess pieces) 转换为 XBoard (XPM/XIM) 片段的工具。
- 笔记本电脑计算机怎么放在桌面,苹果电脑怎么把文件放在桌面
- 密集人群计数 密度图回归 原理
- 豆瓣最新国产电影Top10
- NodeJS之搭建Web服务器
- PV操作经典例题——哲学家进餐问题
- ChatGPT ,能替代程序员吗?