MYSQL网络数据库
网络数据库为网络用户提供数据的存储,查询功能。
1.安装
yum install mariadb-server.x86_64 -y ##安装数据库
systemctl start mariadb ###开启数据库
2.安全初始化
默认情况下,数据库的网络接口是打开的,为了安全需要关闭此接口
vim /etc/my.cnf ##配置文件
skip-networking=1 ##关闭网络端口
systemctl restart mariadb ###开启
mysql_secure_installation ##设置密码
Enter current password for root (enter for none): ###进入设置密码
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
mysql -uroot -p ###用户登陆
3.数据库管理
改密码
mysqladim -uroot -predhat password westos ##知道原密码
超级用户忘记密码时
systemctl stop mariadb ###关闭数据库
mysqld_safe --skip-grant-tables & ###跳过mysql认证过程
mysql ###进入数据库
MariaDB [(none)]> update mysql.user set Password=password('westos') where User='root'; ###更改密码
ps aux |grep mysql ###查看进程
kill -9 mysql的所有进程id ###关闭数据库的进程
systemctl start mariadb ###开启
mysql -uroot -p ###登陆测试一下
【示例】
4.应用
*****建立***
MariaDB [(none)]> SHOW DATABASES; ###列出库
MariaDB [(none)]> CREATE DATABASE westos; ####建立库
MariaDB [(none)]> USE westos ###进入库
MariaDB [westos]> CREATE TABLE linux( ###建立表linux
-> username varchar(50) not null, ###写入字段(属性)字长50不能为空
-> password varchar(50) not null
-> );
MariaDB [westos]> DESC linux; ###查看表结构
MariaDB [westos]> INSERT INTO linux VALUES ('lee','123'); ##插入元组到表linux中
MariaDB [westos]> SELECT * FROM linux; ####查询所有字段在linux表中
MariaDB [westos]> SELECT username from linux; ####查询指定字段在linux表中
【示例】
*******更改******
MariaDB [westos]> ALTER TABLE linux ADD class varchar(20); ###增加class属性
MariaDB [westos]> UPDATE linux SET password=password('lee')where username='lee';
###更改lee用户的密码为加密字符lee;
MariaDB [westos]> ALTER TABLE linux DROP class; ###删除表中的class属性
MariaDB [westos]> ALTER TABLE linux ADD age varchar(10) AFTER username;
#####在username属性后添加class属性;
【示例】
*****删除****
MariaDB [westos]> DELETE FROM linux where username='lee'; ###删除lee元组
MariaDB [westos]> DROP TABLE linux; ###删除表
MariaDB [westos]> DROP DATABASE westos; ####删除库
MariaDB [westos]> flush privileges; ##刷新数据库
******用户授权******
MariaDB [(none)]> CREATE USER lee@'localhost' identified by 'westos';
##创建数据库登录用户为本地用户lee其密码为westos
MariaDB [(none)]> GRANT SELECT,INSERT on westos.* TO lee@localhost;
###给用户某些文件某些表格权限
MariaDB [(none)]> SHOW GRANTS FOR lee@localhost;
###显示此用的权限
MariaDB [(none)]> REVOKE INSERT ON westos.* FROM lee@localhost;
###去掉某些权限
5.数据库的备份
[root@localhost mysql]# mysqldump -uroot -pwestos westos > /mnt/westos1.sql
##备份westos库
[root@localhost mysql]# mysqldump -uroot -pwestos westos --no-data > /mnt/westos2.sql
##备份库westos时不备份数据
[root@localhost mysql]# mysqldump -uroot -pwestos --all-database > /mnt/westos3.sq
###备份所有数据库
恢复方式1
先创建库,再导入数据
[root@localhost mysql]# mysql -uroot -pwestos -e "CREATE DATABASE westos;" ##创建库
[root@localhost mysql]# mysql -uroot -pwestos westos < /mnt/westos1.sql ###导入数据
恢复方式2
修改备份的文件,再导入
[root@localhost mysql]# vim /mnt/westos1.sql ##备份的文件
21 CREATE DATABASE westos; ##修改的内容
22 USE westos;
[root@localhost mysql]# mysql -uroot -pwestos < /mnt/westos1.sql ##导入
【示例】
【21,22行】
6.MYSQL的图形化管理
PHPMYadmin是一个使用php编写的,基于web的mysql客户端程序,使用时要保证apache的正常运行。
[root@localhost html]# tar jxf /var/lib/mysql/phpMyAdmin-3.4.0-all-languages.tar.bz2 ##安装
[root@localhost html]# mv phpMyAdmin-3.4.0-all-languages mysqlamin ##便于测试改名
[root@localhost html]# cd mysqlamin
[root@localhost mysqlamin]# cp config.sample.inc.php config.inc.php ###配置文件用其范例的模版
[root@localhost mysqlamin]# vim config.inc.php ##配置文件更改用户认证方式
【认证方式】
【测试】登陆时需要用户以及密码
7.安装自己的论坛--LAMP平台的搭建
L(linux) A(apache) M(mysql) P(php)
[root@localhost html]# unzip Discuz_X3.2_SC_UTF8.zip #安装一个论坛模版
[root@localhost html]# chmod 777 upload/ -R ##数据文件加权限以便可以被程序读写
[root@localhost html]# setenforce 0 ##关闭SELinux
【示例】论坛安装向导
【数据库安装填写相关信息】
【安装完成,可通过ip访问此php网站】