1、环境设置
安装包: mariadb-server.x86_64
yum install mariadb-server.x86_64 #安装软件包
systemctl start mariadb
systemctl enable mariadb.service
systemctl status mariadb.service
mysql
2、安全初始化
- 设置登陆密码
[root@localhost Desktop]# mysql_secure_installation #进入密码设置模式
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): #原来没有密码 不用输入
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] #是否设置超级用户密码 默认设置
New password: #输入密码
Re-enter new password: # 重复密码
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] #禁止匿名用户登录 默认禁止
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] #禁止超级用户远程登录 默认禁止
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] #默认删除测试数据库
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] #立即重新加载权限列表
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
mysql -uroot -p #使用超级用户输入密码登录数据库
- 设置关闭网络接口,设置跳过网络设置,拒绝外部通过网络访问数据库。
netstat -antlupe | grep mysql
vim /etc/my.cnf # 修改配置文件
skip-networking=1 # 跳过所有关于网络的设置
3、数据库管理
1)查询
SHOW DATABASES; #查询库信息
USE mysql; #进入某一个库
SHOW TABLES; # 查看该库中的所有表
SELECT * FROM user; # 查看某一表中的所有信息
SELECT Host FROM user WHERE User=‘root’; # 按条件查询表中信息
2)建立
SHOW DATABASES; # 查看库
CREATE DATABASE lala; # 新建一个库名称为lala
USE lala; # 进入新建的库
CREATE TABLE toto ( #在库中创建一个新的表
-> username varchar(20) not null, #表中包含的字段以及字段的要求
-> password varchar(30) not null
-> );
DESC toto; # 查看表结构
INSERT INTO toto VALUES(‘lele’,‘123456’); #给表中插入内容
INSERT INTO toto VALUES(‘haha’,‘123456’);
SELECT * FROM toto;
SELECT username FROM toto; 查看表中某一字段的内容
3)更改
ALTER TABLE toto ADD class varchar(15) not null; #给表中添加一个字段,默认在最后添加。
ALTER TABLE toto DROP class; #移除表中某一字段
ALTER TABLE toto ADD class varchar(15) not null AFTER username;
在确定的位置添加某一字段
UPDATE toto SET password=(‘123’) ; #修改某一字段的值,该字段所有值都会改变
UPDATE toto SET password=(‘456’) WHERE username=‘lele’; #在确定的位置修改
ALTER TABLE toto RENAME redhat; #更改表的名称
4) 删除
DELETE FROM redhat WHERE username=‘haha’; # 删除表中的某一行
DROP TABLE redhat; # 删除整个表
DROP DATABASE lala; #删除整个库
5)用户授权
CREATE USER TOTO@‘localhost’ identified by ‘123456’;
新建立的用户,虽然可以登陆该数据库,但是对数据库没有任何权限,看不到内容。
GRANT SELECT,INSERT ON lala. TO TOTO@localhost; # 进行授权
SHOW GRANTS FOR TOTO@localhost; #显示授给的权利*
** REVOKE INSERT ON lala.* FROM TOTO@localhost; #权力回收**
DROP USER TOTO@localhost; #删除用户
PLUSH PRIVILEGES; #刷新
4、数据库的备份
mysqldump -uroot -predhat lala > /opt/lala.sql #将库lala备份到/opt/lala.sql 文件
mysqldump -uroot -predhat lala --no-date # 对指定的库不备份数据只备份结构
mysqldump -uroot -predhat --all-database # 对所有的库进行备份
mysqldump -uroot -predhat --all-database --no-data # 对所有的库不备份数据只备份结构
然后删除数据库lala。
恢复方式1:
mysql -uroot -predhat -e “CREATE DATABASE lala;” #在数据库中先新建一个库
mysql -uroot -predhat lala < /opt/lala.sql #再将备份的数据放进新建的库中
恢复方式2:
vim /opt/lala.sql #直接打开备份的库文件
CREATE DATABASE lala;
USE lala;
mysql -uroot -predhat < /opt/lala.sql #由于已经在文件中指定出新建的库 直接将数据倒进即可
5、修改root用户密码
1)知道密码需要进行修改时:
update mysql.user set Password=password(‘westos’) where User=‘root’;
进入数据库直接使用命令对密码进行修改
systemctl restart mariadb.service #重启服务
2)当忘记 密码的时候重新进行设置密码
systemctl stop mariadb.service #关闭数据库服务
mysqld_safe --skip-grant-tables & #跳过验证列表进行登录 并后台运行
mysql #使用该命令直接进入数据库
update mysql.user set Password=password(‘redhat’) where User=‘root’;
输入命令对超级用户密码进行设置
killall -9 mysql #结束进程 相当于关闭服务
ps aux | grep mysql #查看没有结束的进程
kill -9 11383 #一次关闭所有没有停止的进程
systemctl start mariadb.service #开启服务
mysql -uroot -predhat #使用新密码成功登陆