Linux下安装MySQL

①卸载系统预装 mariadb

rpm -e --nodeps mariadb-libs-1:5.5.56-2.el7.x86_64

注:MySQL 被 Oracle 收购,社区担心将来 MySQL 被 Oracle 关闭开源模式,和 Oracle 数据库一样变成商业化运作。所以社区开发了一个 MySQL 的社区版,内部和 MySQL 一样,只是名字不同,这就是 mariadb。但是我们当前在 Linux 系统中已经预装的 mariadb 只是一个残片,不能直接使用。所以还是要先卸载。

②安装服务器端程序

rpm -ivh /opt/MySQL-server-5.5.52-1.el6.x86_64.rpm

验证

[root@apple opt]# id mysql

uid=988(mysql) gid=982(mysql) 组=982(mysql)

③安装客户端程序

rpm -ivh /opt/MySQL-client-5.5.52-1.el6.x86_64.rpm

④启动 MySQL 服务

systemctl start mysql.service

⑤设置 root 用户密码

注意:这里的 root 用户是 MySQL 的 root 用户,不是 Linux 的 root 用户

[root@apple opt]# mysqladmin -u root password

New password: 

Confirm new password:

⑥登录MySQL

[root@apple opt]# mysql -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.52 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 databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.00 sec)

⑦客户端登录 MySQL 服务器

[1]被防火墙拦截的错误提示

通过关闭防火墙服务器解决

systemctl stop firewalld.service

systemctl disable firewalld.service

[2]被MySQL自己拒绝连接

[3]在 MySQL 服务器端设置允许任何主机地址访问

执行SQL语句

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

重启 MySQL 服务。下面是执行操作的参考:

[root@apple opt]# mysql -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.5.52 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> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select host,user,password from user;

+-----------+------+-------------------------------------------+

| host      | user | password                                  |

+-----------+------+-------------------------------------------+

| localhost | root | *453FDE92DF58E2DE1A51D27869CF3F1A69984B1B |

| apple     | root |                                           |

| 127.0.0.1 | root |                                           |

| ::1       | root |                                           |

| localhost |      |                                           |

| apple     |      |                                           |

+-----------+------+-------------------------------------------+

6 rows in set (0.01 sec)

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

Query OK, 0 rows affected (0.00 sec)

mysql> select host,user,password,select_priv,update_priv from user;

+-----------+------+-------------------------------------------+-------------+-------------+

| host      | user | password                                  | select_priv | update_priv |

+-----------+------+-------------------------------------------+-------------+-------------+

| localhost | root | *453FDE92DF58E2DE1A51D27869CF3F1A69984B1B | Y           | Y           |

| apple     | root |                                           | Y           | Y           |

| 127.0.0.1 | root |                                           | Y           | Y           |

| ::1       | root |                                           | Y           | Y           |

| localhost |      |                                           | N           | N           |

| apple     |      |                                           | N           | N           |

| %         | root | *453FDE92DF58E2DE1A51D27869CF3F1A69984B1B | Y           | Y           |

+-----------+------+-------------------------------------------+-------------+-------------+

7 rows in set (0.00 sec)

重启MySQL服务

systemctl restart mysql.service

⑧解决字符乱码问题

[1]查看字符相关变量

mysql> show variables like "%char%";

+--------------------------+----------------------------+

| Variable_name            | Value                      |

+--------------------------+----------------------------+

| character_set_client     | utf8                       |

| character_set_connection | utf8                       |

| character_set_database   | latin1                     |

| character_set_filesystem | binary                     |

| character_set_results    | utf8                       |

| character_set_server     | latin1                     |

| character_set_system     | utf8                       |

| character_sets_dir       | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+

8 rows in set (0.00 sec)

[2]准备MySQL配置文件

cp /usr/share/mysql/my-small.cnf /etc/my.cnf
[mysqld]

port            = 3306

socket          = /var/lib/mysql/mysql.sock

skip-external-locking

key_buffer_size = 16K

max_allowed_packet = 1M

table_open_cache = 4

sort_buffer_size = 64K

read_buffer_size = 256K

read_rnd_buffer_size = 256K

net_buffer_length = 2K

thread_stack = 128K

character-set-server=utf8

在[mysqld]部分的配置的最后添加 character-set-server=utf8

注意:别在 vim 的一般模式下直接粘贴!一定要进入编辑模式!

重启MySQL服务

systemctl restart mysql.service

查看字符集相关变量

mysql> show variables like "%char%";

+--------------------------+----------------------------+

| 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.00 sec)

重新创建数据库、创建数据库表、插入中文字符数据验证。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值