基于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運(yùn)行需要libaio庫搓茬,所以需要運(yùn)行以下命令進(jìn)行安裝
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
解壓并復(fù)制
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/
創(chuàng)建data目錄
mkdir /usr/local/mysql/data
創(chuàng)建mysql用戶組及其用戶
groupadd mysql
useradd -r -g mysql mysql
初始化數(shù)據(jù)
切換到mysql文件夾下面
[root@localhost mysql] ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
復(fù)制配置文件到 /etc/my.cnf
cp -a ./support-files/my-default.cnf /etc/my.cnf (選擇y)
MySQL的服務(wù)腳本放到系統(tǒng)服務(wù)中
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
創(chuàng)建軟鏈接
ln -s /usr/local/mysql/bin/mysql /usr/bin/
啟動(dòng)服務(wù)
service mysqld start
初始化密碼
mysql5.7會(huì)生成一個(gè)初始化密碼,在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
輸入上面的密碼,并執(zhí)行以下命令
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)
- 創(chuàng)建數(shù)據(jù)庫:
create database abc;
- 導(dǎo)入sql文件:
source /xxx/xxx/xxxx.sql;
- 修改數(shù)據(jù)庫的密碼(有的數(shù)據(jù)庫密碼字段不叫authentication_string,而是password庐扫,可自行修改):
UPDATE user SET authentication_string = password ( '新密碼' ) WHERE User = 'root' ;
- 重啟mysql命令:
service mysqld restart
-
設(shè)置用戶的權(quán)限
- 允許所有遠(yuǎn)程ip進(jìn)行連接
GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY '這里寫自己數(shù)據(jù)庫的密碼' WITH GRANT OPTION;
- 只允許指定的ip進(jìn)行連接
GRANT ALL PRIVILEGES ON . TO 'root'@'允許遠(yuǎn)程連接的ip地址' IDENTIFIED BY '這里寫自己數(shù)據(jù)庫的密碼' WITH GRANT OPTION;
- 允許某個(gè)用戶訪問某個(gè)庫中的某些字段
GRANT select (xx,xx,xx,..) on 庫名.表名 to '用戶名'@'ip地址'饭望;
- 最后都需要刷新權(quán)限
FLUSH PRIVILEGES;