首页 > 代码库 > 三.数据库

三.数据库

####数据库####
1.安装数据库
yum install mariadb-server.x86_64 -y
2.查看网络端口
netstat -antlpe | grep mysql

vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
skip-networking=1   ##添加这一行
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

3.修改数据库的密码
mysql_secure_installation
systemctl restart mariadb

mysql -uroot -p       从本机登录mysql数据库

show databases;      显示数据库
use mysql;           进入数据库
desc user;           查看user表的数据结构
flush privileges;    刷新数据信息
select host,user,password from user; 查询user表中的host,user,password字段

create database westos;  创建westos数据库
use westos;
create table linux(
username varchar(15) not null,
password varchar(15) not null
);
select * from mysql.user;                   查询mysql库下的user表数据
alter table linux add age varchar(4);  查询age字段到linux表中
ALTER TABLE linux DROP age             删除age字段
ALTER TABLE linux ADD age VARCHAR(5) AFTER name 在name字段后添加字段age



show tables;
desc linux;

insert into linux values (‘user1‘,‘passwd1‘);             在linux表中插入值为username = user1,password = password1
uplete linux set password=password(‘passwd2‘) where username=user1; 更新linux表中user1的密码为password2
delete from linux where username=user1;                    删除linux表中user1的所有内容

grant select on *.* to user@localhost identified by ‘passwd1‘;  
 授权user1 密码为passwd1 并且只能在本地 查询数据库的所有内容

4.用户和访问权限
创建用户
CREATE USER wxh@localhost identified by ‘westos‘;  ##密码为westos
CREATE USER lee@‘%‘identified by ‘redhat‘;

用户授权
GRANT INSERT,UPDATE,DELETE,SELECT on *.* to USER westos@localhost;

重载授权表
FLUSH PRIVILEGES;

查看用户授权
SHOW GRANTS FOR wxh@localhost;

撤销用户权限
REVOKE DELETE,INSERT,UPDATE on *.* from wxh@localhost;

删除用户
DROP USER wxh@localhost;
 

yum install mysql mysql-server
mysqladmin -uroot -predhat password westos            修改本地mysql root密码
mysqladmin -uroot -predhat -h 192.168.0.188 password westos    修改远程192.168.0.188 mysql服务器 root密码
    
mysql_secure_installation                    第一次安装mysql以后通过这条命令可以对mysql进行设置

mysql -uroot -predhat                        从本机登录mysql数据库




5.忘了数据库密码怎么办?
mysqld_safe --skip-grant-tables &                    跳过grant-tables授权表  不需要认证登录本地mysql数据库

update mysql.user set password=password(‘westos‘) where user=‘root‘;    更新mysql.user 表中条件为root用户的密码为加密westos

mysql -uroot
fg
killall -9 mysqld_safe
ps aux | grep mysql
systemctl start mysql
systemctl start mariadb
mysql -uroot -predhat

5.备份与恢复
备份
mysqldump -uroot -predhat westos > westos.dump
mysqldump -uroot -predhat --all-databases > backup.dump
mysqldump -uroot -predhat --no-data westos> westos.dump
恢复
mysqladmin -uroot -predhat create db2
mysql -urooot -predhat db2 < westos.dump

备份
/var/lib/mysql
mysqldump -uroot -predhat mysql > mysql.bak    备份mysql库到mysql.bak
mysql -uroot -predhat westos < mysql.bak    恢复mysql.bak 到westos库

6.图形mysql
phpmyadmin
yum install php php-mysql httpd mysql mysql-server

tar jxf phpmyadmin-*.tar.bz2 -C /var/www/html
mv phpmyadmin phpadmin
cp config.sample.inc.php config.inc.php

vim config.inc.php
add
------------------------------
$cfg[‘blowfish_secret‘] = ‘test‘;
------------------------------


本文出自 “12100661” 博客,谢绝转载!

三.数据库