[[email protected] ~]# yum install mariadb-server -y ##安装mysql服务器
[[email protected] ~]# vim /etc/my.cnf
10 skip-networking=1
[[email protected] ~]# systemctl restart mariadb
[[email protected] ~]# mysql_secure_installation ##第一次安装mysql以后通过这条命令可以对mysql进行设置
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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] y ##设置密码(westos)
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] Y ##是否关闭匿名用户登陆权限
... 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] Y##是否允许root与远程登陆
... 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] Y##刷新数据库
- 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] Y
... 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!
[[email protected] ~]# systemctl start mariadb ##开启服务
[[email protected] ~]# mysql -uroot -pwestos ##从本机登录mysql数据库
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; ##显示数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> select * from user; ##查询user表中的所有字段(*可以用此表中的任何字段来代替)
+---------------------+
| Host |
+---------------------+
| 127.0.0.1 |
| ::1 |
| localhost |
| localhost |
| mariadb.example.com |
| mariadb.example.com |
+---------------------+
6 rows in set (0.00 sec)
+---------------------+------+
| Host | User |
+---------------------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | |
| localhost | root |
| mariadb.example.com | |
| mariadb.example.com | root |
+---------------------+------+
6 rows in set (0.00 sec)
+---------------------+------+----------+
| Host | User | password |
+---------------------+------+----------+
| localhost | root | |
| mariadb.example.com | root | |
| 127.0.0.1 | root |
| ::1 | root | |
| localhost | | |
| mariadb.example.com | | |
+---------------------+------+----------+
6 rows in set (0.00 sec)
MariaDB [mysql]> select Host,User,password,Select_priv from user;
+---------------------+------+----------+-------------+
| Host | User | password | Select_priv |
+---------------------+------+----------+-------------+
| localhost | root | | Y |
| mariadb.example.com | root | | Y |
| 127.0.0.1 | root | | Y |
| ::1 | root | | Y |
| localhost | | | N |
| mariadb.example.com | | | N |
+---------------------+------+----------+-------------+
6 rows in set (0.00 sec)
MariaDB [mysql]> desc user; ##查询user表中的结构,显示所有字段的名称
+---------------------------+
| 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)
[[email protected] mysql]# mysql -uroot -pwestos ##登录
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database westos; ##创建westos库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases; ##查询库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
4 rows in set (0.00 sec)
Database changed
MariaDB [westos]> show tables; ##显示westos库中的表
Empty set (0.00 sec)
MariaDB [westos]> create table linux( username varchar(15) not null, password varchar(50) not null );##创建linux表,并且linux表含有两个字段,username,password,username字段,字符长度最大为15个,并且不能为空
Query OK, 0 rows affected (0.41 sec)
MariaDB [westos]> desc linux; ##查看linux表中的数据结构
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
Query OK, 1 row affected (0.09 sec)
MariaDB [westos]> select * from linux; ##查询inux表中的所有字段
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
+----------+----------+
1 row in set (0.00 sec)
Query OK, 1 row affected (0.34 sec)
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+
| username | password |
+----------+-------------------------------------------+
| user1 | 123 |
| user2 | 123 |
| user3 | 123 |
| user4 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+----------+-------------------------------------------+
4 rows in set (0.00 sec)
MariaDB [westos]> update linux set password=password('123') where username='user1';##更新user1的密码,将user1密码改成加密型
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+
| username | password |
+----------+-------------------------------------------+
| user1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| user2 | 123 |
| user3 | 123 |
| user4 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+----------+-------------------------------------------+
4 rows in set (0.00 sec)
MariaDB [westos]> delete from linux where username='user1';##删除user1的信息
Query OK, 1 row affected (0.34 sec)
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+
| username | password |
+----------+-------------------------------------------+
| user2 | 123 |
| user3 | 123 |
| user4 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+----------+-------------------------------------------+
3 rows in set (0.00 sec)
Query OK, 3 rows affected (0.48 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [westos]> select * from linux;##查询linux表格中的数据
+----------+-------------------------------------------+------+
| username | password | age |
+----------+-------------------------------------------+------+
| user2 | 123 | NULL |
| user3 | 123 | NULL |
| user4 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL |
+----------+-------------------------------------------+------+
3 rows in set (0.00 sec)
Query OK, 3 rows affected (0.55 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+------+-------+------+
| username | password | name | class | age |
+----------+-------------------------------------------+------+-------+------+
| user2 | 123 | NULL | | NULL |
| user3 | 123 | NULL | | NULL |
| user4 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL | | NULL |
+----------+-------------------------------------------+------+-------+------+
3 rows in set (0.00 sec)
Query OK, 3 rows affected (0.41 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+------+-------+
| username | password | name | class |
+----------+-------------------------------------------+------+-------+
| user2 | 123 | NULL | |
| user3 | 123 | NULL | |
| user4 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL | |
+----------+-------------------------------------------+------+-------+
3 rows in set (0.00 sec)
5.删除数据库
MariaDB [westos]> delete from linux where username='user2';##从linux表中删除user1数据
Query OK, 1 row affected (0.33 sec)
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+------+-------+
| username | password | name | class |
+----------+-------------------------------------------+------+-------+
| user3 | 123 | NULL | |
| user4 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL | |
+----------+-------------------------------------------+------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+------+-------+
| username | password | name | class |
+----------+-------------------------------------------+------+-------+
| user3 | 123 | NULL | |
| user4 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL | |
+----------+-------------------------------------------+------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> drop table linux;##删除linux表
Query OK, 0 rows affected (0.35 sec)
MariaDB [westos]> select * from linux;
ERROR 1146 (42S02): Table 'westos.linux' doesn't exist
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> use westos
ERROR 1049 (42000): Unknown database 'westos'
[[email protected] ~]# mysqldump -u root -pwestos --all-database##备份所有表中的所有数据
[[email protected] ~]# mysqldump -u root -pwestos --all-database --no-data##备份所有表但不备份数据
[[email protected] ~]# mysqldump -u root -pwestos westos > /mnt/westos.sql##备份westos库并把数据导到/mnt/westos.sql中
[[email protected] ~]# mysql -uroot -pwestos -e "drop database westos;"##删除westos数据库
[[email protected] ~]# mysql -uroot -pwestos -e "show databases;"##显示数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[[email protected] ~]# mysqldump -u root -pwestos westos < /mnt/westos.sql##将数据导入westos库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
[[email protected] ~]# mysql -uroot -pwestos -e "show tables from westos;"(没有内容)
[[email protected] ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 30
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use westos
Database changed
MariaDB [westos]> desc linux;
ERROR 1146 (42S02): Table 'westos.linux' doesn't exist
MariaDB [westos]> create table linux( username varchar(15) not null, password varchar(50) not null );##新建linux表格
Query OK, 0 rows affected (0.38 sec)
MariaDB [westos]> desc linux;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> quit
[[email protected] ~]# mysqldump -u root -pwestos westos linux > /mnt/linux.sql##备份westos库中的linux表格
[[email protected] ~]# mysql -uroot -pwestos -e "drop database westos;" ##删除westos库
[[email protected] ~]# mysql -uroot -pwestos -e "create database westos;" ##创建westos库
[[email protected] ~]# mysql -u root -pwestos westos < /mnt/linux.sql ##恢复linux表格
+------------------+
| Tables_in_westos |
+------------------+
| linux |
+------------------+
7.用户授权
MariaDB [(none)]> create user [email protected] identified by 'lee';##建立用户lee,此用户只能通过本机登录
MariaDB [(none)]> create user [email protected]'%' identified by 'lee'; ##建立用户lee,从用户可以通过网络登录
[[email protected] ~]# mysql -ulee -plee
[[email protected] ~]# mysql -ulee -plee -h 172.25.254.234 ##配置文件写入跳过网络则网络无法登录
ERROR 2003 (HY000): Can't connect to MySQL server on '172.25.254.234' (111)
10 skip-networking=0
[[email protected] ~]# systemctl restart mariadb ##重启服务
[[email protected] ~]# mysql -ulee -plee -h 172.25.254.234 ##通过网络登录
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement
MariaDB [(none)]> grant insert,update,delete,select on westos.* to [email protected];##用户授权
MariaDB [(none)]> show grants for [email protected];##查看用户授权
+------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lee'@'localhost' IDENTIFIED BY PASSWORD '*9BB439A3A652A9DAD3718215F77A7AA06108A267' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `westos`.* TO 'lee'@'localhost' |
+------------------------------------------------------------------------------------------------------------+
+----------------------------------------------------------------------------------------------------+
| Grants for [email protected]% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lee'@'%' IDENTIFIED BY PASSWORD '*9BB439A3A652A9DAD3718215F77A7AA06108A267' |
| GRANT SELECT ON `westos`.* TO 'lee'@'%' |
+----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> quit
Bye
MariaDB [(none)]> revoke delete on westos.linux from [email protected];##去掉用户授权权力
MariaDB [(none)]> drop user [email protected]'%'; ##删除用户
Query OK, 0 rows affected (0.00 sec)
8.超级用户忘记密码
[[email protected] ~]# mysqladmin -uroot -pwestos password lee ##修改密码
[[email protected] ~]# mysql -uroot -pwestos ##密码错误无法登录
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[[email protected] ~]# mysqld_safe --skip-grant-tables & ##开启mysql接口并忽略授权表
[1] 5074
[[email protected] ~]# 170515 07:55:02 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
170515 07:55:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql ##回车
[[email protected] ~]# mysql ##直接不要密码可以登录
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> update mysql.user set Password=password('westos') where User='root';##更改超级用户密码信息
Query OK, 1 row affected (0.00 sec)
Rows matched: 3 Changed: 1 Warnings: 0
MariaDB [(none)]> quit
Bye
[1]+ Killed mysqld_safe --skip-grant-tables
[[email protected] ~]# ps aux | grep mysql ##过滤mysql所有进程
mysql 5229 0.0 9.6 859068 98172 pts/1 Sl 07:55 0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 5528 0.0 0.0 112640 940 pts/1 R+ 08:05 0:00 grep --color=auto mysql
[[email protected] ~]# kill -9 5229 ##结束进程
[[email protected] ~]# ps aux | grep mysql
root 5543 0.0 0.0 112640 936 pts/1 R+ 08:06 0:00 grep --color=auto mysql
[[email protected] ~]# systemctl start mariadb ##开启服务
[[email protected] ~]# mysql -uroot -pwestos ##修改后的密码登录
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
[[email protected] ~]# yum install httpd php php-mysql.x86_64 -y##安装相关服务,php网页工具,php-mysql连接数据库与php
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
[[email protected] ~]# systemctl disable firewalld
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/
dbus-org.fedoraproject.FirewallD1.service'
[[email protected] mnt]# cd /var/www/html/
[[email protected] html]# ls
phpMyAdmin-3.4.0-all-languages
[[email protected] html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin##重命名
[[email protected] mysqladmin]# cp -p config.sample.inc.php config.inc.php##复制配置文件模版
[[email protected] mysqladmin]# vim config.inc.php
17 $cfg['blowfish_secret'] = 'mysql'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
测试
访问http://172.25.254.234/mysqladmin0