欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

新手小白Linux(Centos6.5)部署java web项目(mysql5.7安装及相关操作)

程序员文章站 2022-06-24 21:00:34
一、安装 参考:https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html 二、配置 三、导库 ......

一、安装

  参考:

# 检测系统中是否安装了mysql
yum list installed | grep mysql

### 显示内容 ###
### 我已经装过了……没装之前可能什么都没有,也可能有低版本的mysql
mysql-community-client.x86_64
                     5.7.23-1.el6       @mysql57-community-dmr                  
mysql-community-common.x86_64
                     5.7.23-1.el6       @mysql57-community-dmr                  
mysql-community-libs.x86_64
                     5.7.23-1.el6       @mysql57-community-dmr                  
mysql-community-release.noarch
                     el6-5              @/mysql-community-release-el6-5.noarch  
mysql-community-server.x86_64
                     5.7.23-1.el6       @mysql57-community-dmr       



# 卸载低版本mysql,才能安装上mysql5.7
# remove后面是上面显示的文件(-y表示全过程选是),上面出现的全部都要remove掉
yum -y remove mysql-community-client.x86_64 mysql-community-common.x86_64 ...

### 显示内容 ###
...
removed:
  mysql-community-client.x86_64    0:5.7.23-1.el6   mysql-community-common.x86_64 0:5.7.23-1.el6    
  mysql-community-libs.x86_64 0:5.7.23-1.el6   mysql-community-release.noarch 0:el6-5         

dependency removed:
  mysql-community-server.x86_64 0:5.7.23-1.el6

complete!



# 再检测一次,直到为空

# 下载mysql的yum源(el6-5表示惹的redhat6和sentos6-mysql5)
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

# 载入yum源
yum install mysql-community-release-el6-5.noarch.rpm

# 查看mysql可用的安装源(安装源里面有mysql5的好几个版本)
yum repolist enabled | grep mysql

### 显示内容 ###
mysql-connectors-community mysql connectors community                         59
mysql-tools-community      mysql tools community                              65
mysql56-community          mysql 5.6 community server                        453



# 发现没有5.7版本的,修改repo文件,把5.7版本改为可用,5.6版本改为不可用
vi /etc/yum.repos.d/mysql-community.repo

### 显示内容 ###
############################################################################################################

[mysql-connectors-community]
name=mysql connectors community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/rpm-gpg-key-mysql

[mysql-tools-community]
name=mysql tools community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/rpm-gpg-key-mysql

# enable to use mysql 5.5
[mysql55-community]
name=mysql 5.5 community server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/rpm-gpg-key-mysql

# enable to use mysql 5.6
[mysql56-community]
name=mysql 5.6 community server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1 #把这个1改成0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/rpm-gpg-key-mysql

# note: mysql 5.7 is currently in development. for use at your own risk.
# please read with sub pages: https://dev.mysql.com/doc/relnotes/mysql/5.7/en/
[mysql57-community-dmr]
name=mysql 5.7 community server development milestone release
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=0 #把这个0改成1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/rpm-gpg-key-mysql

############################################################################################################



# 按i进入编辑模式!
# 输入数字不要使用小键盘!
# 按esc键退出编辑模式!
# 输入:wq 回车--保存并退出!

# 再次检查mysql可用的安装源
yum repolist enabled | grep mysql
### 显示内容 ###
mysql-connectors-community mysql connectors community                         59
mysql-tools-community      mysql tools community                              65
mysql57-community-dmr      mysql 5.7 community server development milesto    273



# 发现5.7版本的mysql安装源可用
# 使用yum安装mysql
yum install mysql-community-server -y

### 显示内容 ###
### 安装成功!
...
installed:
  mysql-community-server.x86_64 0:5.7.23-1.el6                                                  

dependency installed:
  mysql-community-client.x86_64 0:5.7.23-1.el6   mysql-community-common.x86_64 0:5.7.23-1.el6  
  mysql-community-libs.x86_64 0:5.7.23-1.el6    

complete!



# 启动服务
service mysqld start

