Article Directory
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
-
Query the version of mariadb in CentOS7
·查询命令:rpm -qa|grep mariadb
-
Forcefully uninstall the installed mariadb
·卸载命令:rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
1.2 Start to install mysql
准备工作:
在/opt/source路径下新建一个mysql的文件夹
/opt目录下:(如果没有下面两个文件夹,可自行放置对应的文件夹中,路径一定不要搞混!)
# module 存放解压后的文件
# source 存放压缩包
-
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
Enter downloads
to find mysql5.7 version
Find and download mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar -
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)
-
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
Then move the mysql folder under /opt/source to the /opt/module folder:·命令:mv mysql ../module
-
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
An error is reported when executing command 3:
After going through Baidu, I found that just add --force --nodeps after the command, as follows:
-
Check whether mysql is installed successfully:
·查询命令:rpm -qa|grep mysql
1.3 Start the mysql service and set a password
-
start mysql service
·启动命令:systemctl start mysqld ·查看mysql启动状态:systemctl status mysqld ·查看服务:netstat -nltp
·Start mysql, and view mysql startup status
·View service
-
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:
·Successful login:
·Set a new password:
·Exit the client:
·Re-login:
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
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';
- View all databases:
2. Switch to the mysql database:
3. Check which tables are in the database:
4. View the contents of the use table:
5. View the table structure:
·Delete the row in the user table where host is equal to %
mysql>delete from user where host='%';
Solve the problem of garbled Chinese characters
- Modify the mysql configuration file, and change the encoding method to: UTF format (to solve the problem of garbled Chinese characters)
Before modification:·执行以下命令: vim /etc/my.cnf 修改内容如下: [client] default-character-set=utf8 [mysql] character-set-server=utf8 collation-server=utf8_general_ci ·重启mysql数据库: service mysqld restart
After modification:
Restart the mysql database
View the changes after modification·进入mysql ·输入以下命令: status
create operation
-
Create a student database
·进入mysql ·创建student数据库命令:create database student default charset 'utf8'; ·删除数据库命令:drop database student;
-
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 records into stu table
·向sut表插入记录:insert into stu(name,gender)values('张三','男'),('小李','女'),('王五','男'); ·查询stu表中的记录:select * from stu;
-
query form
·查看students数据库中的表:show tables;