mysql管理

数据库服务管理

安装完成后,启动mysql服务器
systemctl start mysqld
然后查看mysql状态
systemctl status mysqld

发现报错,因为centos不再支持MySQL数据库,安装mariadb代替

yum install –y mariadb-server
  
会在 /var/log/mysqld.log文件中会自动生成一个随机的密码,用于安装后登录mysql,查看该随机密码。
  grep “temporary password” /var/log/mysqld.log

安装完mysql,如果用于生产环境,最好执行mysql_secure_installation来做一些常规化安全设置。
  [root@server1 ~]# mysql_secure_installation
  NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, 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 MySQL
root user without the proper authorisation.
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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.

查看密钥:kEl.Lb5tr>9,

自己设置的数据库密码 :qaz!123456A.

Remove anonymous users? [Y/n] <– 是否删除匿名用户,生产环境建议删除

… 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] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… Success!
By default, MySQL 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] <– 是否删除test数据库
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] <– 是否重新加载权限表
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!


数据库和表相关操作

对库的操作  

安装mysql之后,系统会自动创建一些数据库,分别为information_schema、mysql、performance_schema、sys。

需要注意的是,不要对这些系统创建的数据库进行修改或者其他操作,否则可能会造成mysql使用异常,严重的会造成数据库不能使用。
创建数据库的命令为: create  database  数据库名;

删除数据库的命令为:drop  database   数据库名;

对表的操作

操作表,主要包括表的创建、修改和删除等。其中表的的修改有包括增减列、修改列、修改表名等。
  首先讲一讲需要使用命令查询数据库的所有表的操作,命令为:show tables;

表的创建

创建语句如下:
create  table   表名(
列名1    列类型1,
列名2    列类型2,
         ..........
列名n    列类型n);

查看表结构的命令

desc  表名;

值得一提的是,这里没有定义key,以及说明字段是否为空和默认值等
  修改表之增减列
  首先需要说的是修改表的命令结构为:
alter   table   表名
xxxxxxxxxx;
第一行都是相同的,第二行的写法不同。

增加列的命令:
alter   table   表名
add  新列名  新列类型;

注意mysql中的sql语句书写规范,命令行以分号结尾,语句可以多行书写
删除列的命令为:
alter   table   表名
drop   列名;

修改表之修改列


包括修改列类型和修改列名(同时伴随类型的修改)


修改列类型的命令

alter   table   表名
modify   列名   列新类型;

修改列名的命令

alter   table   表名
change    列名    新列名    新列类型;

修改表名称


修改表名称的命令为:
alter   table   表名
rename   to   新表名;

删除表


删除表的命令为:
drop   表名;

以上即为mysql常用的对库和表的操作语句,需要在实际的使用中不断地学习。

数据库用户管理

添加用户


  以root用户登录数据库,运行以下命令:
  create user zhangsan identified by 'zhangsan';
  上面的命令创建了用户zhangsan,密码是zhangsan。在mysql.user表里可以查看到新增用户的信息:

用户授权


  命令格式:grant privilegesCode on dbName.tableName to username@host identified by "password";
grant all privileges on zhangsanDb.* to zhangsan@'%' identified by 'zhangsan';
flush privileges;
  上面的语句将zhangsanDb数据库的所有操作权限都授权给了用户zhangsan。
  在mysql.db表里可以查看到新增数据库权限的信息:

https://images2015.cnblogs.com/blog/595137/201705/595137-20170511101449051-92414488.png

修改密码


  运行以下命令可以修改用户密码
update mysql.user set password = password('zhangsannew') where user = 'zhangsan' and host = '%';
flush privileges;


删除用户

运行以下命令可以删除用户:
drop user zhangsan@'%';
drop user命令会删除用户以及对应的权限,执行命令后你会发现mysql.user表和mysql.db表的相应记录都消失了。

  • 17
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值