# 5.7版本跟5.6版本的登录方式不一样,5.6默认root密码为空,5.7的root密码是随机生成的
# 首次登录可查看root密码,登录后要立即修改密码,设置复杂一些,不然报错说密码不安全
grep "password" /var/log/mysqld.log

### 显示内容 ###
2018-09-05t03:32:48.523907z 1 [note] a temporary password is generated for root@localhost: rr_pvi=rj3<u
...



# 可以看到密码为rr_pvi=rj3<u


# 登录mysql
mysql -uroot -p
enter password: rr_pvi=rj3<u

# 修改root密码 mysql> alter user 'root'@'localhost' identified by 'mynewpassword!';
# 退出 mysql> exit; # 重新登录

 

二、配置

 

# linux系统安装mysql默认区分大小写
# 设置不区分大小写
[root@i-epo5ap9i ~]# vi /etc/my.cnf

#########################################################################################
# for advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# remove leading # and set to the amount of ram for the most important data
# cache in mysql. start at 70% of total ram for dedicated server, else 10%.
# innodb_buffer_pool_size = 128m
#
# remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# 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
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
lower_case_table_names=1 #在这里加上这句,linux下1不区分,0区分

# disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
~
~
#########################################################################################

# 保存并退出

# 重启mysql服务
[root@i-epo5ap9i ~]# service mysqld restart
stopping mysqld:                                           [  ok  ]
starting mysqld:                                           [  ok  ]

# 登录mysql
[root@i-epo5ap9i ~]# mysql -uroot -p
enter password:...
type 'help;' or '\h' for help. type '\c' to clear the current input statement.

# 查看所有数据库
mysql> show databases;
+--------------------+
| database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

# 使用数据库:mysql
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> show tables;
+---------------------------+
| tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.00 sec)

# 查看用户表(只看主机和用户名字段,*的话太多了)
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| host      | username      |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.00 sec)

# 创建数据库
mysql> create database test;
query ok, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

# 创建用户并授予test数据库的所有权限
mysql> grant all privileges on test.* to 'test_user'@'%' identified by 'test123.password';
query ok, 0 rows affected, 1 warning (0.00 sec)

# 刷新权限
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)

# 再次查看用户表
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | test_user     |
| host      | username      |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
5 rows in set (0.00 sec)

# 查看test_user用户权限
mysql> show grants for 'test_user'@'%';

+-----------------------------------------------------+
| grants for test_user@%                              |
+-----------------------------------------------------+
| grant usage on *.* to 'test_user'@'%'               |
| grant all privileges on `test`.* to 'test_user'@'%' |
+-----------------------------------------------------+
2 rows in set (0.00 sec)

# 退出
mysql> exit;
bye

 

 

三、导库

# 把数据库的sql文件上传到linux服务器
[root@i-epo5ap9i ~]# rz
-bash: rz: command not found

# 安装lrzsz
[root@i-epo5ap9i ~]# yum install lrzsz -y
...
installed:
  lrzsz.x86_64 0:0.12.20-27.1.el6                                                               

complete!

# 从windows本机上传到linux服务器(回车后会弹出一个选择文件的弹窗,选中文件点击add后再点ok)
[root@i-epo5ap9i ~]# rz
rz waiting to receive.
starting zmodem transfer.  press ctrl+c to cancel.
transferring test.sql...
  100%     953 bytes  953 bytes/sec 00:00:01       0 errors  


# 换用户登录mysql
[root@i-epo5ap9i ~]# mysql -u test_user -p
enter password: 
...
type 'help;' or '\h' for help. type '\c' to clear the current input statement.

# 使用test数据库
mysql> use test;
database changed

# 给test数据库设置编码方式
mysql> set names utf8;
query ok, 0 rows affected (0.00 sec)

# 导入数据库(运行部署项目对应的sql文件)
mysql> source /root/test.sql;
query ok, 0 rows affected (0.00 sec)

query ok, 0 rows affected, 1 warning (0.00 sec)

...

# 查看该数据库的所有表
mysql> show tables;
+----------------+
| tables_in_test |
+----------------+
| role           |
| user           |
| ...            |
+----------------+
2 rows in set (0.00 sec)

# 导入成功,退出mysql
mysql> exit;
bye