Centos 7 mysql 安装使用记

某次把美团云1G 1核 centos 7 装到死机,明白了源码编译安装mysql是个大坑,遂绕路到其他大道。

  • 安装命令
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

rpm -ivh mysql-community-release-el7-5.noarch.rpm

yum install mysql-community-server
  • 配置mysql
service mysqld restart

mysql -u root
mysql> set password for 'root'@'localhost' =password('vm_52.102');    

 

[root@localhost liuhui]# service mysqld restart
Redirecting to /bin/systemctl restart  mysqld.service
[root@localhost liuhui]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password for 'root'@'localhost' =password('password');
Query OK, 0 rows affected (0.00 sec)

正常进程

[root@localhost liuhui]# ps aux|grep mysqld
mysql     59735  0.0  0.0 113124  1580 ?        Ss   16:18   0:00 /bin/sh /usr/bin/mysqld_safe
mysql     59900  0.1  6.3 698924 118004 ?       Sl   16:18   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock

 ===============================================

【安装过程中的错误】

1、软件包依赖 mariadb组件

错误:软件包:akonadi-mysql-1.9.2-4.el7.x86_64 (@anaconda)
          需要:mariadb-server
          正在删除: 1:mariadb-server-5.5.44-2.el7.centos.x86_64 (@anaconda)
              mariadb-server = 1:5.5.44-2.el7.centos
          取代,由: mysql-community-server-5.6.34-2.el7.x86_64 (mysql56-community)
              未找到
          更新,由: 1:mariadb-server-5.5.50-1.el7_2.x86_64 (updates)
              mariadb-server = 1:5.5.50-1.el7_2
          可用: 1:mariadb-server-5.5.47-1.el7_2.x86_64 (updates)
              mariadb-server = 1:5.5.47-1.el7_2
 您可以尝试添加 --skip-broken 选项来解决该问题
 您可以尝试执行:rpm -Va --nofiles --nodigest

 解决:移除对 mariadb-libs的依赖

yum -y remove mariadb-libs

2、在阿里云机器上遇到  libc.so.6(GLIBC_2.17)依赖问题

Error: Package: mysql-community-client-5.6.35-2.el7.x86_64 (mysql56-community)
           Requires: libc.so.6(GLIBC_2.17)(64bit)
Error: Package: mysql-community-libs-5.6.35-2.el7.x86_64 (mysql56-community)
           Requires: libc.so.6(GLIBC_2.17)(64bit)
Error: Package: 2:postfix-2.6.6-6.el6_7.1.x86_64 (base)
           Requires: libmysqlclient.so.16(libmysqlclient_16)(64bit)
           Removing: mysql-libs-5.1.73-3.el6_5.x86_64 (@updates)
               libmysqlclient.so.16(libmysqlclient_16)(64bit)
           Obsoleted By: mysql-community-libs-5.6.35-2.el7.x86_64 (mysql56-community)
               Not found
           Updated By: mysql-libs-5.1.73-8.el6_8.x86_64 (updates)
               libmysqlclient.so.16(libmysqlclient_16)(64bit)
           Available: mysql-libs-5.1.73-7.el6.x86_64 (base)
               libmysqlclient.so.16(libmysqlclient_16)(64bit)
Error: Package: mysql-community-server-5.6.35-2.el7.x86_64 (mysql56-community)
           Requires: libstdc++.so.6(GLIBCXX_3.4.15)(64bit)
Error: Package: 2:postfix-2.6.6-6.el6_7.1.x86_64 (base)
           Requires: libmysqlclient.so.16()(64bit)
           Removing: mysql-libs-5.1.73-3.el6_5.x86_64 (@updates)
               libmysqlclient.so.16()(64bit)
           Obsoleted By: mysql-community-libs-5.6.35-2.el7.x86_64 (mysql56-community)
               Not found
           Updated By: mysql-libs-5.1.73-8.el6_8.x86_64 (updates)
               libmysqlclient.so.16()(64bit)
           Available: mysql-libs-5.1.73-7.el6.x86_64 (base)
               libmysqlclient.so.16()(64bit)
Error: Package: mysql-community-server-5.6.35-2.el7.x86_64 (mysql56-community)
           Requires: libc.so.6(GLIBC_2.17)(64bit)
Error: Package: mysql-community-server-5.6.35-2.el7.x86_64 (mysql56-community)
           Requires: systemd
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

  

 

 

 

 

【mysql的启动和停止】

# 1) 启动 ---官方推荐使用 mysqld_safe 进程启动,遇到mysqld进程停止,mysqld_safe自动重新启动

nohup /bin/sh /usr/bin/mysqld_safe &

#2) 停止 使用 shutdown停止mysql

mysqladmin -uroot -p shutdown;

 

 

  ===============================================

【mysql启动错误】

检查mysql的错误日志发现 是配置文件问题

cat /var/log/mysqld.log |grep ERROR

2016
-12-05 16:08:12 57836 [ERROR] /usr/sbin/mysqld: unknown variable 'default-character-set=utf8' 2016-12-05 16:08:12 57836 [ERROR] Aborting
vim /etc/my.cnf

 #default-character-set=utf8
  character_set_server=utf8

把字符集编码的设置 更改一下,default 改为character_set

 

 ===============================================

【mysql运维演练】

1、修改mysql的默认数据库路径,转移到指定目录~/mysql_data下

 

   【command list】
    (1)停服务 copy文件
    mysqladmin -uroot -p shutdown; 
   
    cd /var/lib
    cp -a -r /var/lib/mysql    /home/mysql_data/
    
    chown mysql:mysql /home/mysql_data/mysql
    chmod 4777 /home/mysql_data/mysql/mysql.sock
    
    chmod a+x /home/mysql_data/
    chmod mysql:mysql a+x /home/mysql_data/
    
