centos 7.6 —— 安装数据库mysql-5.7.17

centos 7.6 —— 安装数据库mysql-5.7.17

文章目录

  • centos 7.6 —— 安装数据库mysql-5.7.17
    • 1. 安装包解压缩编译
    • 2. 修改MySQL配置文件my.cnf
    • 3. 设置环境变量&初始化数据库
    • 4. 启动数据库并设置用户密码
    • 5. 允许远程终端连接数据

1. 安装包解压缩编译


yum -y install ncurses ncurses-devel bison cmake

useradd -s /sbin/nologin mysql   //登录的数据库的用户

cd /opt
tar zxvf mysql-5.7.17.tar.gz   
tar zxvf boost_1_59_0.tar.gz

 mv boost_1_59_0 /usr/local/

cd /usr/local/

mv boost_1_59_0/ boost


cd /opt/mysql-5.7.17/
yum -y install gcc gcc-c++ 

cd mysql-5.7.20/
 cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \    //安装路径的设置
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \    //sock 文件路径设置
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DWITH_SYSTEMD=1

make  && make install


2. 修改MySQL配置文件my.cnf

调整权限
chown -R mysql:mysql /usr//local/mysql/     //数据库文件添加属组
调整配置文件

[root@localhost mysql-5.7.20]#vi /etc/my.cnf  
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysq/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

3. 设置环境变量&初始化数据库

设置环境变量
echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >>/etc/profile
source /etc/profile

初始化数据库
cd /usr/local/mysql
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

4. 启动数据库并设置用户密码


数据库开启
cp usr/lib/systemd/system/mysqld.service  /usr/lib/systemd/system/ ## 系统能识别 能用systemctl命令开启

systemctl daemon-reload   //数据库重新加载

systemctl  start mysqld  ## 其他如自启动关闭查看状态跟其他服务命令一个格式

netstat -anpt | grep 3306  ## 开启后查看服务

设置密码
 mysqladmin   -u  root -p  password
[root@localhost mysql]#  mysqladmin   -u  root -p  password
Enter password:      ## 原来没密码 直接回车
New password:         ## 输入新密码
Confirm new password:    ## 再次输入密码
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

5. 允许远程终端连接数据

###########################测试数据库###########################
## 报错
[root@localhost mysql]#  mysqladmin   -u  root -p  password//设置数据库的密码  方法二:mysql -u root -p password “123”
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysq/mysql.sock' (2)
改用
[root@localhost mysql]# mysql -uroot -h 127.0.0.1 -p   //需要输入数据库服务器的ip地址

#########################允许远程终端连接数据库###########################

[root@localhost mysql]# mysql -uroot -h 127.0.0.1 -p

mysql> grant all privileges on *.* to 'root'@'%' identified by '123' with grant option;  //允许远程终端连接数据库
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit

你可能感兴趣的:(数据库)