CentOS7安装MySQL(完整版)

﹥>﹥吖頭↗ 提交于 2020-12-05 12:07:55

<div class="htmledit_views" id="content_views"> <p>&nbsp;</p>

<p><span style="color:#000000;">在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。</span></p>

<p>&nbsp;</p>

<h3>1 下载并安装MySQL官方的&nbsp;Yum Repository</h3>

<pre class="has"><code class="language-swift">[root@localhost ~]# wget -i -<span class="hljs-built_in">c</span> http:<span class="hljs-comment">//dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm</span></code></pre>

<p>&nbsp;</p>

<p><span style="color:#000000;">&nbsp; 使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。</span></p>

<pre class="has"><code class="language-css"><span class="hljs-selector-attr">[root@localhost ~]</span># <span class="hljs-selector-tag">yum</span> <span class="hljs-selector-tag">-y</span> <span class="hljs-selector-tag">install</span> <span class="hljs-selector-tag">mysql57-community-release-el7-10</span><span class="hljs-selector-class">.noarch</span><span class="hljs-selector-class">.rpm</span></code></pre>

<p>&nbsp;</p>

<p><span style="color:#000000;">之后就开始安装MySQL服务器。</span></p>

<pre class="has"><code class="language-perl">[root@localhost ~]<span class="hljs-comment"># yum -y install mysql-community-server</span></code></pre>

<p><span style="color:#000000;">这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb。</span></p>

<p><span style="color:#000000;"><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209203835547-746466608.png"></span></p>

<p><span style="color:#000000;">至此MySQL就安装完成了,然后是对MySQL的一些设置。</span></p>

<h3>2 MySQL数据库设置</h3>

<p><span style="color:#000000;">&nbsp; 首先启动MySQL</span></p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp;</p>

<p>&nbsp;</p>

<pre class="has"><code class="language-css"><span class="hljs-selector-attr">[root@localhost ~]</span># <span class="hljs-selector-tag">systemctl</span> <span class="hljs-selector-tag">start</span> <span class="hljs-selector-tag">mysqld</span><span class="hljs-selector-class">.service</span></code></pre>

<p><span style="color:#000000;">&nbsp; 查看MySQL运行状态,运行状态如图:</span></p>

<p>&nbsp;</p>

<pre class="has"><code class="language-html hljs xml">[root@localhost ~]# systemctl status mysqld.service</code><div class="hljs-button signin" data-title="登录后复制" onclick="hljs.signin(event)"></div></pre>

<p><span style="color:#000000;"><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209203855442-1655350294.png"></span></p>

<p><span style="color:#000000;"><span style="color:#000000;">此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:</span></span></p>

<pre class="has"><code class="language-vbscript">[root@localhost ~]# grep <span class="hljs-string">"password"</span> /var/<span class="hljs-built_in">log</span>/mysqld.<span class="hljs-built_in">log</span></code></pre>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209203915260-1942438112.png"></p>

<p><span style="color:#000000;">&nbsp; 如下命令进入数据库:</span></p>

<pre class="has"><code class="language-perl">[root@localhost ~]<span class="hljs-comment"># mysql -uroot -p</span></code></pre>

<p><span style="color:#000000;">输入初始密码(<span style="color:#000000;">是上面图片最后面的 no;e!5&gt;&gt;alfg</span>),此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:</span></p>

<pre class="has"><code class="language-java">mysql&gt; ALTER USER <span class="hljs-string">'root'</span>@<span class="hljs-string">'localhost'</span> IDENTIFIED BY <span class="hljs-string">'new password'</span>;</code></pre>

<p>其中‘new password’替换成你要设置的密码,注意:密码设置必须要大小写字母数字和特殊符号(,/';:等),不然不能配置成功</p>

<p>&nbsp;</p>

<h3>3 开启mysql的远程访问</h3>

<p>执行以下命令开启远程访问限制(注意:下面命令开启的IP是 192.168.0.1,如要开启所有的,用%代替IP):</p>

<pre class="has"><code class="language-sql"><span class="hljs-keyword">grant</span> all <span class="hljs-keyword">privileges</span> <span class="hljs-keyword">on</span> *.* <span class="hljs-keyword">to</span> <span class="hljs-string">'root'</span>@<span class="hljs-string">'192.168.0.1'</span> <span class="hljs-keyword">identified</span> <span class="hljs-keyword">by</span> <span class="hljs-string">'password'</span> <span class="hljs-keyword">with</span> <span class="hljs-keyword">grant</span> <span class="hljs-keyword">option</span>;</code></pre>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209203933806-178841248.png"></p>

<p>然后再输入下面两行命令</p>

<pre class="has"><code class="language-undefined">mysql&gt; flush privileges; </code></pre>

<pre class="has"><code class="language-vbscript">mysql&gt; <span class="hljs-keyword">exit</span></code></pre>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209203950714-262872624.png"></p>

<p>&nbsp;</p>

<h3>4 为firewalld添加开放端口</h3>

<p>添加mysql端口3306和Tomcat端口8080</p>

<pre class="has"><code class="language-perl">[root@localhost ~]<span class="hljs-comment"># firewall-cmd --zone=public --add-port=3306/tcp --permanent</span></code></pre>

<pre class="has"><code class="language-perl">[root@localhost ~]<span class="hljs-comment"># firewall-cmd --zone=public --add-port=8080/tcp --permanent</span></code></pre>

<p>然后再重新载入</p>

<pre class="has"><code class="language-perl">[root@localhost ~]<span class="hljs-comment"># firewall-cmd --reload</span></code></pre>

<p>&nbsp;</p>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209204010852-701003602.png"></p>

<p>&nbsp;</p>

<h3>5 更改mysql的语言</h3>

<p>首先重新登录mysql,然后输入status:</p>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209204027769-1825625649.png"></p>

<p>&nbsp;</p>

<p>可以看到,绿色箭头处不是utf-8</p>

<p>&nbsp;</p>

<p>因此我们先退出mysql,然后再到、etc目录下的my.cnf文件下修改一下文件内容</p>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209204053174-95064340.png"></p>

<p>进入文件后,新增四行代码:</p>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209204107107-2002321317.png"></p>

<p>保存更改后的my.cnf文件后,重启下mysql,然后输入status再次查看,你就会发现变化啦</p>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209204123096-1613813450.png"></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>最后,到Windows下用cmd命令启动mysql啦!!</p>

<p><img alt="" class="has" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191209204143810-1840687449.png"></p> 原文地址:https://blog.csdn.net/qq_36582604/article/details/80526287 </div>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!