美文网首页程序员
Linux下安装Mysql

Linux下安装Mysql

作者: 阿懒土灵 | 来源:发表于2019-03-07 19:06 被阅读5次

    安装

    • 下载并安装
    wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
    rpm -ivh mysql-community-release-el7-5.noarch.rpm
    yum update
    yum install mysql-server
    
    • 权限设置
    chown mysql:mysql -R /var/lib/mysql
    chmod 755 /etc/rc.d/init.d/mysqld
    
    • 初始化MySQL
    mysqld --initialize
    
    • 启动mysql服务
    service mysqld start
    
    • 关闭服务和查看状态
    service mysqld stop
    service mysqld status
    

    设置root密码

    • 停止mysql服务
    service mysqld stop
    
    • 用安全模式重启mysql
    mysqld_safe --skip-grant-tables
    
    • 重新打开一个shell操作界面,在新界面操作
    mysql -u root
    
    • 进入mysql命令行
    use mysql;
    flush privileges;
    alter user 'root'@'localhost' identified by '123';
    flush privileges;
    
    • 退出,重启mysql
    exit;
    
    service mysqld stop;
    service mysqld start;
    

    设置远程访问

    • 在安装mysql机器操作
    mysql -h localhost -u root -p 
    

    输入密码之后,进入MySQL操作台

    • 命令行输入命令
    update user set host = '%' where user = 'root';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    
    • 解决navicat连接MySQL 1251错误
    alter user 'root'@'%' identified with mysql_native_password by 'yourpassword';
    

    字符集

    • 查询字符集
      进入mysql命令行
    mysql> show variables like '%char%';  
    +--------------------------------------+--------------------------------+
    | Variable_name                        | Value                          |
    +--------------------------------------+--------------------------------+
    | character_set_client                 | utf8mb4                        |
    | character_set_connection             | utf8mb4                        |
    | character_set_database               | utf8mb4                        |
    | character_set_filesystem             | binary                         |
    | character_set_results                | utf8mb4                        |
    | character_set_server                 | utf8mb4                        |
    | character_set_system                 | utf8                           |
    | character_sets_dir                   | /usr/share/mysql-8.0/charsets/ |
    | validate_password.special_char_count | 1                              |
    +--------------------------------------+--------------------------------+
    9 rows in set (0.00 sec) 
    

    查看编码格式,将编码设置为统一格式utf8。

    设置数据存储位置

    • 暂停mysql服务
    service mysqld stop
    
    • 移动mysql文件夹到指定目录/home
    mv /var/lib/mysql /home/
    
    • 修改配置文件my.cnf
    vim /etc/my.cnf
    

    将配置文件中字段改为如下格式

    [client]
    socket=/home/mysql/mysql.sock
    
    [mysqld]
    datadir=/home/mysql
    socket=/home/mysql/mysql.sock
    
    • 启动mysql
    service mysqld start
    
    • 进入mysql命令行输入命令查看数据存放位置是否变更正确
    mysql>  show global variables like "%datadir%";
    

    相关文章

      网友评论

        本文标题:Linux下安装Mysql

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