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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM