[Big Data] Install MySQL database in CentOS7 environment

1. Install mysql

CentOS7 comes with Mariadb, we need to delete the .

我们在Linux系统中,如果要使用关系型数据库的话,基本都是用的mysql。
而且以往7以下版本的centos系统都是默认的集成有mysql。
然而对于现在最新的centos7系统来说,已经不支持mysql数据库,它默认内部集成了mariaDB。
如果我们想要使用 mysql 的话,就要先将原来的mariaDB卸载掉,不然会引起冲突。

1.1 Delete the CentOS system's own database

  1. Query the version of mariadb in CentOS7

    ·查询命令:rpm -qa|grep mariadb
    

    insert image description here

  2. Forcefully uninstall the installed mariadb

    ·卸载命令:rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
    

    insert image description here

1.2 Start to install mysql

准备工作:
	在/opt/source路径下新建一个mysql的文件夹
	/opt目录下:(如果没有下面两个文件夹,可自行放置对应的文件夹中,路径一定不要搞混!)
		# module 存放解压后的文件
		# source 存放压缩包
  1. Download mysql (download to local first)

    【了解】
    也可以直接在Linux系统中执行以下命令进行下载(以下两条命令是最新版Mysql8.0版本的下载和解压命令):
    ·下载(该命令是下载到当前目录下):wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.13-1.el7.x86_64.rpm-bundle.tar
    ·解压(解压到当前目录下):tar -xvf mysql-8.0.13-1.el7.x86_64.rpm-bundle.tar
    

    Tsinghua University mirror download mysql5.7 version (click here)
    search myql
    insert image description here

    Enter downloads
    insert image description here
    to find mysql5.7 version
    insert image description here
    Find and download mysql-5.7.38-1.el7.x86_64.rpm-bundle.tarinsert image description here

  2. Use Xftp in Xshell to upload the MySQL file to the /opt/source folder in CentOS7
    (if you don’t have Xshell, you can use the rz command to upload)
    insert image description here
    insert image description here

  3. decompress

    ·在source路径下新建一个mysql文件夹(如果之前没有创建的话):mkdir mysql
    ·然后将mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar解压到新建的mysql文件夹中
    ·解压命令:tar xvf mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar -C ./mysql
    

    insert image description here
    Then move the mysql folder under /opt/source to the /opt/module folder:

    ·命令:mv mysql ../module
    

    insert image description here

  4. Install (enter the mysql folder)

    依次执行以下5个命令
    ·安装命令1:rpm -ivh mysql-community-common-5.7.38-1.el7.x86_64.rpm	
    ·安装命令2:rpm -ivh mysql-community-libs-5.7.38-1.el7.x86_64.rpm
    ·安装命令3:rpm -ivh mysql-community-libs-compat-5.7.38-1.el7.x86_64
    ·安装命令4:rpm -ivh mysql-community-client-5.7.38-1.el7.x86_64.rpm
    ·安装命令5:rpm -ivh mysql-community-server-5.7.38-1.el7.x86_64.rpm
    

    insert image description here
    insert image description here
    insert image description here
    An error is reported when executing command 3:
    insert image description here

    After going through Baidu, I found that just add --force --nodeps after the command, as follows:insert image description here

  5. Check whether mysql is installed successfully:

    ·查询命令:rpm -qa|grep mysql
    

    insert image description here

1.3 Start the mysql service and set a password

  1. start mysql service

    ·启动命令:systemctl start mysqld
    ·查看mysql启动状态:systemctl status mysqld
    ·查看服务:netstat -nltp
    

    ·Start mysql, and view mysql startup statusinsert image description here

    ·View service
    insert image description here

  2. Log in

    ·查找初始密码:
    	grep "A temporary password" /var/log/mysqld.log
    ·登录mysql
    	mysql -uroot -p
    	输入初始密码:/+3wHq0ag>z>
    ·设置新密码:
    	ALTER USER 'root'@'localhost' IDENTIFIED BY 'MySQL_123';
    	新密码为:MySQL_123
    ·退出客户端:
    	exit
    ·重新登录:
    	mysql -uroot -pMySQL_123
    

    ·Find the initial password: insert image description here
    ·Successful login:
    insert image description here
    ·Set a new password:
    insert image description here
    ·Exit the client:
    insert image description here
    ·Re-login:
    insert image description here

1.4 Authorize all external users to connect to MySQL (understand)

·命令:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MySQL_123' WITH GRANT OPTION;
·说明: grant 权限名(所有的权限用all)on 库名(*全部).表名(*全部) to '要授权的用户名'@'%'(%表示所有的IP,也可以只写一个IP) identified by "密码";

·刷新:flush privileges;
·重启mysql服务即可
	systemctl restart mysqld

insert image description here

insert image description here

2. The client operates mysql

Common query operations

1. 查看所有数据库:show databases;
2.切换到mysql数据库:use mysql;
3.查看数据库中有哪些表:show tables;
4.查看use表的内容:select Host,User from user;
5.查看表结构:desc user;
6.修改user表中host等于localhost的行(只能进行本机连接不能用于外网访问mysql数据库):
	update user set host='%' where host='localhost';
  1. View all databases:

insert image description here

2. Switch to the mysql database:

insert image description here

3. Check which tables are in the database:
insert image description here

4. View the contents of the use table:

insert image description here

5. View the table structure:
insert image description here

·Delete the row in the user table where host is equal to %
mysql>delete from user where host='%';
insert image description here

Solve the problem of garbled Chinese characters

  1. Modify the mysql configuration file, and change the encoding method to: UTF format (to solve the problem of garbled Chinese characters)
    ·执行以下命令:
    	vim /etc/my.cnf
    	修改内容如下:
    		[client]
    		default-character-set=utf8
    		[mysql]
    		 character-set-server=utf8
    		 collation-server=utf8_general_ci
    ·重启mysql数据库:
    	service mysqld restart
    
    Before modification:
    insert image description here
    After modification:
    insert image description here
    Restart the mysql database
    insert image description here
    View the changes after modification
    ·进入mysql
    ·输入以下命令:
    	status
    
    insert image description here

create operation

  1. Create a student database

    ·进入mysql
    ·创建student数据库命令:create database student default charset 'utf8';
    ·删除数据库命令:drop database student;
    

    insert image description here

  2. create stu table

    ·进入student数据库:use student;
    ·创建stub表:create table if not exists stu(id int primary key auto_increment,name varchar(20),gender char(2)) default charset 'utf8';
    

    insert image description here

  3. Insert records into stu table

    ·向sut表插入记录:insert into stu(name,gender)values('张三','男'),('小李','女'),('王五','男');
    ·查询stu表中的记录:select * from stu;
    

    insert image description here

  4. query form

    ·查看students数据库中的表:show tables;
    

    insert image description here

Guess you like

Origin blog.csdn.net/weixin_45954198/article/details/128474042