[root@server0 ~]# mysql -uroot
MariaDB [(none)]> SHOW DATABASES;
MariaDB [(none)]> QUIT
为数据库账号修改密码:
mysqladmin [-u用户名] [-p[旧密码]] password '新密码'
导入/恢复到数据库:
mysql [-u用户名] [-p[密码]] 数据库名 < 备份文件.sql
为数据库用户授权/撤销权限:
grant 权限1,权限2... on 库名.表名 to 用户名@客户机地址 identified by '密码';
revoke 权限1,权限2... on 库名.表名 from 用户名@客户机地址;
表记录增删改查:
insert into [库名.]表名 values(值1,值2,值3);
delete from [库名.]表名 where ...;
update [库名.]表名 set 字段名=字段值 where ....;
select 字段列表 from [库名.]表名 where 字段名1=值 and|or 字段名2=值;
统计查询结果的数量:
select count(*) from [库名.]表名 where .. ..;
[root@localhost html]# mysql -uroot -p
Enter password:
MariaDB [(none)]>
MariaDB [(none)]> show databases; 查看当前服务器中有那些库。
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbsdb |
| mysql |
| performance_schema |
| test |
| ultrax |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> use mysql;
MariaDB [mysql]> show tables; 查看当前使用的库中有哪些表。
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
MariaDB [mysql]> describe user; 查看表的结构。
MariaDB [(none)]> use auth;
Database changed
MariaDB [auth]>
MariaDB [(none)]> use auth;
Database changed
MariaDB [auth]> create table users (user_name char(16) not null,user_passwd char(48) default '',primary key (user_name));
Query OK, 0 rows affected (0.09 sec)
MariaDB [auth]> show tables;
+----------------+
| Tables_in_auth |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)
MariaDB [auth]>
MariaDB [auth]> desc users;
+-------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| user_name | char(16) | NO | PRI | NULL | |
| user_passwd | char(48) | YES | | | |
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [auth]>
MariaDB [auth]> desc auth.users;
+-------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| user_name | char(16) | NO | PRI | NULL | |
| user_passwd | char(48) | YES | | | |
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)
MariaDB [auth]> drop table auth.users;
MariaDB [auth]> show databases; 查看当前服务器中有哪些库。
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| bbsdb |
| mysql |
| performance_schema |
| test |
| ultrax |
+--------------------+
7 rows in set (0.00 sec)
MariaDB [auth]> drop database auth;
MariaDB [auth]> insert into users(user_name,user_passwd) values('zhangsan',password ('123456'));
Query OK, 1 row affected (0.03 sec)
MariaDB [auth]> insert into users values ('lisi',password('654321'));
Query OK, 1 row affected (0.04 sec)
MariaDB [auth]> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *2A032F7C5BA932872F0F045E0CF6B53CF702F2C5 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [auth]> select user_name,user_passwd from auth.users where user_name='zhangsan';
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [auth]>
MariaDB [auth]> update auth.users set user_passwd=password('') where user_name='lisi';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [auth]> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [auth]>
MariaDB [auth]> delete from auth.users where user_name='lisi';
Query OK, 1 row affected (0.04 sec)
MariaDB [auth]>
MariaDB [auth]> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [auth]>
MariaDB [auth]> select user,host,password from mysql.user where user='';
+------+-----------------------+----------+
| user | host | password |
+------+-----------------------+----------+
| | localhost | |
| | localhost.localdomain | |
+------+-----------------------+----------+
2 rows in set (0.00 sec)
MariaDB [auth]>
MariaDB [auth]> grant select on auth.* to 'xiaoqi'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
[root@localhost html]# mysql -u xiaoqi -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> select * from mysql.user;
ERROR 1142 (42000): SELECT command denied to user 'xiaoqi'@'localhost' for table 'user'
MariaDB [(none)]>
MariaDB [(none)]> create database bdqn;
Query OK, 1 row affected (0.00 sec)
授予权限。
MariaDB [(none)]> grant all on bdqn.* to 'dbuser'@'192.168.4.19' identified by 'pwd@123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> show grants for 'dbuser'@'192.168.4.19';查看权限
MariaDB [(none)]> revoke all on auth.* from 'xiaoqi'@'localhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
备份数据库。
[root@localhost beifen]# mysqldump -u root -p mysql user > mysql-user.sql
[root@localhost beifen]# mysqldump -u root -p --database auth > auth.sql
[root@localhost beifen]# mysqldump -u root -p --opt --all-databases > all-data.sql
[root@localhost beifen]# cat mysql-user.sql
[root@localhost beifen]# grep -v '^--' auth.sql | grep -v '^/' | grep -v '^$'
恢复数据库
[root@localhost beifen]# mysql -u root -p test < mysql-user.sql
Enter password:
[root@localhost beifen]# mysql -u root -p
Enter password:
MariaDB [(none)]>
MariaDB [(none)]> use test;
MariaDB [test]> show tables;
[root@localhost beifen]# mysql -u root -p < ./all-data.sql
[root@localhost beifen]# mysql -u root -p
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| bbsdb |
| bdqn |
| mysql |
| performance_schema |
| test |
| ultrax |
+--------------------+
=========================================================
==============================================================
[root@svr7 ~]# yum -y install httpd mariadb-server mariadb php php-mysql
2.配置MySQL
[root@svr7 ~]# systemctl restart mariadb
[root@svr7 ~]# systemctl enable mariadb.service
[root@svr7 ~]# mysqladmin -u root password 'Taren1'
3.配置Httpd
[root@svr7 ~]# vim /var/www/html/test1.php
phpinfo();
?>
[root@svr7 ~]# vim /var/www/html/test2.php
$link=mysql_connect('localhost','root','Taren1');
if($link) echo "Success !!"; //成功则显示Success !!
else echo "Failure !!"; //失败则显示Failure !!
mysql_close(); //关闭数据库连接
?>
5.启动服务
[root@svr7 ~]# systemctl start httpd.service
6.测试
[root@pc205 ~]# firefox http://127.0.0.1/test1.php
[root@pc205 ~]# firefox http://127.0.0.1/test2.php
[root@svr7 ~]# systemctl restart httpd.service
实验五:PHP应用部署(Discuz!论坛系统)
1.建论坛库
[root@svr7 ~]# mysql -uroot -p
Enter password: //验证管理密码
mysql> create database bbsdb;//创建bbsdb数据库
mysql> show databases;//查看数据库
mysql> grant all on bbsdb.* to runbbs@localhost identified by 'pwd123';//授权数据库
mysql> quit
2.部署论坛网页代码
[root@svr7 ~]# unzip Discuz_X3.4_SC_UTF8.zip
[root@svr7 ~]# ls upload/
[root@svr7 ~]# cp -rf upload/* /var/www/html/
[root@svr7 ~]# cd /var/www/html/
[root@svr7 ~]# chown -R apache template/ config/ data/ uc_client/ uc_server/
selinux安全机制。
[root@localhost html]# getenforce
Enforcing
[root@localhost html]# setenforce 0
[root@localhost html]# ls
[root@localhost html]# getenforce
Permissive
3.安装论坛系统
[root@pc207 ~]# firefox http://127.0.0.1/
4.访问论坛前台首页 http://127.0.0.1/
微信 269611002 一起交流吧