# 启动mysql /bin/sh /usr/bin/mysqld_safe (2)修改3个配置 cp /etc/my.cnf /etc/my.cnfbak cp /usr/bin/mysqld_safe /usr/bin/mysqld_safebak vim /etc/my.cnf datadir=/home/mysql_data/mysql socket=/home/mysql_data/mysql/mysql.sock

vim /usr/bin/mysqld_safe datadir=/home/mysql_data/mysql (3)建立链接 ln -s /home/mysql_data/mysql/mysql.sock /var/lib/mysql/mysql.sock

再启动mysql

/bin/sh /usr/bin/mysqld_safe

 2、部分配置参数的意义整理

       interactive_timeout=300            #服务器关闭交互式连接前等待活动的秒数,单位秒,默认8小时,28800秒。避免闲置连接过多。
       wait_timeout=300                    #服务器关闭非交互连接之前等待活动的秒数与iteractive_timeout一起设置客户端超时时间。
                                        #只有 wait_timeout 会真正起到超时限制的作用
                                        interactive_timeout和wait_timeout在连接空闲阶段(sleep)起作用
                                        
       max_allowed_packet=16M            #消息缓冲区包的最大大小
       
       net_read_timeout=180              #在终止读之前,从一个连接获得数据而等待的时间秒数;
                                         当服务正在从客户端读取数据时,net_read_timeout控制何时超时
       net_write_timeout=180            # 在终止写之前,等待多少秒把block写到连接;
                                         当服务正在写数据到客户端时,net_write_timeout控制何时超时
                                         net_read_timeout和net_write_timeout:则是在连接繁忙阶段(query)起作用
                                         
       max_connect_errors=10000         #主机在连接被中断(网络,或者程序意外中断)等,都会增加 connect_errors的计数值,
当超过了阈值就只能被 blocked,如果连接成功,计数值又会被初始为0;如果出现错误,还是老老实实的 flush hosts吧
max_connections=1000 #mysql 最大连接数 connect_timeout=20 # 在获取连接阶段(authenticate)起作用 bulk_insert_buffer_size = 256M # 批量insert脚本大小设置,有 blob大字段要设置大一些,

 

2、查询结果导出到文件

1) 直接命令行执行 
mysql -h 127.0.0.1 -uroot -p -Ne "use test; select * from test limit 100;" > /tmp/outdata.txt

2) 命令行调用sql文件执行

vim run_out.sql
use test; select * from test limit 100;
mysql -uroot -p -N < run_out.sql > /tmp/outdata.txt

 3、mysql 数据导入

#【source 和 load data infile 命令,进入mysql】

mysql > use test
mysql > set names utf8
mysql > source /home/mysql_data/test.sql;

#load data ,注意 local参数。 以下生成测试数据 并导入测试表
python -c "for i in range(1,1+1000000): print(i)">100w.txt
mysql > create table tmp_series(id int,primary key(id));
mysql > load data local infile '/home/test/100w.txt' replace into table tmp_series;

--------------------------------
[不加local参数,报错]
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

 4、Innodb 和 myisam 存储引擎的差异

Innodb 需要事务处理或是外键,update更快些,严格保持数据一致性时更适合;
select count from tables, innodb实时去扫描表计数,慢
auto_increment自动增长计数器仅被存储在主内存中,而不是存在磁盘上

MyISAM 需要全文索引,大批的inserts 语句在MyISAM下会快一些,count更快,快速写入数据场景更适合;
3个构成文件,.frm文件存储表定义,.MYD 数据文件,.MYI 索引文件
select count from tables, myisam直接读系统结果表,很快
更好和更快的auto_increment处理;

5、数据库表的一些修复方法

# shell 命令行下执行
mysqlcheck -uroot -p database test table  -c  test


# myisamchk修复myisam存储引擎的错误表,对innodb无效,必须在mysql服务停止后进行,-O参数设置各项参数内存大小;

    myisamchk -r tablename
    
    myisamchk –safe-recover tbl_name
    myisamchk -O sort=16M -O key=16M -O read=1M -O write=1M ...


# innnodb的表修复,可通过修改表存储引擎 导数据,再修改回来等方式
具体参考

【Innodb 表修复】http://www.cnblogs.com/zhoujinyi/archive/2013/05/10/3070667.html

 

【master-slave主从复制】

【配置数据库的主从复制,此方案是 服务器对服务器,所有库的任何变更都会同步到从库上,新建库,新建任何表等】
  主从服务需要确保mysql版本一致;log-bin设成二进制日志;server-id主从分别设成唯一值,
  
 #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   //[必须]启用二进制日志
       server-id=222      //[必须]服务器唯一ID,默认是1,一般取IP最后一段,主服务器,
       
       server-id=223      //从服务器

 分别重启两个mysql server。
 
       
【master服务器】
mysql > GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';
mysql > show master status
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      320 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

【slave库】
mysql > change master to master_host='192.168.52.101',master_user='mysync',master_password='q123456',
         master_log_file='mysql-bin.000001',master_log_pos=320;

 【slave库查看主从复制状态】

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.52.101
                  Master_User: mysync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 961
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 924
        Relay_Master_Log_File: mysql-bin.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
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 961
              Relay_Log_Space: 1098
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 222
                  Master_UUID: 6be51420-bb89-11e6-99c1-000c29d16f0c
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

【参考资料馆】

 监控mysql主从复制状态的shell脚本                 http://blog.formyz.org/?paged=6

 centos6.5下安装mysql             http://www.linuxidc.com/Linux/2015-01/111413.htm

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

posted @ 2016-12-02 22:04  快乐无限  阅读(8753)  评论(0编辑  收藏  举报