Linux Cetnos7安装Mysql

安装MySQL,Linux Centos7

下载并安装Mysql官方的Yum Repository

[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

如果提示wget未安装,执行以下命令
yum install wget -y
等待安装完成即可

使用下面的命令直接安装Yum Repository

[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm

安装Mysql服务器

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

Mysql数据库设置

  1. 启动mysql

    [root@localhost ~]# systemctl start mysqld.service

  2. 查看Mysql运行状态

    [root@localhost ~]# systemctl status mysqld.service

  3. mysql已经启动,查看mysql密码并进行登录

    [root@localhost ~]# grep "password" /var/log/mysqld.log

    图中圈出来的地方即为初始密码
    初始密码

  4. 登录mysql

    [root@localhost ~]# mysql -uroot -p

    登录密码可能会有错误,那就参考下面这篇博客进行无密码登录
    https://blog.csdn.net/qq_37671523/article/details/102160714

  5. 修改密码(严格按照这个步骤来,否则不成功!!!)

        mysql> set global validate_password_policy=0;
    
      	 mysql> set global validate_password_length=1;
    
     	 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
    
  6. 开启远程访问权限

    #任何主机

    mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
    

    #刷新权限
    mysql>flush privileges;
    #退出Mysql
    mysql>exit;

  7. 卸载Yum Repository

    [root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch

为了让 MySQL支持中文,需要把字符集改成UTF-8,方法如下

# vim /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
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8

[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
character-set-server=utf8
# 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

重启mysql服务

# service mysqld restart

之后reboot虚拟机就可以了。

执行了以上操作还有可能会导致在自己的电脑上无法远程连接虚拟机中的mysql,原因可能是虚拟机没有打开3306端口,方法参考下面这篇博客
https://blog.csdn.net/zsp151296/article/details/89364738

猜你喜欢

转载自blog.csdn.net/weixin_44851055/article/details/114941051