饭随笔

导航

mysql操作

mysql启动:

  mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/var/lib/mysql

mysql关闭:

  mysqladmin -S /tmp/mysql${port}.sock shutdown  -u$user -p$password

创建系统表:

  mysql_install_db --user=mysql --datadir=/var/lib/mysql/

修改mysql的端口:

  修改/etc/my.cnf文件,在[mysqld]下 添加 port=3303

[mysqld]
datadir=/var/lib/mysql3303
socket=/var/lib/mysql3303/mysql.sock
port=3303
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld3303.log
pid-file=/var/run/mysqld/mysqld3303.pid
~

 访问mysql:

  mysql -S /var/lib/mysql3303/mysql.sock -P 3303  -u user -p

创建数据库的用户:

  GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@'192.168.0.%' IDENTIFIED BY 'p4ssword';

  GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

   flush privileges;

 

修改表名称    
ALTER  TABLE product_info RENAME TO product_v4_info

创建表
CREATE TABLE `node` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `ip_in` varchar(15) NOT NULL COMMENT '内网IP',
  `ip_ex` varchar(15) DEFAULT NULL COMMENT '外网IP',
  `cmdb` varchar(100) NOT NULL COMMENT 'cmdb盘点号',
  `idc_id` int(11) NOT NULL COMMENT 'IDC',
  `section_id` varchar(10) DEFAULT '',
  `env` varchar(16) NOT NULL COMMENT '环境名',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `init_status` tinyint(1) DEFAULT '0',
  `annotating` tinyint(1) NOT NULL DEFAULT '0',
  `nimsg` varchar(500) DEFAULT '',
  `in_cfengine` tinyint(1) NOT NULL DEFAULT '1',
  `in_pingmon` tinyint(1) NOT NULL DEFAULT '1',
  `param` mediumtext,
  `desp` mediumtext NOT NULL,
  `timeline` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `is_repairing` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `ipin` (`ip_in`),
  KEY `ipex` (`ip_ex`)
) ENGINE=InnoDB AUTO_INCREMENT=8141 DEFAULT CHARSET=utf8;

删除主键
Alter table cpunit_module drop primary key

添加列
alter table 表名 add column 列名 varchar(30);

修改列的类型
ALTER TABLE chatter_users MODIFY COLUMN ip VARCHAR(50);

修改列的名称
alter table user CHANGE new1 new4 int;  //修改一个字段的名称,此时一定要重新指定该字段的类型

 

给列值添加唯一限制。

ALTER TABLE `perm_app_interface` ADD unique(`url`);

 

删除主键
Alter table tb drop primary key;

初始化系统表
scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/var

后台启动mysql
nohup mysqld -u root --defaults-file=/etc/my3307.cnf  >/dev/null 2>&1 &
nohup /usr/libexec/mysqld --defaults-file=/etc/my.cnf --user=mysql --datadir=/usr/local/mysql  &

//创建用户
mysql> insert into mysql.user(Host,User,Password) values("localhost","phplamp",password("1234"));
//刷新系统权限表
mysql>flush privileges;

手动关闭数据库
mysqladmin -uroot -p shutdown
若/etc/my.cnf配置文件中没有skip-grant-tables,这需要权限验证:需要使用root用户,需要密码。若有skip-grant-tables,则无需密码,所有用户都可以执行关闭操作。

http://dev.mysql.com/doc/refman/5.6/en/server-administration.html

posted on 2014-03-17 18:16  饭随笔  阅读(251)  评论(0编辑  收藏  举报