Linux下安装MySQL数据库

基于CentOS7,MySQL版本为5.7.11

将MySQL安装在/usr/local/mysql目录下

mkdir /usr/local/mysql

下载MySQL压缩包

wget https://dev.MySQL.com/get/Downloads/MySQL-5.7/mysql-5.7.11-Linux-glibc2.5-x86_64.tar.gz 

// 由于MySQL运行需要libaio库,所以需要运行以下命令进行安装
yum install -y libaio

// 如果上边的命令不行的话 可以使用下边的命令
curl -O -L https://dev.MySQL.com/get/Downloads/MySQL-5.7/mysql-5.7.11-Linux-glibc2.5-x86_64.tar.gz

解压并复制

tar -xvf mysql-5.7.11-Linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.11-linux-glibc2.5-x86_64/* /usr/local/mysql/

创建data目录

mkdir /usr/local/mysql/data

创建mysql用户组及其用户

groupadd mysql
useradd -r -g mysql mysql

初始化数据

切换到mysql文件夹下面

[root@localhost mysql] ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

复制配置文件到 /etc/my.cnf

cp -a ./support-files/my-default.cnf /etc/my.cnf (选择y)

MySQL的服务脚本放到系统服务中

cp -a ./support-files/mysql.server /etc/init.d/mysqld

vim编辑etc文件夹下的my.cnf文件

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
# server_id = .....
socket = /tmp/mysql.sock
character-set-server = utf8
# 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

创建软链接

ln -s /usr/local/mysql/bin/mysql  /usr/bin/

启动服务

service mysqld start 

初始化密码

mysql5.7会生成一个初始化密码,在root中.mysql_secret文件中。
[root@localhost ~]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2017-03-16 00:52:34 
ws;fmT7yh0CM

登录并修改密码

[root@localhost ~]# mysql -u root -p
输入上面的密码,并执行以下命令
alter user root@localhost identified by 'root';
flush privileges;

退出重新登录,至此,全部完成

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
rows in set (0.11 sec)

  1. 创建数据库:

create database abc;

  1. 导入sql文件:

source /xxx/xxx/xxxx.sql;

  1. 修改数据库的密码(有的数据库密码字段不叫authentication_string,而是password,可自行修改):

UPDATE user SET authentication_string = password ( '新密码' ) WHERE User = 'root' ;

  1. 重启mysql命令:

service mysqld restart

  1. 设置用户的权限

    • 允许所有远程ip进行连接

    GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY '这里写自己数据库的密码' WITH GRANT OPTION;

    • 只允许指定的ip进行连接

    GRANT ALL PRIVILEGES ON . TO 'root'@'允许远程连接的ip地址' IDENTIFIED BY '这里写自己数据库的密码' WITH GRANT OPTION;

    • 允许某个用户访问某个库中的某些字段

    GRANT select (xx,xx,xx,..) on 库名.表名 to '用户名'@'ip地址';

    • 最后都需要刷新权限

    FLUSH PRIVILEGES;

你可能感兴趣的:(Linux下安装MySQL数据库)