mysql的基本设定
一、安装
yum install mariadb-server -y
systemctl start maridb
二、安全初始化
1、默认情况下,数据库接口(3306)是打开的,为了安全需要关闭此接口
vim /etc/my.cnf ##关闭网络接口
9 # instructions in http://fedoraproject.org/wiki/Systemd
10 skip-networking=1
11
12 [mysqld_safe]
systemtl start mariadb
测试:
2、数据库起始状态信是不安全的,需要做设置密码
[[email protected] logs]# mysql_secure_installation
3、密码的管理
改密码
[[email protected] logs]# mysqladmin -uroot -predhat password zhaoyan
[[email protected] logs]# mysql -uroot -pzhaoyan
Welcome to the MariaDB monitor. Commands end with ; or \g.
当超级用户忘记密码
[[email protected] logs]# systemctl stop mariadb.service
[[email protected] logs]# mysqld_safe --skip-grant-tables &
[1] 5733
[[email protected] logs]# mysql
MariaDB [(none)]> update mysql.user set Password=password('westos') where User='root'
-> ;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
关闭所有进程
[[email protected] logs]# ps aux | grep mysql
[[email protected] logs]# systemctl start mariadb
测试:
##p后接的redhat为密码
4、数据库的管理
MariaDB [(none)]> SHOW DATABASES; ##列出库
MariaDB [(none)]> CREATE DATABASE WESTOS; ##建立库
MariaDB [(none)]> USE WESTOS ##进入库
MariaDB [WESTOS]> CREATE TABLE linux(
-> username varchar(50) not null,
-> username varchar(50) not null
-> ); ##建立内容
MariaDB [WESTOS]> DESC linux ##查看表的结构
-> ;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(50) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
MariaDB [WESTOS]> INSERT INTO linux VALUES ('zhaoyan','111'); ##插入数据到linux表中
Query OK, 1 row affected (0.34 sec)
MariaDB [WESTOS]> SELECT * FROM linux; ##查询所有字段在linux表中
+----------+----------+
| username | password |
+----------+----------+
| zhaoyan | 111 |
+----------+----------+
1 row in set (0.00 sec)
MariaDB [WESTOS]> SELECT username from linux ##查询指定字段在linux表中
-> ;
+----------+
| username |
+----------+
| zhaoyan |
+----------+
)更改
MariaDB [WESTOS]> UPDATE linux SET password=password('123') where username='zhaoyan'; ##更改内容
MariaDB [WESTOS]> ALTER TABLE linux ADD class varchar(20); ##给文件里面添加相应内容
MariaDB [WESTOS]> ALTER TABLE linux DROP class; ##丢弃文件里面的部分内容
MariaDB [WESTOS]> ALTER TABLE linux ADD age varchar(20) AFTER password; ##在某一个指定的地方加入项目
MariaDB [WESTOS]> ALTER TABLE linux RENAME redhat; ##更改整个文件的名字
刷新命令
MariaDB [(none)]> flush privileges;
)删除
MariaDB [WESTOS]> DELETE FROM redhat where username='zhaoyan'; ##删除表中的某一行
MariaDB [WESTOS]> DROP TABLE redhat; ##删除某一个表
MariaDB [WESTOS]> DROP DATABASE WESTOS; ##删除某一个库
MariaDB [(none)]> SHOW DATABASES; ##查询,该库是否还存在
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
###数据所在的目录路径 /var/lib/mysql,在这个路径创建相应的东西,在mysql中也会有相应的展现。
###用户授权
1、建立这个用户
MariaDB [(none)]> DROP USER [email protected] #与@'%'的区别在于,'%'可以进行远程登陆,更加不安全。
MariaDB [(none)]> CREATE USER [email protected]'localhost' identified by 'westos'; #建立名为zhaoyan的用户,且登陆密码为westos
MariaDB [(none)]> GRANT SELECT,INSERT on westos.* TO [email protected]; #给zhaoyan用户SELECT,INSERT的权限
MariaDB [(none)]> SHOW GRANTS FOR [email protected]; #显示系统给与zhaoyan用户的权限
MariaDB [(none)]> REVOKE INSERT on westos.* FROM [email protected]; #移除zhaoyan的权限
MariaDB [(none)]> DROP USER [email protected]; #移除用户
5、数据库的备份
[[email protected] mysql]# mysqldump -uroot -pwestos westos > /mnt/westos.sql ##备份westos库中的内容
[[email protected] mysql]# mysqldump -uroot -pwestos westos --all-database > /mnt/westos.sql ##备份库中的所有内容及其架构
[[email protected] mysql]# mysqldump -uroot -pwestos westos --all-database --no-data > /mnt/westos.sql ##备份库中的架构
恢复方式1:
[[email protected] mysql]# mysql -uroot -pwestos -e "CREATE DATABASE westos;"
[[email protected] mysql]# mysql -uroot -pwestos westos < /mnt/westos.sql
恢复方式2:
vim /mnt/westos.sql
21 CREATE DATABASE westos;
22 USE westos;
[[email protected] mysql]# mysql -uroot -pwestos < /mnt/westos.sql
#6、安装phpmyadmin数据库图形管理
1、下载phpMyAdmin-3.4.0-all-languages.tar.bz2
2、yum install php -y
3、yum install php-mysql.x86_64 -y
4、systemctl restart httpd.service
5、cp phpMyAdmin-3.4.0-all-languages.tar.bz2 /var/www/html/
6、cd /var/www/html/
7、tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2
8、mv phpMyAdmin-3.4.0-all-languages mysqladmin
9、cd mysqladmin/
10、less README
11、less Documentation.txt
12、cp config.sample.inc.php config.inc.php
13、vim config.inc.php
14、 17 $cfg['blowfish_secret'] = '17c1ec07d65003'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
测试:
http://172.25.254.173/mysqladmin