美文网首页
MySQL安装、卸载及基础配置

MySQL安装、卸载及基础配置

作者: Doooook | 来源:发表于2020-11-15 22:42 被阅读0次

    MySQL单机安装

    操作系统:CentoOS 7
    MySQL:5.6
    

    MySQL卸载

    查看MySQL软件

    rpm -qa | grep mysql
    yum repolist all | grep mysql
    
    image.png image.png

    卸载MySQL

    # 卸载mysql
    yum remove -y mysql mysql-libs mysql-common
    # 删除mysql下的数据文件
    rm -rf /var/lib/mysql
    # 删除mysql配置文件
    rm /etc/my.cnf
    # 删除组件
    yum remove -y mysql-community-release-el6-5.noarch 
    

    查看是否还有MySQL软件,有的话继续删除。

    安装MySQL

    # 下载rpm文件
    wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm 
    # 执行rpm源文件
    rpm -ivh mysql-community-release-el6-5.noarch.rpm 
    # 执行安装文件
    yum install mysql-community-server
    

    启动MySQL

    systemctl start mysqld
    

    设置root用户密码

    例如:为root账号设置密码为root

    /usr/bin/mysqladmin -u root password 'root'
    # 如果原来有旧密码,需要修改密码,123是旧密码,root为新密码
    /usr/bin/mysqladmin -u root -p '123' password 'root'
    

    MySQL远程连接授权

    • 授权命令
    grant 权限 on 数据库对象 to 用户
    
    • 示例:
      授予root用户对所有数据库对象的全部操作权限:
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; 
    -- 刷新权限
    FLUSH PRIVILEGES;
    
    • 命令说明
      • ALL PRIVILEGES:表示授予所有的权限,此处可以指定具体的授权权限。
      • *.*:表示所有库中的所有表。
      • 'root'@'%':root是数据库的用户名,%表示任意的IP地址,可以指定具体的IP地址。
      • IDENTIFIED BY 'root':root表示数据库的密码。

    关闭Linux防火墙

    # 关闭防火墙
    ststemctl stop firewalld
    # 设置开机不启动
    systemctl disable firewalld.service
    

    配置MySQL

    vim /etc/my.cnf
    

    修改内容如下:

    [mysqld]
    # MySQL设置大小写不敏感:默认:区分表名的大小写,不区分列名的大小写 
    # 0:大小写敏感 1:大小写不敏感 
    lower_case_table_names=1 
    # 默认字符集
    character-set-server=utf8
    

    注意:一定要配置在[mysqld]下面

    客户端远程访问

    mysql -h 192.168.65.102 -uroot -proot
    

    如果连接不上,可以按照如下步骤排查:

    # 1. MySQL是否正常启动
    [root@mysql-os ~]# ps -ef | grep mysql
    root       6697      1  0 21:58 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
    mysql      7157   6697  0 21:58 ?        00:00:03 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
    root      25431  24971  0 22:15 pts/2    00:00:00 mysql -uroot -px xx
    root      86966   7421  0 22:45 pts/0    00:00:00 grep --color=auto mysql
    
    # 2. 查看防火墙是否关闭
    [root@mysql-os ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
         Docs: man:firewalld(1)
    
    # 3. 查看root权限为所有ip都可以访问
    mysql> show grants for root;
    +--------------------------------------------------------------------------------------------------------------------------------+
    | Grants for root@%                                                                                                              |
    +--------------------------------------------------------------------------------------------------------------------------------+
    | GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION |
    +--------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    # 4. 服务器与客户端是否可以ping/telnet通
    (base) localhost:~ pengjinsen$ ping 192.168.65.102
    PING 192.168.65.102 (192.168.65.102): 56 data bytes
    64 bytes from 192.168.65.102: icmp_seq=0 ttl=64 time=0.187 ms
    64 bytes from 192.168.65.102: icmp_seq=1 ttl=64 time=0.399 ms
    64 bytes from 192.168.65.102: icmp_seq=2 ttl=64 time=0.376 ms
    64 bytes from 192.168.65.102: icmp_seq=3 ttl=64 time=0.206 ms
    64 bytes from 192.168.65.102: icmp_seq=4 ttl=64 time=0.144 ms
    ^C
    --- 192.168.65.102 ping statistics ---
    5 packets transmitted, 5 packets received, 0.0% packet loss
    round-trip min/avg/max/stddev = 0.144/0.262/0.399/0.104 ms
    (base) localhost:~ pengjinsen$ telnet 192.168.65.102 3306
    Trying 192.168.65.102...
    Connected to localhost.
    Escape character is '^]'.
    J
    

    相关文章

      网友评论

          本文标题:MySQL安装、卸载及基础配置

          本文链接:https://www.haomeiwen.com/subject/oieobktx.html