mysql常用操作

傻男人 1年前 ⋅ 939 阅读

mysql的操作命令

一、安装

一键安装的脚本

链接:https://pan.baidu.com/s/1Q3I3npfJUAncOznRi1aFAQ

提取码:wkmx

二、常规操作

检查是否通过rpm的方式安装过mysql

rpm -qa | grep -i mysql

远程登录

mysql -h 127.0.0.1 -P3306 -uroot -p

更改密码

set password for root@localhost = password('new-password');

配置用户远程都可以连接数据库

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

配置用户只能通过192.168.1.16连接数据库

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.16' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

修改mysql默认的结束符号,默认是分号;但是在函数和存储过程中会使用到分号导致解析不正确

delimiter $$

启动

service mysqld start或者/etc/init.d/mysqld start

重启

service mysqld restart

跳过授权

1、vim /etc/my.cnf
2、在[mysqld]下加入skip-grant-tables
3、重启mysql

设置mysql的开机启动

chkconfig --add mysqld;
chkconfig mysqld on;

查看mysql的日志

tail -f /var/log/mysqld.log

三、主从配置

主库操作

配置文件修改
sh vim /etc/my.cnf
[mysqld]
port=13063
server-id=1
# bin log config
log-bin=mysql-bin-172-16-10-150#开启mysql的binlog日志功能
# sync-binlog=1#控制数据库的binlog刷到磁盘上去 , 0 不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
binlog-format=mixed#binlog日志格式,mysql默认采用statement,建议使用mixed
# expire-logs_days=5#binlog过期清理时间
# max-binlog-size=200m#binlog每个日志文件大小
# binlog-cache-size = 8m#binlog缓存大小
# max-binlog-cache-size= 512m#最大binlog缓存大
max_binlog_size = 100m
# #不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
binlog-ignore-db=mysql
# #同步的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
binlog-do-db=process
# slave-skip-errors = all #跳过从库错误
# # 自增值的偏移量
# #auto-increment-offset = 1
# # 自增值的自增量
# #auto-increment-increment = 1 
重启数据库
创建访问的用户并授权

CREATE USER slave IDENTIFIED BY 'slave_password';

赋予该用户复制的权利
grant replication slave on *.* to 'slave'@'172.16.20.150'  identified by 'slave_password';
FLUSH PRIVILEGES;
检查状态
mysql> show master status;
+--------------------------------+----------+--------------+------------------+-------------------+
| File                           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------------------+----------+--------------+------------------+-------------------+
| mysql-bin-172-16-10-150.000001 |      154 | process      | mysql            |                   |
+--------------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从库操作

配置文件修改
sh vim /etc/my.cnf
[mysqld]
log-bin=master-a-bin #日志文件名字
binlog-format=MIXD  #二进制日志的格式  有row/statement/mixd三种类型
server-id=2  #要求各个服务器的这个id必须不一样
log-slaver-updates=true
relay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
配置从库

CHANGE MASTER TO MASTER_HOST='172.16.10.150', MASTER_USER='slave',MASTER_PASSWORD='slave_password',MASTER_LOG_FILE='mysql-bin-172-16-10-150.000001', MASTER_LOG_POS=729;

开启从服务器

start slave;

查看从的状态
mysql> show slave status \G; 
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 172.16.10.150
                  Master_User: slave
                  Master_Port: 13063
                Connect_Retry: 60
              Master_Log_File: mysql-bin-172-16-10-150.000001
          Read_Master_Log_Pos: 729
               Relay_Log_File: localhost-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin-172-16-10-150.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0

全部评论: 0

    我有话说: